Как получить «липкий» пост за пределами «петли»?
-
16-10-2019 - |
Вопрос
Мне нужно отобразить «липкий» пост в разделе, который находится за пределами «петли».
Кто -нибудь может сказать мне, как это сделать?
Решение
Я использую этот запрос;
<?php
$sticky = get_option( 'sticky_posts' ); // Get all sticky posts
rsort( $sticky ); // Sort the stickies, latest first
$sticky = array_slice( $sticky, 0, 1 ); // Number of stickies to show
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) ); // The query
if (have_posts() ) { while ( have_posts() ) : the_post(); ?>
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<?php endwhile;?>
<?php } else { echo ""; }?>
Надеюсь, это тоже будет на тебя .. :)
Другие советы
$last_sticky_post = get_post(end(get_option('sticky_posts')));
if(!empty($last_sticky_post)):
setup_postdata($last_sticky_post);
the_title(); // standard loop here...
the_content();
endif;
(Это получает один пост)
использовать get_posts () с 'include' => implode(',', get_option('sticky_posts'))
Аргумент, чтобы получить все липкие посты (решение Мартина без array_slice сделает то же самое).
Не связан с wordpress.stackexchange