Quick Tip: After Content - More Content in the Same Category
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 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?
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 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.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



WordPress is easy for beginners to get started. 1. After logging into the background, the user interface is intuitive and the simple dashboard provides all the necessary function links. 2. Basic operations include creating and editing content. The WYSIWYG editor simplifies content creation. 3. Beginners can expand website functions through plug-ins and themes, and the learning curve exists but can be mastered through practice.

WordPressisgoodforvirtuallyanywebprojectduetoitsversatilityasaCMS.Itexcelsin:1)user-friendliness,allowingeasywebsitesetup;2)flexibilityandcustomizationwithnumerousthemesandplugins;3)SEOoptimization;and4)strongcommunitysupport,thoughusersmustmanageper

Can learn WordPress within three days. 1. Master basic knowledge, such as themes, plug-ins, etc. 2. Understand the core functions, including installation and working principles. 3. Learn basic and advanced usage through examples. 4. Understand debugging techniques and performance optimization suggestions.

Wix is suitable for users who have no programming experience, and WordPress is suitable for users who want more control and expansion capabilities. 1) Wix provides drag-and-drop editors and rich templates, making it easy to quickly build a website. 2) As an open source CMS, WordPress has a huge community and plug-in ecosystem, supporting in-depth customization and expansion.

WordPress itself is free, but it costs extra to use: 1. WordPress.com offers a package ranging from free to paid, with prices ranging from a few dollars per month to dozens of dollars; 2. WordPress.org requires purchasing a domain name (10-20 US dollars per year) and hosting services (5-50 US dollars per month); 3. Most plug-ins and themes are free, and the paid price ranges from tens to hundreds of dollars; by choosing the right hosting service, using plug-ins and themes reasonably, and regularly maintaining and optimizing, the cost of WordPress can be effectively controlled and optimized.

People choose to use WordPress because of its power and flexibility. 1) WordPress is an open source CMS with strong ease of use and scalability, suitable for various website needs. 2) It has rich themes and plugins, a huge ecosystem and strong community support. 3) The working principle of WordPress is based on themes, plug-ins and core functions, and uses PHP and MySQL to process data, and supports performance optimization.

WordPress is a Content Management System (CMS). It provides content management, user management, themes and plug-in capabilities to support the creation and management of website content. Its working principle includes database management, template systems and plug-in architecture, suitable for a variety of needs from blogs to corporate websites.

The core version of WordPress is free, but other fees may be incurred during use. 1. Domain names and hosting services require payment. 2. Advanced themes and plug-ins may be charged. 3. Professional services and advanced features may be charged.
