Function and usage of is_sticky() in wordpress

Question

Answers ( 1 )

    0
    2024-01-08T23:45:59+00:00

    The is_sticky() function in WordPress is a conditional tag used to determine if a specific post is marked as "sticky." Sticky posts are a feature in WordPress that allows you to highlight or prioritize certain posts by keeping them at the top of your blog's front page.

    Here's a detailed explanation of the is_sticky() function:

    Function Signature

    is_sticky( int $post_id ): bool
    

    Purpose

    • Determines whether a post is sticky.
    • Sticky posts are intended to be featured or highlighted and remain at the top of the post list, usually on the blog's front page.

    Description

    • The function checks if a given post is marked as sticky.
    • Sticky posts are typically used to draw attention to important content.
    • If the $post_id parameter is not provided, the function will use the ID of the current post in The Loop.

    Parameters

    • $post_id (int, optional): The ID of the post you want to check. If not specified, it defaults to the ID of the global $post object.

    Return Value

    • Returns a boolean (bool):
      • true if the post is sticky.
      • false if the post is not sticky.

    Usage Example

    Here's a simple example of how you might use is_sticky() within a WordPress theme:

    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post();
    
            if ( is_sticky() ) {
                // This is a sticky post
                echo '<h2>Featured Post: ' . get_the_title() . '</h2>';
            } else {
                // This is a regular post
                echo '<h2>' . get_the_title() . '</h2>';
            }
        }
    }
    

    In this example, if the current post in The Loop is sticky, it outputs a heading with "Featured Post" before the post title. For regular posts, only the title is displayed.

    Additional Resources

    • For more information on this and similar theme functions, refer to the Conditional Tags article in the WordPress Theme Developer Handbook. This resource provides in-depth guidance on how to effectively use conditional tags to customize your WordPress theme.

Leave an answer