Rumah > Tutorial CMS > WordPress > teks badan

快速提示:内容之后 - 同一类别的更多内容

王林
Lepaskan: 2023-09-03 17:53:02
asal
874 orang telah melayarinya

快速提示:内容之后 - 同一类别的更多内容

您的博客上有可靠的类别结构吗?如果是这样,您可能根本不需要“相关帖子”部分 - 您只需显示同一类别的最新帖子即可。

在这篇文章中,我们将介绍“此类别的更多内容”部分,这是“相关帖子”(我们之前介绍过)的替代选项。


表明你还有更多话要说

如果您将帖子按类别组织得很好,您可能会发现拥有帖子类别中的帖子列表很有用。

“相关帖子”并不总是答案:如果您的网站上的帖子按类别分隔,那么“相关帖子”部分可能会“打破”这种分隔。

例如,如果您有一个关于不同职业群体的博客,则无法在有关信息学的帖子下将有关纺织行业的新闻显示为“相关新闻”。同一类别的许多最新帖子会更相关,对吧?

创建“此类别的更多内容”列表

正如您可能已经猜到的,列出帖子类别中的最新帖子比根据帖子标签显示相关帖子要容易得多。我们只需要获取帖子的类别并列出该类别中的许多帖子,不包括访问者刚刚阅读的帖子。我们可以在 get_posts() 函数中传递的参数包含我们需要的一切。

ID );
    $first_cat = $categories[0]->cat_ID;
    // Let's start the $output by displaying the title and opening the 
    $output = '

    ' . $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 .= '
    • ' . $post_title . '
    • '; } $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
    and return the $output: $output .= '
    '; return $output; } ?>
Salin selepas log masuk

完成!您可以将此函数包含在您的 functions.php 文件中(或将其保存为单独的插件)并回显它(如 ) 在 single.php 文件中的任何位置。


结论

是的,内容可能是“王”,但孤独的王是软弱的王,人们可能不会尊重那个“王”。

您认为还有更多的页面元素可以帮助“王”吗?请在下面发表您的评论 - 与我们分享您的想法对您来说始终很重要!

Atas ialah kandungan terperinci 快速提示:内容之后 - 同一类别的更多内容. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!