如何查询 WordPress 上最近的 5 个置顶帖子
How can I query recent 5 sticky posts on WordPress
我试过循环使用这段代码,它只给了我一个 post 来自粘性 posts:
<div id="content">
<ul class="disclosure table group">
<?php
$sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 10,
'post__in' => $sticky,
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
if ( isset($sticky[0]) ) {
?>
<li style="text-align: justify; font-weight: 500; color: #b30404;">
<a href="<?php the_permalink(); ?>" style="color: #b30404;" title="<?php the_title(); ?>"><span style="font-size: 15px;"><?php the_title(); ?> </span></a>
</li>
<?php } ?></ul></div>
我希望它显示 5。我已尝试将此行添加到代码中但不起作用:
$sticky = array_slice( $sticky, 0, 5 );
我需要有关如何使此代码显示 5 个最新 post 的帮助(仅粘性 post)。或者给我一个我可以使用的代码,这将是我请求的解决方案。提前致谢
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
$sticky = array_slice( $sticky, 0, 5 );
$the_query = new WP_Query( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1 ) );
试试这个
并删除这一行 if ( isset($sticky[0]) ) {
在查询之后添加这个循环
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_title();
}
}
我试过循环使用这段代码,它只给了我一个 post 来自粘性 posts:
<div id="content">
<ul class="disclosure table group">
<?php
$sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 10,
'post__in' => $sticky,
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
if ( isset($sticky[0]) ) {
?>
<li style="text-align: justify; font-weight: 500; color: #b30404;">
<a href="<?php the_permalink(); ?>" style="color: #b30404;" title="<?php the_title(); ?>"><span style="font-size: 15px;"><?php the_title(); ?> </span></a>
</li>
<?php } ?></ul></div>
我希望它显示 5。我已尝试将此行添加到代码中但不起作用:
$sticky = array_slice( $sticky, 0, 5 );
我需要有关如何使此代码显示 5 个最新 post 的帮助(仅粘性 post)。或者给我一个我可以使用的代码,这将是我请求的解决方案。提前致谢
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
$sticky = array_slice( $sticky, 0, 5 );
$the_query = new WP_Query( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1 ) );
试试这个
并删除这一行 if ( isset($sticky[0]) ) {
在查询之后添加这个循环
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_title();
}
}