0

I have a page that has a true/false custom field.

What I want to do is output a link on another page that shares a parent with the page with the custom field.

Below is code from Wordpress Reference that allows me to target the page that shares a parent.

<?php
if ( $post->post_parent ) {
$children = wp_list_pages( array(
    'title_li' => '',
    'child_of' => $post->post_parent,
    'echo'     => 0
) );
} else {
$children = wp_list_pages( array(
    'title_li' => '',
    'child_of' => $post->ID,
    'echo'     => 0
) );
}

if ( $children ) : ?>

WANT TO INSERT LINK HERE

<?php endif; ?>

This is the code I am using to insert my link via an Advanced Custom Field checkbox.

<?php if ( get_field( 'show_video_menu' ) ): ?>

<li><a id="artists-video" href="/<?php global $post;
if($post->post_parent) { $post_data = get_post($post->post_parent);
echo $post_data->post_name; }?>/video">Video</a></li>

<?php endif; // end of if field_name logic ?>

I was close, but I cannot seem to combine the codes in a way the shows the link only on the page that shares the parent. The link was occurring on all pages using the same Page Template.

This is the code I am currently using

<?php
if ( $post->post_parent ) {
$children = wp_list_pages( array(
    'title_li' => '',
    'child_of' => $post->post_parent,
    'echo'     => 0
) );
} 

if ( $children || get_field( 'show_video_menu' ) ): ?>

<li><a id="artists-video" href="/<?php global $post;
if($post->post_parent) { $post_data = get_post($post->post_parent);
echo $post_data->post_name; }?>/video">Video</a></li>

<?php endif; ?>

I have created a Video Page with the parent Margo Wolowiec with the Custom Field however it was showing on other unrelated pages as well.

1
0

I'm assuming there are only two children in such cases.

<?php
$child_id = $post->ID;
if ( $post->post_parent ) {
    $args = array(
                    'post_parent' => $post->post_parent,
                    'post_type'   => 'page', 
                    'numberposts' => -1,
                    'post_status' => 'publish' 
                  );
    $children = get_children($args);
    if(count($children) == 2) {
        $child_id = $children[0]->ID == $post->ID ? $children[1]->ID : $children[0]->ID;
    }
} 

if ( get_field( 'show_video_menu' , $child_id ) ): ?>

<li><a id="artists-video" href="/<?php global $post;
if($post->post_parent) { $post_data = get_post($post->post_parent);
echo $post_data->post_name; }?>/video">Video</a></li>

<?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.