Function and usage of get_the_content() in wordpress

Question

Answers ( 1 )

    0
    2024-01-08T14:25:39+00:00

    The get_the_content() function in WordPress is used to retrieve the content of a post. Here's a detailed explanation of the function and its parameters:

    Function Signature

    get_the_content( string $more_link_text = null, bool $strip_teaser = false, WP_Post|object|int $post = null ): string
    

    Function Description

    • Purpose: Retrieves the content of a post.
    • Return Type: string – The content of the post.

    Parameters

    1. $more_link_text (string, optional)

      • Purpose: Specifies the text to be shown for the 'read more' link.
      • Default Value: null. If null, WordPress uses the default more link text.
    2. $strip_teaser (bool, optional)

      • Purpose: Determines whether to strip the teaser content before the 'more' text.
      • Default Value: false. When false, the teaser (the content before the 'more' tag) is included in the output.
    3. $post (WP_Post | object | int, optional)

      • Purpose: Specifies the post to retrieve the content from.
      • Default Value: null. If null, the current post in the loop is used.
      • Types:
        • WP_Post: A WP_Post object.
        • object: A standard PHP object representing a post.
        • int: The ID of a post.

    Usage Example

    Here's a sample usage of get_the_content():

    // Basic usage without any parameters. Retrieves content for the current post.
    $content = get_the_content();
    
    // Usage with 'more' text link.
    $more_link_text = 'Read More...';
    $content = get_the_content($more_link_text);
    
    // Usage with 'more' text link and stripping teaser.
    $strip_teaser = true;
    $content = get_the_content($more_link_text, $strip_teaser);
    
    // Usage with a specific post ID.
    $post_id = 123; // Assuming 123 is a valid post ID.
    $content = get_the_content(null, false, $post_id);
    

    Notes

    • The function must be used within The Loop or a WordPress query to work correctly.
    • If the more tag is used in the post content, this function will handle it according to the provided parameters.
    • This function does not apply the_content filters. For filtered content, use the_content() instead, which echoes the content.

Leave an answer