Determine sticky articles
is_sticky() function is used to determine whether an article is a sticky article.
Usage
is_sticky( $post_id );
Parameters
$post_id
(integer) (optional) The article ID to be judged, the default is the current article in the loop.
Default value: 0 (current article in loop)
Return value
(Boolean) Whether the article is a pinned article.
Example
if( is_sticky() ) echo //'当前文章是置顶文章'; if( is_sticky( 68 ) ) echo //'ID 为 68 的文章是置顶文章';
Others
This function is located at: wp-includes/post.php
Function to add and remove pinned articles
WordPress supports the pinned article function by default. You can put important or exciting articles in Keep it at the top of the background so users can see it first.
During development, you may need to add and remove pinned articles through code. The principle of WordPress sticky articles is to store the ID of the sticky article in the options table, and you can control the sticky article by modifying the sticky_posts field.
However, WordPress provides two functions that make it easier to add and remove sticky posts. You can modify the sticky_posts field by calling the function directly. The
stick_post()
stick_post() function is used to pin an article to the top. Example:
stick_post( 68 );//置顶 ID 为 68 的文章 stick_post( get_the_ID() );//置顶循环中的当前文章
unstick_post()
unstick_post() is the opposite of the stick_post() function and is used to unpin a pinned article to the top. :
unstick_post( 425 );//取消置顶 ID 为 425 的文章 unstick_post( get_the_ID() );//取消置顶循环中的当前文章
The above has introduced a summary of relevant PHP functions for determining, adding and deleting WordPress pinned articles, including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.