• Hey,

    After spending half a day trying to figure it out, im still totally lost.
    I would like to display 5 recent posts for each category in front page.

    Here is my code from index.php

    <?php if (have_posts()) : ?>
      <div id="post-listing" class="clearfix">
    
    	<?php
    		// Show only one Sticky Post
    	$sticky = get_option( 'sticky_posts' );
    	$args = array(
    		'posts_per_page' => 1,
    		'post__in'  => $sticky,
    		'ignore_sticky_posts' => 1
    	);
    	query_posts( $args );
    	if ( $sticky[0] ) {
    	while (have_posts()) : the_post();
    		get_template_part( 'loop', 'index' );
    	endwhile;
    	}
    	wp_reset_query();
    
    	// Exclude Sticky Posts and show remaining normal posts
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	query_posts( array(
    		'post__not_in' => get_option( 'sticky_posts' ),
    		'paged' => $paged
    		) );
    
    		while (have_posts()) : the_post();
    		get_template_part( 'loop', 'index' );
    
    		endwhile;
    
    	?>
    
         </div>
      <!-- #post-listing -->
      <?php
    	  sampression_content_nav( 'nav-below' );
    	  else:
    	?>
    
        <article id="post-0" class="no-results not-found">
            <header class="entry-header">
                <h2 class="entry-title"><?php _e( 'Nothing Found', 'sampression' ); ?></h2>
            </header><!-- .entry-header -->
    
            <div class="entry-content">
                <p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'sampression' ); ?></p>
            </div><!-- .entry-content -->
        </article><!-- #post-0 -->
    
    <?php endif; ?>

    How should i change it ?

    Cheers,
    Risto

Viewing 11 replies - 1 through 11 (of 11 total)
  • you will need to replace this section:

    // Exclude Sticky Posts and show remaining normal posts
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	query_posts( array(
    		'post__not_in' => get_option( 'sticky_posts' ),
    		'paged' => $paged
    		) );
    
    		while (have_posts()) : the_post();
    		get_template_part( 'loop', 'index' );
    
    		endwhile;

    then get the site’s categories ( get_categories() https://codex.www.ads-software.com/Function_Reference/get_categories ) and loop through the categories with a custom query for each of them;

    example:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    if( $cats = get_categories() ) foreach( $cats as $cat ) :
    //go through all site's categories and get up to 5 posts each////
    
    	$cat_query = new WP_Query( array(
    		'post__not_in' => get_option( 'sticky_posts' ),
    		'category__in' => array($cat->term_id),
    		'posts_per_page' => 5,
    		'paged' => $paged
    		) );
    
    		if($cat_query->have_posts()) : ?>
    <h3 class="cat-title"><?php echo $cat->name; ?></h3>
    		<?php while ($cat_query->have_posts()) : $cat_query->the_post();
    		//get_template_part( 'loop', 'index' );
    get_template_part( 'content', get_post_format() );
    		endwhile; endif; wp_reset_postdata();
    endforeach;
    Thread Starter ristop

    (@ristop)

    Thanks for the reply. If i change the code as provided, it only displays the name of one category, no post. Should i just replace the code as provided or do i have to change anything there ?

    Cheers,
    Risto

    my mistake – I copied the code from my test site where I used a different template part ??

    corrected:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    if( $cats = get_categories() ) foreach( $cats as $cat ) :
    //go through all site's categories and get up to 5 posts each////
    
    	$cat_query = new WP_Query( array(
    		'post__not_in' => get_option( 'sticky_posts' ),
    		'category__in' => array($cat->term_id),
    		'posts_per_page' => 5,
    		'paged' => $paged
    		) );
    
    		if($cat_query->have_posts()) : ?>
    <h3 class="cat-title"><?php echo $cat->name; ?></h3>
    		<?php while ($cat_query->have_posts()) : $cat_query->the_post();
    		get_template_part( 'loop', 'index' );
    
    		endwhile; endif; wp_reset_postdata();
    endforeach;
    Thread Starter ristop

    (@ristop)

    Thank you so much, it works perfecty ??

    One more question, is it possible to order these posts by date in the front page ? At the moment it lists 5 from one category, and after that takes the other category and list 5, etc.

    Cheers,
    Risto

    Thread Starter ristop

    (@ristop)

    Anyone knows how to order them by date ?

    [ https://codex.www.ads-software.com/Forum_Welcome#No_Bumping ]

    Eu tenho um loop normal:

    <!-- article - noticias -->
    	<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    
    		<!-- post thumbnail -->
    		<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
    			<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    				<?php the_post_thumbnail(array(120,120)); // Declare pixel size you need inside the array ?>
    			</a>
    		<?php endif; ?>
    		<!-- /post thumbnail -->
    
    		<!-- categoria -->
                <div class="categoria-home">
                	<p><?php the_category(''); ?></p>
                </div>
    		<!-- /categoria -->
    
    		<!-- post title -->
    		<h2>
    			<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    		</h2>
    		<!-- /post title -->
    
    	</article>
    	<!-- /article - noticias -->

    Como fa?o para os posts fixos n?o aparecerem nos posts gerais?

    Preciso muito da ajuda!!!

    Obrigado.
    Abra?os, Sully.

    These are English language forums, so please use English. Alternatively, use a WP support forum in your language.

    I have a normal loop:

    <!-- article - news -->
    	<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    
    		<!-- post thumbnail -->
    		<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
    			<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    				<?php the_post_thumbnail(array(120,120)); // Declare pixel size you need inside the array ?>
    			</a>
    		<?php endif; ?>
    		<!-- /post thumbnail -->
    
    		<!-- categoria -->
                <div class="categoria-home">
                	<p><?php the_category(''); ?></p>
                </div>
    		<!-- /categoria -->
    
    		<!-- post title -->
    		<h2>
    			<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    		</h2>
    		<!-- /post title -->
    
    	</article>
    	<!-- /article - news -->

    How do I fixed the posts do not appear in general posts?

    I really need help!!

    Thank you.
    Hugs, Sully.

    How do I fixed the posts do not appear in general posts?

    Sorry? Can you clarify what you are trying to do?

    Necessary that the fixed posts do not appear in other posts general.

    Sorry again. What do you mean by “fixed posts” and “other posts general”? Are you sure you wouldn’t be better off asking this question in a WP support forum in your language?

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘5 recent posts for each category in front page’ is closed to new replies.