一组PHP代码实现WordPress相关文章调用 增加SEO内链粘性

我们有些时候下载的海外的WordPress主题的内置SEO功能不够好,都需要我们自己用插件或者手动修改代码实现一些功能。比如面包屑导航、相关文章调用都需要我们设置。麦子的策略就是能不用插件的就不用插件,这里我要在单页模板中调用相关文章,这里有一组PHP代码实现。

<div class="related_posts">
<h3>这几篇文章你可能也喜欢:</h3>
<ul>
<?php
$post_num = 10;
$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>没有相关文章!</li>';
?>
</ul>
</div>

这里我们将代码添加到需要调用展现当前篇幅文章相关文章,然后对于CSS部分我们自己设置。

投票 post
© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享