Function and usage of get_the_permalink() in wordpress


Answers ( 1 )


    The get_the_permalink() function in WordPress is used to retrieve the full permalink URL for a post. Here's a detailed explanation of this function:

    Function Signature:

    get_the_permalink( int|WP_Post $post = null, bool $leavename = false ): string|false


    • Purpose: This function is primarily used to obtain the full URL of a specific post in a WordPress site.
    • Alias: It is an alias for the get_permalink() function. This means that get_the_permalink() and get_permalink() essentially perform the same function.


    1. $post (int|WP_Post) [Optional]

      • Description: The post ID or post object for which the permalink is required.
      • Type: It can be either an integer (representing the post ID) or a WP_Post object.
      • Default: If not provided, the function uses the global $post object, which represents the current post in the WordPress loop.
    2. $leavename (bool) [Optional]

      • Description: Determines whether to keep the post name or page name in the URL.
      • Type: Boolean.
      • Default: False. If set to true, the permalink structure will be returned with %postname% placeholder.


    • Type: string|false
    • Description: The function returns the permalink URL as a string. If the post does not exist, it returns false.

    Usage Example:

    // Example 1: Get permalink of the current post
    $permalink = get_the_permalink();
    echo 'Permalink: ' . $permalink;
    // Example 2: Get permalink of a specific post by ID
    $post_id = 123; // Replace with a specific post ID
    $permalink = get_the_permalink($post_id);
    echo 'Permalink for post ID ' . $post_id . ': ' . $permalink;
    // Example 3: Get permalink with the post name placeholder
    $permalink = get_the_permalink(null, true);
    echo 'Permalink with post name: ' . $permalink;

    In these examples:

    • Example 1 retrieves the permalink of the current post in the loop.
    • Example 2 demonstrates obtaining the permalink of a specific post using its ID.
    • Example 3 shows how to get the permalink structure with the post name placeholder.

    Additional Notes:

    • This function is commonly used in WordPress themes and plugins to dynamically generate links to posts.
    • It respects the permalink settings defined in the WordPress dashboard.
    • The function is part of the WordPress Template Tags, which are used for theme development.

Leave an answer