WordPress 7B2 Pro主题美化:文章底部添加所属专题

WordPress 7B2 Pro主题美化:文章底部添加所属专题

 

 

 

在主题目录下functions.php文件合适位置添加如下代码:

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

 

相关CSS文件:

 

免责说明

本站资源部分来自网友投稿,如有侵犯你的权益请联系管理员或给邮箱发送邮件PubwinSoft@foxmail.com 我们会第一时间进行审核删除。
站内资源为网友个人学习或测试研究使用,未经原版权作者许可,禁止用于任何商业途径!请在下载24小时内删除!


如果遇到评论下载的文章,评论后刷新页面点击对应的蓝字按钮即可跳转到下载页面
本站资源少部分采用7z压缩,为防止有人压缩软件不支持7z格式,7z解压,建议下载7-zip,zip、rar解压,建议下载WinRAR

温馨提示:本站部分付费下载资源收取的费用为资源收集整理费用,并非资源费用,不对下载的资源提供任何技术支持及售后服务。

给TA打赏
共{{data.count}}人
人已打赏
WordPress教程

WordPress中使用CloudFront CDN加速站点访问速度配置教程

2023-5-5 13:05:25

WordPress教程

WordPress 配置多站点网络

2023-5-7 10:10:39

3 条回复 A文章作者 M管理员
  1. u8037525612996846

    恋爱会有差此外外形,我会把最像你的样子记在脑海。

  2. 花醉𐂂

    默默不语,沾化浅笑,一辈子最深的执着陪你浪迹天涯。

  3. fange

    无论我是怎样的人都会一直陪你走下去。

个人中心
购物车
优惠劵
有新私信 私信列表
搜索