If the current post/page has a parent, it returns the parent’s ID
If there’s no parent, it returns the current post’s ID
{current_or_parent_post_id}
add_filter( 'bricks/dynamic_tags_list', 'register_current_or_parent_post_id_tag' );
function register_current_or_parent_post_id_tag( $tags ) {
$tags[] = [
'name' => 'current_or_parent_post_id',
'label' => 'Current or Parent Post ID',
'group' => 'Custom Tags',
];
return $tags;
}
add_filter( 'bricks/dynamic_data/render_tag', 'render_current_or_parent_post_id_tag', 10, 3 );
function render_current_or_parent_post_id_tag( $tag, $post, $context = 'text' ) {
if ( $tag !== 'current_or_parent_post_id' ) {
return $tag;
}
// If post has a parent, return parent ID, otherwise return current post ID
if ( $post->post_parent ) {
return $post->post_parent;
}
return $post->ID;
}
add_filter( 'bricks/dynamic_data/render_content', 'render_current_or_parent_post_id_tag_in_content', 10, 3 );
function render_current_or_parent_post_id_tag_in_content( $content, $post, $context = 'text' ) {
if ( strpos( $content, '{current_or_parent_post_id}' ) !== false ) {
$post_id = render_current_or_parent_post_id_tag( 'current_or_parent_post_id', $post, $context );
$content = str_replace( '{current_or_parent_post_id}', $post_id, $content );
}
return $content;
}