WordPress无插件调用最新评论内容实现代码

一般,我们选择的默认WordPress主题都可以自带评论内容调用的,只需要在侧栏小工具拖动模块就可以实现评论的调用。但是有些我们需要自己定义的模板不可能实现自动的模块形式。因为是自己使用而已不需要发布出去给别人使用。比如老蒋经常有很多企业网站需要调用评论,那就直接一个代码调用出来就可以了。

具体我们如何实现的呢?

<?php
$post_num = 8; // 设置调用8条数
$args = array(
'post_password' =>'',
'post_status' => 'publish', // 只选公开的文章.
'post__not_in' => array($post->ID),//排除当前文章
'caller_get_posts' => 1, // 排除置顶文章.
'orderby' => 'comment_count', // 依评论数排序.
'posts_per_page' => $post_num
);
$query_posts = new WP_Query();
$query_posts->query($args);
while( $query_posts->have_posts() ) { $query_posts->the_post(); ?>
<li ><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php } wp_reset_query();?>

将代码添加到WordPress首页模板文件合适的位置,就可以调用自定义调用的评论数量。

投上你的一票

本文出处:老蒋部落 » WordPress无插件调用最新评论内容实现代码 | 欢迎分享( 公众号:老蒋朋友圈 )

公众号 「老蒋朋友圈」获取站长新知 / 加QQ群 【1012423279】获取商家优惠推送