• Resolved azlivin008

    (@azlivin008)


    Hello, can you tell me how to insert the TITLE of a parent page without having to enter the page ID manually?

    I was able to use key=’post_parent’ to return the page ID of the parent page, but how do I get that value into the post_id to dynamically return the title of the parent page? I have many pages, with many child pages, so I cannot enter each page ID separately.

    [dynamictext inputname “CF7_get_post_var key=’post_title’ post_id=’123‘”]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    You can certainly do that with a custom shortcode! Just plop this snippet into your active theme’s functions.php file:

    /**
     * Get Title of Parent Page of Current Page
     *
     * @return string The title of the current page's parent, or empty string
     */
    function get_current_post_parent_title()
    {
        global $post;
        if (isset($post)) {
            $post_id = $post->ID; // If the global $post object is set, get its ID
        } else {
            $post_id = get_the_ID(); // Otherwise get it from "the loop"
        }
        $parent_id = get_post_field('post_parent', $post_id); // Get parent ID of post
        // Validate that the parent ID is a number
        if ($parent_id && is_numeric($parent_id) && (int)$parent_id > 0) {
            return esc_attr(get_the_title($parent_id)); // Return the title
        }
        return ''; // Return empty string otherwise
    }
    add_shortcode('get_current_post_parent_title', 'get_current_post_parent_title', 10, 0);

    And then update your form tag to use it instead ??

    [dynamictext inputname "get_current_post_parent_title"]
    Thread Starter azlivin008

    (@azlivin008)

    Thank you – YOU ROCK!!!! ??

    • This reply was modified 1 year, 7 months ago by azlivin008.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Shortcode for title of parent page?’ is closed to new replies.