#record#wordpress calls pinned articles and controls the number

888u

Last update at :2024-02-16,Edit by888u

Since my template is using XIU, a special module has been set up for the pinned article, which has become the four tofu block styles at the top of the homepage. For some reasons, I want the pinned article to be displayed in the article list. , so I did some research on it myself, and finally added some code to index.php to implement this function.

First of all, I need to understand how wordpress calls pinned articles, so I searched for the following content.

$sticky, 'caller_get_posts' => 1 ) ); if (have_posts()) :while (have_posts()) : the_post(); ?>
  • The code is explained as follows:

    • rsort( $sticky ); Reverse sort the array of sticky articles, that is, the largest ID first;
    • $sticky = array_slice( $sticky, 0, 5); Controls the number of sticky articles to be displayed and outputs the number of sticky articles. Please modify 5 and leave 0 unchanged. If you need to output all sticky articles, you can comment out this sentence;
    • ‘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.
    • This is the final display style, of course it can be customized

    Extract what you need according to the above content

    $sticky = get_option('sticky_posts'); rsort( $sticky ); $sticky = array_slice( $sticky, 0, 1); query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );

    This is what I need. It only displays a pinned article, and finally adds the style that needs to be displayed, and puts it in the article list in index.php. Since this code coexists with the code that calls other articles, Therefore, this pinned article will also be displayed on other pages of the article list, so a page number judgment needs to be added. The code is as follows

    if( $paged && $paged > 1 ){}else{ The above calling code }

    Finally, this function has been implemented. There are other good methods that are being further explored. If there are experts, please reply, thank you!

    Recommended site searches: US host rental, .net space, Guangzhou website registration, Hong Kong host, free server website, buy server high defense, free asp.net space, how much does it cost to rent a server for one year, expired domain name, domain name Register,

    #record#wordpress calls pinned articles and controls the number

    All copyrights belong to 888u unless special state
    取消
    微信二维码
    微信二维码
    支付宝二维码