我正在尝试创建一个杂志风格的网站。对于这个特殊的页面(不是主页),我想获取最新的帖子并将其设置为单向样式(style1)。对于所有其他帖子,我只想显示较小的(style2)。
这很好,但我也想启用分页。当用户单击之前的帖子时,页面是否会显示在底部?以前的帖子将以第二种样式(style2)显示?
我的代码如下:
<?php
/*
Template Name: Interview
*/
get_header(); ?>
<div id="content" class="wrapper">
<div id="main" class="post">
<ul id="latest-feature">
<h3>Interviews</h3>
<?php
$args=array(
\'cat\' => \'12,3\',
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 3,
\'caller_get_posts\'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
while ($my_query->have_posts()) : $my_query->the_post();
$counter++;
if($counter===1) {
?>
<li class="main-box">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( \'slider-image\' ); } ?>
<h4><?php the_title(); ?></h4></a>
<span><?php the_time(\'j M\') ?> | <?php comments_number(\'No Comments\', \'One Comment\', \'% Comments\' );?></span>
<?php my_excerpt(40); ?>
</li>
</ul>
<ul id="other-features">
<?php } else { ?>
<li class="box">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<span class="box-img">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( \'latest-thumb-small\' ); } ?>
</span>
<div class="box-content">
<h4>
<?php the_title(); ?></a>
</h4>
<span><?php the_time(\'j M\') ?> | <?php comments_number(\'No Comments\', \'One Comment\', \'% Comments\' );?></span>
<?php my_excerpt(30); ?>
</div>
</li>
<?php } ?>
<?php endwhile; ?>
<?php next_posts_link(\'« Older Entries\', $my_query->max_num_pages) ?>
<?php previous_posts_link(\'Newer Entries »\') ?>
</ul>
</div>
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
TL;博士:理想情况下,我想:
将最新帖子显示为样式1将所有其他帖子显示为样式2当用户单击下一步/上一步时,加载更多帖子并将其设置为样式2。除非他们在最新的帖子页面上更新代码;
<?php
/*
Template Name: Film
*/
get_header(); ?>
<div id="content" class="wrapper">
<div id="main" class="post">
<ul id="latest-feature">
<?php
// Only on the first page
if ( ! is_paged() ) {
// Custom query - latest post
$latest_post = new WP_Query( array(
\'cat\' => \'4\',
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 1
) );
// Output latest post
if ( $latest_post->have_posts() ) : while ( $latest_post->have_posts() ) : $latest_post->the_post();
?>
<li class="main-box">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( \'slider-image\' ); } ?>
<h4><?php the_title(); ?></h4></a>
<span><?php the_time(\'j M\') ?> | <?php comments_number(\'No Comments\', \'One Comment\', \'% Comments\' );?></span>
<?php my_excerpt(40); ?>
</li>
</ul>
<ul id="other-features">
<?php
endwhile; endif;
// Don\'t forget me
wp_reset_postdata();
}
// Main query
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
<li class="box">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<span class="box-img">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( \'latest-thumb-small\' ); } ?>
</span>
<div class="box-content">
<h4>
<?php the_title(); ?></a>
</h4>
<span><?php the_time(\'j M\') ?> | <?php comments_number(\'No Comments\', \'One Comment\', \'% Comments\' );?></span>
<?php my_excerpt(30); ?>
</div>
</li>
<?php
endwhile; endif;
?>
</ul>
<?php
// Pagination
next_posts_link();
previous_posts_link();
?>
</div>
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
功能:
function wpse121486_pre_get_posts( $query ) {
// Main blog posts index, first page, main query
if ( is_home() && ! $query->is_paged() && $query->is_main_query() ) {
$query->set( \'posts_per_page\', 9 );
$query->set( \'offset\', 1 );
}
}
add_action(
\'pre_get_posts\', \'wpse121486_pre_get_posts\' );
SO网友:Chip Bennett
编辑,这不起作用的原因是您修改了问题,指定这是在custom page template. 既然是这种情况,您将需要使用两个单独的自定义查询,并且不需要通过pre_get_posts
.
第一个查询将与下面相同。第二个类似,但我们将添加一些条件来说明第一页的特色帖子:
// Main posts query args
$posts_query_args = array(
\'posts_per_page\' => 10
);
// Conditionally modify the query
if ( ! is_paged() ) {
// Apply offset
$posts_query_args[\'offset\'] = 1;
$posts_query_args[\'posts_per_page\'] = 9;
}
// Run the query
$posts_query = new WP_Query( $posts_query_args );
// Loop
if ( $posts_query->have_posts() ) : while ( $posts_query->have_posts() ) : $posts_query->the_post();
// Loop markup here
endwhile; endif;
// Reset post
wp_reset_postdata();
要使分页正常工作,
you\'ll need to refer to this answer.
最初的答案是,我认为最简单的解决方案是:
使用custom query 对于单个最新帖子,请过滤main query 在pre_get_posts
, 应用offset
属于1
此方法将假定最新的post特殊样式将仅出现在第一页上;在随后的页面上,帖子都将使用普通样式。否则,我们必须添加一个额外的修复程序来解释由于使用offset
.
在模板中:
// Only on the first page
if ( ! is_paged() ) {
// Custom query - latest post
$latest_post = new WP_Query( array(
\'posts_per_page\' => 1
) );
// Output latest post
if ( $latest_post->have_posts() ) : while ( $latest_post->have_posts() ) : $latest_post->the_post();
// Style 1 loop
endwhile; endif;
// Don\'t forget me
wp_reset_postdata();
}
// Main query
if ( have_posts() ) : while ( have_posts() ) : the_post();
// Style 2 loop
endwhile; endif;
// Pagination
next_posts_link();
previous_posts_link();
在中
functions.php
:
function wpse121486_pre_get_posts( $query ) {
// Main blog posts index, first page, main query
if ( is_home() && ! $query->is_paged() && $query->is_main_query() ) {
$query->set( \'posts_per_page\', 9 );
$query->set( \'offset\', 1 );
}
}
add_action( \'pre_get_posts\', \'wpse121486_pre_get_posts\' );