这里有一个更简洁的方法,它不会跳过循环中的第一个帖子
由于这是主页上的主要查询,我们可以执行以下操作:
使用pre_get_posts
更改主页上的主查询,以便设置一个偏移量,以便忽略最新的帖子。
重新计算找到的帖子数量,以便分页可以正确计算要显示的页面数量。
因为我们从第1页中排除了一篇文章,所以将每页偏移一页以下是进入函数的代码。php:(
注意:此代码在使用时至少需要php 5.3anonymous functions/closures. 如果您使用的是较早的PHP版本,请升级您的PHP版本,PHP 5.3及更低版本已经失效add_action( \'pre_get_posts\', function ( $q ) {
if( !is_admin() && $q->is_main_query() && $q->is_home() ) {
$offset = 1;
$ppp = get_option( \'posts_per_page\' );
if ( !$q->is_paged() ) {
$q->set( \'offset\', $offset );
} else {
$offset = $offset + ( ( $q->query_vars[\'paged\']-1 ) * $ppp );
$q->set( \'offset\', $offset );
}
}
return $q;
});
//Recalculate found_posts for the home page
add_filter( \'found_posts\', function ( $found_posts, $q ) {
$offset = 1;
if( $q->is_home() && $q->is_main_query() ) {
$found_posts = $found_posts - $offset;
}
return $found_posts;
}, 10, 2 );
这将从主页循环中删除最新的帖子。现在是小部件为此,我快速修改了一个小部件,以显示最新/最新的帖子。您只需修改它以满足您的确切需要,还需要应用您自己的标记和样式
我不打算在这里讨论细节Widget API 是相当广泛的。你需要在自己的时间去研究这个
小部件如下:(注意:此小部件至少需要PHP 5.4)
/**
* Newest_Post_Widget widget class
*
* Displays the newest post
*
* @since 1.0.0
*/
class Newest_Post_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
\'widget_newest_posts\',
_x( \'Newest Post Widget\', \'Tagged Posts Widget\' ),
[ \'description\' => __( \'Displays the newest post.\' ),
]
);
$this->alt_option_name = \'widget_newest_posts\';
add_action( \'save_post\', [$this, \'flush_widget_cache\'] );
add_action( \'deleted_post\', [$this, \'flush_widget_cache\'] );
add_action( \'switch_theme\', [$this, \'flush_widget_cache\'] );
}
public function widget( $args, $instance ) {
$cache = [];
if ( ! $this->is_preview() ) {
$cache = wp_cache_get( \'widget_new_posts\', \'widget\' );
}
if ( ! is_array( $cache ) ) {
$cache = [];
}
if ( ! isset( $args[\'widget_id\'] ) ) {
$args[\'widget_id\'] = $this->id;
}
if ( isset( $cache[ $args[\'widget_id\'] ] ) ) {
echo $cache[ $args[\'widget_id\'] ];
return;
}
ob_start();
$title = ( ! empty( $instance[\'title\'] ) ) ? $instance[\'title\'] : __( \'Newest Post\' );
/** This filter is documented in wp-includes/default-widgets.php */
$title = apply_filters( \'widget_title\', $title, $instance, $this->id_base );
/**
* Filter the arguments for the Newest Post widget.
*
* @since 1.0.0
*
* @see WP_Query::get_posts()
*
* @param array $args An array of arguments used to retrieve the newest post
*/
$query_args = [
\'posts_per_page\' => 1,
];
$q = new WP_Query( apply_filters( \'newest_post_args\', $query_args ) );
if( $q->have_posts() ) {
echo $args[\'before_widget\'];
if ( $title ) {
echo $args[\'before_title\'] . $title . $args[\'after_title\'];
}
while( $q->have_posts() ) {
$q->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( \'<h1 class="entry-title"><a href="\' . esc_url( get_permalink() ) . \'" rel="bookmark">\', \'</a></h1>\' ); ?>
</header><!-- .entry-header -->
<?php
if ( has_post_thumbnail() ) { ?>
<div class="post-thumbnail">
<?php the_post_thumbnail(); ?>
</div><!--/.post-thumbnail-->
<?php
}
?>
<div class="entry-content">
<?php the_content(); ?>
</div><!-- .entry-content -->
</article><!-- #post-## -->
<?php
}
wp_reset_postdata();
}
echo $args[\'after_widget\'];
if ( ! $this->is_preview() ) {
$cache[ $args[\'widget_id\'] ] = ob_get_flush();
wp_cache_set( \'widget_new_posts\', $cache, \'widget\' );
} else {
ob_end_flush();
}
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
$this->flush_widget_cache();
$alloptions = wp_cache_get( \'alloptions\', \'options\' );
if ( isset($alloptions[\'widget_newest_posts\']) )
delete_option(\'widget_newest_posts\');
return $instance;
}
public function flush_widget_cache() {
wp_cache_delete(\'widget_new_posts\', \'widget\');
}
public function form( $instance ) {
$title = isset( $instance[\'title\'] ) ? esc_attr( $instance[\'title\'] ) : \'\';
?>
<p>
<label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'Title:\' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<?php
}
}
add_action( \'widgets_init\', function () {
register_widget( \'Newest_Post_Widget\' );
});