How To Add The Block “More Posts From This Category” After The Article In WordPress

3 minutes read

If the content on your site is correctly organized by headings, then you probably will not need the block “Similar entries” on the site, because you can simply display fresh entries from the same heading. 
In today’s article, we will show you how to create a section called “More posts from this category,” as an alternative to “Related posts.”

Show Me What You Have To Say

If your records are organized correctly by rubrics, it may be useful to display a list of entries from the rubric of the current article.

“Similar articles” are not always suitable: if articles on your site are divided into headings, then a block of similar entries can “break” this division.

For example, if you have a blog about various professional groups, then you do not need to display articles about textiles in a block of similar news under the post about information technologies. Some fresh entries from the same heading will be more relevant, right?

Create A List Of “More Entries From This Category”

As you have already guessed, it will be much easier to display the list of fresh entries from the heading of the current article than to display similar entries based on the tags. We will only need to get the entry heading, and then output a certain number of entries from it, with the exception of the article that the user is currently viewing. The arguments that we can pass to the get_posts () functions contain everything that is needed.

 

1 <?php
2 // List of "More entries from this category"
3 function devise_more_from_cat( $title "More entries from this category:" ) {
4     global $post;
5     // We must get the first heading of the current record.
6     $categories = get_the_category( $post->ID );
7     $first_cat $categories[0]->cat_ID;
8     // Начнем $output with header output and opening tag <ul>
9     $output '<div id="more-from-cat"><h3>' $title '</h3>';
10     // Arguments for the list of entries
11     $args array(
12         // It should be in the first section of our post.:
13         'category__in' => array$first_cat ),
14         // Our current record should NOT be on the list.:
15         'post__not_in' => array$post->ID ),
16         // ...and 5 records should be processed - this value can be changed at your discretion:
17         'posts_per_page' => 5
18     );
19     // Function get_posts()
20     $posts = get_posts( $args );
21     if$posts ) {
22         $output .= '<ul>';
23         // Start the cycle
24         foreach$posts as $post ) {
25             setup_postdata( $post );
26             $post_title = get_the_title();
27             $permalink = get_permalink();
28             $output .= '<li><a href="' $permalink '" title="' . esc_attr( $post_title ) . '">' $post_title '</a></li>';
29         }
30         $output .= '</ul>';
31     else {
32         //
If there are no records, we still need to return something.
33         $output .= '<p>Sorry, there is only one entry in this section and you have just read it.!</p>';
34     }
35     // We tag <div> and return $output:
36     $output .= '</div>';
37     return $output;
38 }
39 ?>

Done! You can add this function to your functions.php file of your theme or to the plugin for the WordPress site , and then output it (like this <? Php echo devise_more_from_cat (‘More posts from this category:’);?> ) Anywhere in file single.php themes.

Facebook Twitter LinkedIn Telegram Pocket

Related Posts:

Filtering posts in WordPress by category allows you to display only the posts that belong to a specific category or categories. This can be helpful if you want to create custom pages or sections on your website that focus on specific topics.To filter posts by ...
Have you ever had a need to bring your fresh posts from each separate column to your WordPress sidebar? Recently, one of our users asked us if there was a way to quickly bring up fresh posts from a specific category to the WordPress sidebar widget. In today’s ...
To transfer blog posts from one WordPress site to another, you can follow these steps:Export the blog posts: On the WordPress site you want to transfer the blog posts from, go to the WordPress dashboard and navigate to the &#34;Tools&#34; section. Click on &#3...