云淡轻风 發表於 2023-9-2 00:00:00

wordpress获取当前文章的评论数实现代码

<p>
<span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>有时候,为了需要,是要获取wordpress当前文章的评论数,要想获取文章的评论数必须要知道当前文章的id,怎么获取当前文章的id和文章评论数呢? </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>比如说,循环某一分类的文章: </span></p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<p style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span></p>
<p>
代码如下:</p>
</div>
<p style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
&lt;?php $posts = get_posts( "category=9&amp;numberposts=5" ); ?&gt; <br>
&lt;?php if( $posts ) : ?&gt; <br>
&lt;?php foreach( $posts as $post ) : setup_postdata( $post ); ?&gt; <br>
&lt;?php $id=$post-&gt;ID; echo get_post($id)-&gt;comment_count;?&gt; <br>
&lt;?php endforeach; ?&gt; <br>
&lt;?php endif; ?&gt; </p>
<p>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>如上红色部分,通过$post-&gt;ID获取当前文章的id,然后通过get_post($id)-&gt;comment_count获取当前文章的评论数。</span></p>
頁: [1]
查看完整版本: wordpress获取当前文章的评论数实现代码