【修改】wordpress 文章末尾加上相关文章--亲测可用

介绍如何在WordPress文章底部添加相关推荐文章,通过自定义PHP代码实现,并提供了一个实用的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天给大家介绍一下在wordpress 文章末尾加上相关文章,因为之前的加了之后,个别朋友提出,有些主题调用标签会出错,所以修改了另一个版本的!亲测可用!

先看案例


具体案例可以 在文章里面看到

怎么实现的呢?下面是代码,复制代码到 文章函数 (single.php) 对应的位置里面 即可  



因为各个主题不一样,这里以知更鸟4.4 为例

在主题编辑里面找到 文章底部对应的位置



<!-- 相关文章开始 -->
<ul class="spostinfo">

<h3>80%的人都看过的文章</h3>
<ul class="related_posts">
<?php
$post_num = 8;
$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>';
?>


		<br/><li><strong><?php _e( '版权声明:', 'begin' ); ?></strong>本文版权归<a href="http://bushen.me">补肾么</a>和原作者所有,未经许可不得转载。文章部分来源于网络仅代表作者看法,如有不同观点,欢迎进行交流。除非注明,文章均由 <a href="http://bushen.me">补肾么</a> 整理发布,欢迎转载,转载请带版权。</li><br/>
		<li class="reprinted"><strong><?php _e( '来源: ', 'begin' ); ?></strong> 补肾么(http://bushen.me/),分享补肾壮阳知识,解答补肾壮阳问题,分享壮阳补肾秘方。<p>本文链接:<a href="<?php echo Bing_current_url();?>" rel="bookmark" title="本文固定链接 <?php echo Bing_current_url();?>"><?php echo Bing_current_url();?></a></p></li>
	</ul>
<!-- 相关文章结束 -->

具体可参考:http://bushen.me/zhuangyangff/133.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值