Function and usage of get_extended() in wordpress

Question

Answers ( 1 )

    0
    2024-01-06T22:29:48+00:00

    The get_extended() function in WordPress is a useful tool for handling posts with the <!--more--> tag. Here's a detailed explanation of its functionality and usage:

    Function:

    • Name: get_extended()
    • Purpose: Retrieves extended entry information from a post content in WordPress, particularly focusing on the content divided by the <!--more--> tag.

    Description:

    • The <!--more--> tag is used in WordPress posts to specify a "break" point. Anything before this tag is considered part of the main content, and anything after is considered extended content.
    • It's important to note that the tag should be formatted without any space between the second dash and the word ‘more’. However, text or spaces after the word ‘more’ are permissible but not referenced by the function.
    • This function is typically used to separate a post into a teaser and a full content section, enabling a "Read More" feature on blog pages.

    Parameters:

    • $post (string, required): This parameter accepts the post content where the <!--more--> tag is located.

    Return:

    The function returns an associative array with three keys:

    1. main: This key contains the content of the post that appears before the <!--more--> tag.
    2. extended: This key holds the content that is located after the <!--more--> tag.
    3. more_text: This key stores the custom "Read More" text if provided. If no custom text is set, it returns an empty string.

    Sample Usage:

    Here's a basic example of how get_extended() might be used in a WordPress theme or plugin:

    <?php
    // Assuming $post_content contains the content of a post
    $post_content = get_the_content();
    
    // Use get_extended() to split the content at the <!--more--> tag
    $extended_content = get_extended($post_content);
    
    // Access the parts of the content
    $main_content = $extended_content['main']; // Content before <!--more-->
    $additional_content = $extended_content['extended']; // Content after <!--more-->
    $read_more_text = $extended_content['more_text']; // Custom "Read More" text
    
    // Output the content
    echo '<p>' . $main_content . '</p>';
    if ( !empty($additional_content) ) {
        echo '<a href="#">' . (empty($read_more_text) ? 'Read More' : $read_more_text) . '</a>';
    }
    ?>
    

    In this example, the post content is first retrieved and then passed to the get_extended() function. The function returns an array that separates the main content from the extended content and any custom "Read More" text. The content is then outputted accordingly, with a "Read More" link if extended content exists.

Leave an answer