wp query - Posts feed number of posts

admin2025-06-05  2

Happy Holidays everyone! I hope you can help me with this 'little' problem I'm having :)

I am using a child theme based on Elara which displays the posts feed in different formats on the front page and on the archive pages. The main difference is that on the front page the design best suits 8 posts while on the archive pages it best fits 10 posts before navigation.

However, there is no option to specify the different limits. As far as I can tell the feed is controlled by this:

<?php
/**
 * The loop / blog feed
 *
 * @package elara
 */
?>

<div id="blog-feed" class="section-feed row">
    <?php
        /**
         * The loop
         *
         * For covering all customizer options from above, take a look at elara's feed.php
         */
        if ( have_posts() ) : while ( have_posts() ) : the_post();

            get_template_part( 'content', get_post_format() );

        endwhile; endif;
    ?>
</div><!-- section-feed row -->

Then the content part of the code is this:

<?php
/**
 * Main template for displaying a post within a feed
 *
 * @package elara
 */
$elara_class = elara_set_feed_post_class(); ?>

<article id="post-<?php the_ID(); ?>" <?php post_class( 'entry matcheight ' . esc_attr( $elara_class ) ); ?>>

    <?php elara_entry_thumbnail( 'elara-archive', true ); ?>

    <footer class="entry-meta">
        <div>
            <?php elara_entry_categories(); ?>
            <?php elara_entry_separator( 'categories-date' ); ?>
            <?php elara_entry_date(); ?>
        </div>
        <div>
            <?php elara_entry_author(); ?>
            <?php elara_entry_separator( 'author-comments' ); ?>
            <?php elara_entry_comments_link(); ?>
        </div>
    </footer>

    <?php elara_entry_title(); ?>
    <?php elara_feed_entry_excerpt(); ?>

    <?php
        /**
         * Get video modal for video post format
         */
        get_template_part( 'parts/entry', 'video-modal' );
    ?>

</article><!-- #post-<?php the_ID(); ?> -->

So, my question is: can you help me customize it so the frontpage displays 8 posts before prev/next page navigation controls while the archive pages display 10 posts?

Happy Holidays everyone! I hope you can help me with this 'little' problem I'm having :)

I am using a child theme based on Elara which displays the posts feed in different formats on the front page and on the archive pages. The main difference is that on the front page the design best suits 8 posts while on the archive pages it best fits 10 posts before navigation.

However, there is no option to specify the different limits. As far as I can tell the feed is controlled by this:

<?php
/**
 * The loop / blog feed
 *
 * @package elara
 */
?>

<div id="blog-feed" class="section-feed row">
    <?php
        /**
         * The loop
         *
         * For covering all customizer options from above, take a look at elara's feed.php
         */
        if ( have_posts() ) : while ( have_posts() ) : the_post();

            get_template_part( 'content', get_post_format() );

        endwhile; endif;
    ?>
</div><!-- section-feed row -->

Then the content part of the code is this:

<?php
/**
 * Main template for displaying a post within a feed
 *
 * @package elara
 */
$elara_class = elara_set_feed_post_class(); ?>

<article id="post-<?php the_ID(); ?>" <?php post_class( 'entry matcheight ' . esc_attr( $elara_class ) ); ?>>

    <?php elara_entry_thumbnail( 'elara-archive', true ); ?>

    <footer class="entry-meta">
        <div>
            <?php elara_entry_categories(); ?>
            <?php elara_entry_separator( 'categories-date' ); ?>
            <?php elara_entry_date(); ?>
        </div>
        <div>
            <?php elara_entry_author(); ?>
            <?php elara_entry_separator( 'author-comments' ); ?>
            <?php elara_entry_comments_link(); ?>
        </div>
    </footer>

    <?php elara_entry_title(); ?>
    <?php elara_feed_entry_excerpt(); ?>

    <?php
        /**
         * Get video modal for video post format
         */
        get_template_part( 'parts/entry', 'video-modal' );
    ?>

</article><!-- #post-<?php the_ID(); ?> -->

So, my question is: can you help me customize it so the frontpage displays 8 posts before prev/next page navigation controls while the archive pages display 10 posts?

Share Improve this question edited Dec 25, 2018 at 11:45 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Dec 25, 2018 at 10:11 kr3t3nkr3t3n 1031 bronze badge 3
  • Create your own loop for either front or archive using WP_Query utilizing posts_per_page parameter. – Max Yudin Commented Dec 25, 2018 at 10:44
  • @MaxYudin, I thought that I would have to create my own loop, however my knowledge is quite limited and am unsure how to make it specific to the archive pages and not the frontpage. Also, would I do this on the same .php file (the feed.php)? – kr3t3n Commented Dec 25, 2018 at 11:14
  • You don't have to create loop - just use pre_get_posts filter action... – Krzysiek Dróżdż Commented Dec 25, 2018 at 11:16
Add a comment  | 

1 Answer 1

Reset to default 1

OK, so this theme uses global $wp_query to print these posts. It makes it pretty easy to change the number of posts... All you have to do is to use pre_get_posts action and change query params.

function my_change_number_of_posts( $query ) {
    if ( ! is_admin() && $query->is_main_query() ) {
        if ( is_home() ) {  // change only for home page
            $query->set( 'posts_per_page', 8 );
        }
    }
}
add_action( 'pre_get_posts', 'my_change_number_of_posts' );

Of course you can modify the conditions in that action, to achieve exactly what you need.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749069401a316089.html

最新回复(0)