【问题标题】:Different number of posts on first page in Wordpress loopWordpress 循环中第一页上不同数量的帖子
【发布时间】:2015-03-31 04:02:17
【问题描述】:

有没有办法让第一页(Wordpress)的帖子数量与其他所有帖子不同?这是因为只有我在第一页上的第一篇文章有​​ 2 篇普通文章那么大。所以最好在第一页少发一个帖子。 有什么想法吗?

这是我目前所得到的:

<?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; ?>

希望有人可以帮助我们!

干杯

【问题讨论】:

  • 到目前为止你尝试过什么?我看不出上面代码的哪一部分是为了做到这一点。
  • 对不起,现在它在里面。似乎可以工作,但它不是很帅。
  • 我明白了。你试过谷歌搜索吗? “wordpress第一页上的帖子数量不同”似乎有一堆结果?包括这个可能有帮助的:wordpress.stackexchange.com/questions/155758/…
  • 做到了,但是感谢您的链接,但我仍然想知道如何让它工作。我想没有快速修复我的代码对吗?

标签: wordpress loops post pagination


【解决方案1】:

这对我有用!

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 );

【讨论】:

  • 是的,我遇到了这个老问题,想把它包装起来并分享我用来解决问题的方法,代码的和平显然来自上面的共享链接 (wordpress.stackexchange.com/questions/155758/…) ;)
  • 加一个用于发布和关闭此主题。已经投票了:-)
  • 您使用的示例(来自@PieterGoosen)在首页显示一篇文章more,而您的问题是显示一篇文章less 在第一页。上面的答案产生无效的输出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多