Different number of posts on first page in Wordpre

2019-06-11 02:09发布

Is there a way to have a different number of posts op the first page (Wordpress) then all the others? This because only my first post on the first page is as big as 2 normal posts. So would be nice to have a post less on the first page. Any ideas?

This is what i've got so far:

<?php 
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); } 
else { $paged = 1; }
?>

<?php if ($paged == 1) : ?>
<?php query_posts('posts_per_page=11&paged=' . $paged); ?>
<?php else : ?>
<?php query_posts('posts_per_page=10&paged=' . $paged); ?>
<?php endif;?>

<?php if (have_posts()) : ?>
<?php $postcount = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $postcount++; ?>

<?php if ($postcount == 1 && $paged == 1) : // if this is the first post & first page ?>
<div class="large-10">
<?php the_post_thumbnail('large'); ?>
</div>

    <?php else : //if this is NOT the first post ?>         
    <div class="large-6 columns">
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <div class="portfolio">
        <a href="<?php the_permalink(); ?>">
        <?php the_post_thumbnail('large'); ?>
        <span><h6><?php the_title(); ?></h6></span>
        </a>
    </div>

    </article>
    </div>

<?php endwhile; ?>
<?php endif; ?>

Hope someone can help us out!

Cheers

1条回答
别忘想泡老子
2楼-- · 2019-06-11 02:49

This worked for me!

function tax_and_offset_homepage( $query ) {
if ($query->is_home() && $query->is_main_query() && !is_admin()) {
$query->set( 'post_type', 'my_post_type' );
$query->set( 'post_status', 'publish' );
$query->set( 'ignore_sticky_posts', '-1' );
$tax_query = array(
    array(
        'taxonomy' => 'my_taxo',
        'field' => 'slug',
        'terms' => array('slug1', 'slug2', 'slug3')
    )
);
$query->set( 'tax_query', $tax_query );
$ppp = get_option('posts_per_page');
$offset = 1;
if (!$query->is_paged()) {
  $query->set('posts_per_page',$offset + $ppp);
} else {
  $offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
  $query->set('posts_per_page',$ppp);
  $query->set('offset',$offset);
}
}
}
add_action('pre_get_posts','tax_and_offset_homepage');

function homepage_offset_pagination( $found_posts, $query ) {
$offset = 1;

if( $query->is_home() && $query->is_main_query() ) {
    $found_posts = $found_posts - $offset;
}
return $found_posts;
}
add_filter( 'found_posts', 'homepage_offset_pagination', 10, 2 );
查看更多
登录 后发表回答