b2主题文章中添加该文章所属专题的其他文章列表

B2主题的专题功能有利于将相关的文章组织到一起,但是在专题中的文章内页,并没有关于该专题的其他信息,也没有引导用户阅读该专题的其他文章。今天倡萌就和大家分享下优化方案。

具体效果:在文章顶部显示该专题前面的文章,在文章底部显示该专题后面的文章。
b2主题文章中添加该文章所属专题的其他文章列表

/**
* 调用该文章所在专题的其他文章
* https://118ms.com/93603.html
*/
function b2child_get_collection_posts_list( $location = 'before' ) {

if( is_singular('post') ) {

$post_id = get_the_ID();

if( has_term( '', 'collection' ) ) {
$terms = get_the_terms( $post_id, 'collection' );
if( $terms && !is_wp_error($terms) ) {
foreach ( $terms as $term ) {
$term_id = $term->term_id;
$term_name = $term->name;
$term_desc = $term->description;

$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'nopaging' => true,
'ignore_sticky_posts' => true,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'collection',
'field' => 'term_id',
'terms' => $term_id,
),
),
);

$the_query = new WP_Query( $args );

$count = $current = false;

if ( $the_query->have_posts() ) {

ob_start();

$i = 1;
while ( $the_query->have_posts() ) {
$the_query->the_post();

$count = $the_query->found_posts;
if( $post_id == get_the_ID() ) {
$current = $i;
}
$i++;
}

$class = 'collection-before';
if( $location == 'after' ) $class = 'collection-after';

if( ($location == 'before' && $current > 1) || ($location == 'after' && $current < $count) ){

echo '<div class="collection-posts '.$class.'">';

if( $location == 'before' ) {
echo '<p class="before">文本是《<a target="_blank" title="'.$term_desc.'" href="'.esc_url( get_term_link($term_id) ).'">'.$term_name.'(共'.$count.'篇)</a>》专题的第 '.$current.' 篇。阅读本文前,建议先阅读前面的文章:</p>';
} elseif ( $location == 'after' ) {
echo '<p class="after">您已阅读完《<a target="_blank" title="'.$term_desc.'" href="'.esc_url( get_term_link($term_id) ).'">'.$term_name.'(共'.$count.'篇)</a>》专题的第 '.$current.' 篇。请继续阅读该专题下面的文章:</p>';
}

echo '<ul class="collection-posts-ul ">';

$i = 1;
while ( $the_query->have_posts() ) {
$the_query->the_post();

if( ( $location == 'before' && $i < $current ) || ( $location == 'after' && $i > $current ) ) {
echo '<li><span>'.$i.'.</span><a href="'.esc_url( get_permalink() ).'">' . get_the_title() . '</a></li>';
}

$i++;
}

echo '</ul>';

echo '</div>';
}
}
wp_reset_postdata();

return ob_get_clean();

}
}
}

}
}
/**
* 文章中添加该文章所属专题的其他文章列表
* https://118ms.com/93603.html
*/
function b2child_display_collection_posts( $content ) {

if( is_singular('post') ) {

$before = b2child_get_collection_posts_list( 'before' );

$after = b2child_get_collection_posts_list( 'after' );

$content = $before ."\n". $content ."\n". $after;
}

return $content;

}
add_filter('the_content', 'b2child_display_collection_posts', 999 );

参考的样式代码如下,可以添加到子主题的 style.css 中:

.collection-before {
border-bottom: 1px solid #ddd;
}
.entry-content > .collection-after {
border-top: 1px solid #ddd;
padding-top: 1.5em;
margin-bottom: 0;
}
.collection-posts-ul {
background: #f5f5f5;
padding: 10px 0;
border-radius: 2px;
max-height: 290px;
overflow-y: hidden;
}
.collection-posts-ul:hover {
overflow-y: scroll;
}
.entry-content .collection-posts-ul a {
color: #444;
}
.entry-content .collection-posts-ul a:hover {
color: #ff3657;
}
温馨提示:本站提供的一切软件、教程和内容信息都来自网络收集整理,仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负,版权争议与本站无关。用户必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解! 联系邮箱:lgg.sinyi@qq.com

给TA打赏
共{{data.count}}人
人已打赏
分享B2主题美化包、B2 pro主题美化包、B2子主题

B2PRO主题显示评论者IP的归属地及运营商信息

2025-10-7 4:52:32

分享B2主题美化包、B2 pro主题美化包、B2子主题

B2主题代码添加精美弹窗样式美化(全部主题适用)

2025-10-7 15:47:30

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索