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).
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 are separated by 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?
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 the get_posts()
function contain everything we need.
<?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 <ul> $output = '<div id="more-from-cat"><h3>' . $title . '</h3>'; // 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 .= '<ul>'; // Let's start the loop! foreach( $posts as $post ) { setup_postdata( $post ); $post_title = get_the_title(); $permalink = get_permalink(); $output .= '<li><a href="' . $permalink . '" title="' . esc_attr( $post_title ) . '">' . $post_title . '</a></li>'; } $output .= '</ul>'; } else { // If there are no posts, we should return something, too! $output .= '<p>Sorry, this category has just one post and you just read it!</p>'; } // Let's close the <div> and return the $output: $output .= '</div>'; return $output; } ?>
Finish! You can include this function in your functions.php file (or save it as a separate plugin) and echo it (like <?php echo wptuts_more_from_cat( 'More From This Category:' ); ? >
) in anywhere in the single.php file.
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!