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

If the content on your site is well organized by headings, then you probably won’t need the “Similar posts” block on the site at all, because you can simply display fresh posts from the same heading.
In today’s article we will show you how to create a “More Posts from This Category” section as an alternative to “Related Posts”.

Show that you have what to say
If your posts are correctly organized by heading, it may be helpful to list posts from the heading of the current article.

“Similar articles” are not always suitable: if articles on your site are divided into categories, then a block of similar posts 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 the similar news block under the information technology entry. A certain amount of recent posts from the same category would be more relevant, wouldn’t it?

Create a List of “More Posts From This Category”
As you may have guessed, it will be much easier to display a list of recent posts from the heading of the current article than to display similar posts based on the tags affixed. We just need to get the category of the post, and then output a certain number of posts from it, except for the article that the user is currently viewing. The arguments that we can pass to the get_posts () function contain everything we need.

// Список "Больше записей из этой рубрики"
function devise_more_from_cat( $title = "Больше записей из этой рубрики:" ) {
    global $post;
    // Мы должны получить первую рубрику текущей записи
    $categories = get_the_category( $post->ID );
    $first_cat = $categories[0]->cat_ID;
    // Начнем $output с вывода заголовка и открывающего тега <ul>
    $output = '<div id="more-from-cat"><h3>' . $title . '</h3>';
    // Агрументы для списка записей
    $args = array(
        // Он должен быть в первой рубрике нашей записи:
        'category__in' => array( $first_cat ),
        // Наша текущая запись НЕ должна быть в списке:
        'post__not_in' => array( $post->ID ),
        // ...и должно обрабатываться 5 записей - это значение вы можете изменить на своё усмотрение:
        'posts_per_page' => 5
    // Функция get_posts()
    $posts = get_posts( $args );
    if( $posts ) {
        $output .= '<ul>';
        // Начинаем цикл
        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 {
        // Если записей нет, то нам всё равно нужно что-то вернуть
        $output .= '<p>Извините, в этой рубрике только одна запись и Вы только что ее прочли!</p>';
    // Закываем тег <div> и возвращаем $output:
    $output .= '</div>';
    return $output;

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

Yes, content is a king, but a lonely king is a weak king, and people can stop respecting such a king.

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro
It looks like you\'re using an Adblock. That\'s okay. Who doesn\'t😔

But without advertising, we can\'t keep making this awesome.
Please disable Adblock for our domain🙏