Home >CMS Tutorial >WordPress >How to get the list of pinned articles in WordPress

How to get the list of pinned articles in WordPress

藏色散人
藏色散人Original
2019-12-07 11:00:093312browse

How to get the list of pinned articles in WordPress

#How to get the list of pinned articles in wordpress?

In WordPress, maybe you want to call the set specified article list. How to implement this function? The implementation method is introduced below, please refer to it

Recommended: "WordPress Tutorial"

First of all, you need to understand the query_posts function. The function of this function is to retrieve, select, and sort articles, and use the selected and sorted articles in the subsequent LOOP loop. For example:

The code is as follows

<?php
query_posts(&#39;posts_per_page=10&ignore_sticky_posts=1&orderby=rand&#39;);
while(have_posts()):the_post();
echo &#39;<li>&#39;;the_title();echo &#39;</li>&#39;;
endwhile;
wp_reset_query();

will randomly list the title of an article. As for the specific parameters of query_posts, please refer to the development manual.

Next, we need to select the top article list by adjusting the parameters of query_posts.

The code is as follows:

$query_post = array(
&#39;posts_per_page&#39; => 10,
&#39;post__in&#39; => get_option(&#39;sticky_posts&#39;),
&#39;caller_get_posts&#39; => 1
);
query_posts($query_post);
?>
<ul style="display:none;">
<?php while(have_posts()):the_post(); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php
wp_reset_query();

The parameters are placed in $query_post in the form of an array. The key parameters are 'post__in' =>get_option('sticky_posts') and 'caller_get_posts' => ; 0.

'post__in' => get_option('sticky_posts') determines that the LOOP calls the list of sticky articles. The function of 'caller_get_posts' is to exclude non-specified articles, that is, no other articles will be displayed except the top articles. (Without adding, if the pinned article entry is less than the value specified by 'posts_per_page', the latest article will be used to replace it.)

The above is the detailed content of How to get the list of pinned articles in WordPress. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn