Wordpress循环首页上不同数量的帖子 [英] Different number of posts on first page in Wordpress loop

查看:93
本文介绍了Wordpress循环首页上不同数量的帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以在首页(Wordpress)上和其他所有页面上张贴不同数量的帖子?这是因为只有我第一页上的第一篇文章才和2篇普通文章一样大.因此,最好不要在首页上发表文章. 有什么想法吗?

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!

欢呼

推荐答案

这对我有用!

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循环首页上不同数量的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆