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:

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 article, we will show you how to display the latest posts for each rubric in the WordPress sidebar. There are two ways to display fresh posts from each individual column in WordPress. The first method is really simple and convenient for...
Have you decided to add keywords and a meta description to WordPress content? Keywords and page descriptions allow you to improve your site’s SEO. In this article, we will show you how to properly add keywords and meta descriptions in WordPress.   Why Add Keywords And Meta Descriptions To WordPress Key words and descriptions will allow you to tell search engines about your content in posts and pages. Keywords are important words or phrases that people most often request if they were looking...
Would you like to add a WordPress widget to your site’s header area? Widgets allow you to easily add content blocks to specific places in your theme. In today’s article, we will show you how easy it is to add a WordPress widget to the site header. Why And When It May Be Necessary To Add A Widget To The Header Widgets allow you to easily add content blocks to specific areas of your theme. These areas are called sidebars or widget areas. The area for widgets in the header or in front of t...