How to have a different section for a special category of posts in landing page?

I wish to have a different section for a certain category of posts displayed in the landing page of my WordPress website, while posts of various other categories display in an other section.

Is this possible in WordPress?

Topic featured-post Wordpress

Category Web


I saw your comment on another answer "wow I'm not that into programming or coding." so I figured I'd chime in with another option.

If you're using Elementor or Divi or another similar visual builder, they generally will have a module for blog posts that you can filter by category, which allows you to create a section on a page of posts within a certain category or categories without the need to add code to your site.

For example, in Divi there is a module that allows this that looks the screenshot below.

enter image description here


yes this is definitely possible. You basically you need to do is combine multiple loops with custom loop queries, more info can be found here: The Loop

 <?php $query = new WP_Query( 'cat=CATEGORY ID YOU WANT TO DISPLAY HERE' ); ?>
 <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

    OUTPUT HTML HERE

 <?php endwhile; endif; ?>

 <?php rewind_posts(); ?>

<?php $query = new WP_Query( 'cat=2ND CATEGORY ID YOU WANT TO DISPLAY HERE' ); ?>
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

    OUTPUT HTML HERE

 <?php endwhile; endif; ?>

Remember the rewind_posts() method to get this to work properly


It is possible, you will need to create another loop for that special category using WP_Query class. An example of a loop returning the "cats" category would be:

// WP_Query arguments
$args = array (
    'category_name'          => 'cats',
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        // do something
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();

Depending on the number of sections on your landing page, you will have multiple loops, but do not forget to reset each loop on the page.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.