Warning: Undefined array key "night" in /www/wwwroot/dhaomu.com/wp-content/themes/b2/header.php on line 18

WordPress网站B2主题美化之文章底部添加所属专题

今天老白博客@老白跟大家分享一下由倡萌原创的“WordPress网站B2主题美化之文章底部添加所属专题”。B2主题的专题功能有利于将相关的文章组织到一起,但是在专题中的文章内页,并没有关于该专题的其他信息,也没有引导用户阅读该专题的其他文章。实现的代码如下,将代码添加到子主题的 functions.php 和style.css即可。

WordPress网站B2主题美化之文章底部添加所属专题插图

(转载自https://www.wpdaxue.com/docs/b2/b2-dev/collection-posts,谢谢站长分享)

function文件代码

/**
* 调用该文章所在专题的其他文章
* https://www.xcbtmw.com/26089.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://www.xcbtmw.com/26089.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' );

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;
}

相关文章请点击文末标签阅读!

温馨提示:
1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:duhaomu@163.com,我们将第一时间处理!
2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。
3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读网站声明

给TA赞赏
共{{data.count}}人
人已赞赏
未整理

动漫Coser@花铃 玛修婚纱 54P

2023-3-29 0:00:00

未整理

快手在线查询权重网站源码+接口

2023-3-30 0:00:00

0 条回复 A文章作者 M管理员

Warning: Trying to access array offset on value of type null in /www/wwwroot/dhaomu.com/wp-content/themes/b2/functions.php on line 3914

Warning: Trying to access array offset on value of type null in /www/wwwroot/dhaomu.com/wp-content/themes/b2/functions.php on line 3914
----《》
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索