Current child post or page parent title and link, Dynamic Data Tag

Current child post or page parent title and link, Dynamic Data Tag

paste this wherever you want to display the current post/page parent’s title and link on a child page or post.

It can be used in or outside the loop.

add_filter( 'bricks/dynamic_tags_list', 'register_parent_link_tag' );
function register_parent_link_tag( $tags ) {
    $tags[] = [
        'name'  => '',
        'label' => 'Parent Title and Link',
        'group' => 'Custom Tags',
    ];
    return $tags;
}
add_filter( 'bricks/dynamic_data/render_tag', 'render_parent_link_tag', 10, 3 );
function render_parent_link_tag( $tag, $post, $context = 'text' ) {
    if ( $tag !== 'parent_link' ) {
        return $tag;
    }
    if ( $post->post_parent ) {
        $parent_post = get_post( $post->post_parent );
        if ( $parent_post ) {
            $parent_title = get_the_title( $parent_post );
            $parent_link  = get_permalink( $parent_post );
            return '<a href="' . esc_url( $parent_link ) . '">' . esc_html( $parent_title ) . '</a>';
        }
    }
    return 'No Parent Found';
}
add_filter( 'bricks/dynamic_data/render_content', 'render_parent_link_tag_in_content', 10, 3 );
function render_parent_link_tag_in_content( $content, $post, $context = 'text' ) {
    if ( strpos( $content, '' ) !== false ) {
        $parent_link = render_parent_link_tag( 'parent_link', $post, $context );
        $content = str_replace( '', $parent_link, $content );
    }
    return $content;
}