1 WordPress相關(guān)文章推薦、猜你喜歡代碼示例
很多WordPress插件都可以實現(xiàn)相關(guān)文章推薦、猜你喜歡的功能。
以下是從標簽、分類獲取相關(guān)文章的猜你喜歡代碼:
<h3>猜你喜歡</h3>
<ul class="related_posts">
<?php
$post_num = 10; //文章數(shù)量
$exclude_id = $post->ID;
$posttags = get_the_tags(); $i = 0;
if ( $posttags ) {
$tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ',';
$args = array(
'post_status' => 'publish',
'tag__in' => explode(',', $tags),
'post__not_in' => explode(',', $exclude_id),
'caller_get_posts' => 1,
'orderby' => 'comment_date',
'posts_per_page' => $post_num,
);
query_posts($args);
while( have_posts() ) { the_post(); ?>
<li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
<?php
$exclude_id .= ',' . $post->ID; $i ++;
} wp_reset_query();
}
if ( $i < $post_num ) {
$cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
$args = array(
'category__in' => explode(',', $cats),
'post__not_in' => explode(',', $exclude_id),
'caller_get_posts' => 1,
'orderby' => 'comment_date',
'posts_per_page' => $post_num - $i
);
query_posts($args);
while( have_posts() ) { the_post(); ?>
<li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
<?php $i++;
} wp_reset_query();
}
if ( $i == 0 ) echo '<li>暫無相關(guān)推薦</li>';
?>
</ul>
$post_num = 10;
是顯示相關(guān)文章的數(shù)量為10。
希望我們網(wǎng)站( https://www.wordpressx.com/ ) 分享的《WordPress代碼實現(xiàn)獲取顯示相關(guān)文章推薦猜你喜歡功能》,對您有幫助。