文章中添加該文章所屬專題的其他文章列表

  1. 主頁(yè)
  2. 文檔
  3. B2主題使用教程
  4. B2主題二開(kāi)
  5. 文章中添加該文章所屬專題的其他文章列表

B2主題的專題功能有利于將相關(guān)的文章組織到一起,但是在專題中的文章內(nèi)頁(yè),并沒(méi)有關(guān)于該專題的其他信息,也沒(méi)有引導(dǎo)用戶閱讀該專題的其他文章。今天倡萌就和大家分享下優(yōu)化方案。

具體效果:在文章頂部顯示該專題前面的文章,在文章底部顯示該專題后面的文章。

文章中添加該文章所屬專題的其他文章列表 - Collection Posts

實(shí)現(xiàn)的代碼如下,將代碼添加到子主題的 functions.php 即可。

/**
 * 調(diào)用該文章所在專題的其他文章
 * http://www.ydqwiac.cn/docs/b2/b2-dev/collection-posts
 */
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.' 篇。請(qǐng)繼續(xù)閱讀該專題下面的文章:</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();
                
                }
            }
        }
        
    }
}

/**
 * 文章中添加該文章所屬專題的其他文章列表
 * http://www.ydqwiac.cn/docs/b2/b2-dev/collection-posts
 */
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;
}

有能力的朋友可以自行修改代碼。已將該方案提交給春哥,后續(xù)更新應(yīng)該會(huì)集成到父主題。

這篇文章對(duì)您有用嗎? 3 4
6 條回復(fù) A文章作者 M管理員
  1. 設(shè)計(jì)趁年華

    我怎么加上之后,閱讀前,閱讀后的專題都在文章地步呢

    • 網(wǎng)站編輯

      不清楚,大學(xué)網(wǎng)站正在使用,目前沒(méi)發(fā)現(xiàn)問(wèn)題哦,你也可以試試最后一行代碼后面添加一個(gè)優(yōu)先級(jí) add_filter(‘the_content’, ‘b2child_display_collection_posts’, 999 );

    • 網(wǎng)站編輯

      上面的代碼的單引號(hào)被轉(zhuǎn)義為中文的了,注意將上面代碼的單引號(hào)換成英文的哦

  2. user25898

    有個(gè)問(wèn)題假如這個(gè)專題有100+文章,那樣顯示出來(lái)豈不是太長(zhǎng)了?建議加個(gè)縮略顯示。

    • 網(wǎng)站編輯

      通過(guò)樣式限制了最大高度,超過(guò)高度會(huì)顯示滾動(dòng)條的

?
個(gè)人中心
購(gòu)物車(chē)
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索

郎溪县| 常州市| 林芝县| 西峡县| 富源县| 辽中县| 个旧市| 青冈县| 余姚市| 漳州市| 岗巴县| 昌都县| 云南省| 中江县| 罗江县| 长乐市| 寻乌县| 吉林省| 手游| 泾川县| 台东县| 聂荣县| 周宁县| 澎湖县| 阳谷县| 迁安市| 小金县| 青田县| 司法| 沂源县| 华阴市| 长沙县| 建德市| 克拉玛依市| 新巴尔虎左旗| 郴州市| 灵璧县| 射洪县| 宽甸| 汝城县| 黄陵县|