我希望在内容循环中回应最新的帖子。只有1个帖子。
我是否需要在以下代码中添加某种参数:
if (have_posts()) :
while (have_posts()) :
the_post();
the_content();
endwhile;
endif;
我该怎么做?我希望在内容循环中回应最新的帖子。只有1个帖子。
我是否需要在以下代码中添加某种参数:
if (have_posts()) :
while (have_posts()) :
the_post();
the_content();
endwhile;
endif;
我该怎么做?如果它是您的主循环,您可以将其添加到function.php
:
function custom_pre_get_posts($query) {
if($query->is_main_query() && /* is sticky conditional tag(sorry I need to check what is that) */) {
$query->set(\'posts_per_page\', "1");
}
}
add_action(\'pre_get_posts\', \'custom_pre_get_posts\');
也可以在主题文件中创建新的WP\\U查询实例:<?php
$onePost = new WP_Query(array(
\'post_type\' => // your post type,
\'posts_per_page\' => 1,
\'post__in\' => get_option(\'sticky_posts\')
));
if($onePost->have_posts()) :
while(($onePost->have_posts()) :
$onePost->the_post();
?>
// your post HTML as normal
<?php
endwhile;
wp_reset_query();
endif;
?>
请注意,如果使用WP\\u查询,则需要设置自定义帖子所需的所有参数。您可以在WP\\U查询法典中阅读此内容给你。。
function latest_sticky() {
/* Get all sticky posts */
$sticky = get_option( \'sticky_posts\' );
/* Sort the stickies with the newest ones at the top */
rsort( $sticky );
/* Get the latest */
$sticky = array_slice( $sticky, 0, 1 ); //Change the 1 to get more than one.
/* Query sticky posts */
$the_query = new WP_Query( array( \'post__in\' => $sticky, \'ignore_sticky_posts\' => 1 ) );
// The Loop
if ( $the_query->have_posts() ) {
$return .= \'<ul>\';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$return .= \'<li><a href="\' .get_permalink(). \'" title="\' . get_the_title() . \'">\' . get_the_title() . \'</a><br />\' . get_the_excerpt(). \'</li>\';
}
$return .= \'</ul>\';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
return $return;
}
只要做一个<?php echo latest_sticky();?>
模板的任何位置。抱歉,我是一个PHp初学者,我不知道为什么这个循环对我的标签不起作用。php页面欢迎任何帮助。<?php get_header(); ?> <div class=\"posts\"><!-- BLOG --> <!-- Shapes on sides --> <div class=\"shapes_left\"> </div> <div class=\"shape