您的博客上有可靠的類別結構嗎?如果是這樣,您可能根本不需要“相關帖子”部分 – 您只需顯示同一類別的最新帖子即可。
在這篇文章中,我們將介紹“此類別的更多內容”部分,這是“相關帖子”(我們之前介紹過)的替代選項。
表明你還有更多話要說
如果您將帖子按類別組織得很好,您可能會發(fā)現(xiàn)擁有帖子類別中的帖子列表很有用。
“相關帖子”并不總是答案:如果您的網(wǎng)站上的帖子按類別分隔,那么“相關帖子”部分可能會“打破”這種分隔。
例如,如果您有一個關于不同職業(yè)群體的博客,則無法在有關信息學的帖子下將有關紡織行業(yè)的新聞顯示為“相關新聞”。同一類別的許多最新帖子會更相關,對吧?
創(chuàng)建“此類別的更多內容”列表
正如您可能已經(jīng)猜到的,列出帖子類別中的最新帖子比根據(jù)帖子標簽顯示相關帖子要容易得多。我們只需要獲取帖子的類別并列出該類別中的許多帖子,不包括訪問者剛剛閱讀的帖子。我們可以在 get_posts() 函數(shù)中傳遞的參數(shù)包含我們需要的一切。
<?php // "More from This Category" list by Bar?? ünver @ Wptuts+ function wptuts_more_from_cat( $title = "More From This Category:" ) { global $post; // We should get the first category of the post $categories = get_the_category( $post->ID ); $first_cat = $categories[0]->cat_ID; // Let's start the $output by displaying the title and opening the
- $output = ‘
- ‘ . $post_title . ‘
‘ . $title . ‘
‘; // The arguments of the post list! $args = array( // It should be in the first category of our post: ‘category__in’ => array( $first_cat ), // Our post should NOT be in the list: ‘post__not_in’ => array( $post->ID ), // …And it should fetch 5 posts – you can change this number if you like: ‘posts_per_page’ => 5 ); // The get_posts() function $posts = get_posts( $args ); if( $posts ) { $output .= ‘
- ‘; // Let’s start the loop! foreach( $posts as $post ) { setup_postdata( $post ); $post_title = get_the_title(); $permalink = get_permalink(); $output .= ‘
‘; } $output .= ‘
‘; } else { // If there are no posts, we should return something, too! $output .= ‘
Sorry, this category has just one post and you just read it!
‘; } // Let’s close the
‘; return $output; } ?>
完成!您可以將此函數(shù)包含在您的 functions.php 文件中(或將其保存為單獨的插件)并回顯它(如 ) 在 single.php 文件中的任何位置。
結論
是的,內容可能是“王”,但孤獨的王是軟弱的王,人們可能不會尊重那個“王”。
您認為還有更多的頁面元素可以幫助“王”嗎?請在下面發(fā)表您的評論 – 與我們分享您的想法對您來說始終很重要!