Home> CMS Tutorial> WordPress> body text

Quick Tip: After Content - More Content in the Same Category

王林
Release: 2023-09-03 17:53:02
Original
925 people have browsed it

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

Do you have a solid category structure on your blog? If so, you probably don't need a "Related Posts" section at all - you can just display the latest posts in the same category.

In this post, we’ll introduce the “More in this category” section, an alternative to “Related Posts” (which we’ve covered before).


Show you have more to say

If you organize your posts well into categories, you may find it useful to have a list of posts within a post category.

"Related Posts" isn't always the answer: If posts on your site areseparatedby category, then a "Related Posts" section may "break" that separation.

For example, if you have a blog about a different occupational group, you cannot show news about the textile industry as "Related News" under a post about informatics. Many recent posts in the same category would be more relevant, right?

Create a "More in this category" list

As you may have guessed, it is much easier to list the latest posts in a post category than to display related posts based on post tags. We just need to get the category of the post and list the many posts in that category, excluding the post the visitor just read. The parameters we can pass in theget_posts()function contain everything we need.

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; } ?>
Copy after login

Finish! You can include this function in yourfunctions.phpfile (or save it as a separate plugin) and echo it (like) inanywhere in the single.phpfile.


in conclusion

Yes, the content may be "king", but a lonely king is a weak king, and people may not respect that "king".

Do you think there are more page elements that can help "The King"? Please leave your comments below - it's always important for you to share your thoughts with us!

The above is the detailed content of Quick Tip: After Content - More Content in the Same Category. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!