admin管理员组

文章数量:1316851

I am trying to display a sub-menu with the sub-pages of a parent, if you are on the parent page, and the siblings if you are on one of the child pages, but the problem is that if the page have a featured image is recognized az a parent. this is the code I use:

    // check if page have children
        $children = get_children( get_the_ID() );
        if ( ! empty($children)) {
            echo '<ul class="submenu">';
            wp_list_pages(array(
                'child_of' => $post->ID,
                'title_li' => 0,
                'item_spacing' => 'discard'
                // 'exclude' => $post->ID
            ));
            echo '</ul>';
        } else if ($post->post_parent){
            echo '<ul class="submenu">';
    // get a back link to the parent
            echo '<li class = "bold parentbut"><a href ="' . get_permalink($post->post_parent) . '">' . get_the_title($post->post_parent) . '</a></li>';
            wp_list_pages(array(
                'child_of' => $post->post_parent,
                'title_li' => 0,
                'item_spacing' => 'discard'
                // 'exclude' => $post->ID
            ));
            echo '</ul>';
        } else {
            echo '<div class="dunga-albastra"></div>';
        }

the problem is that I do not get to the second else-if, if the child page have an atachment.

I am trying to display a sub-menu with the sub-pages of a parent, if you are on the parent page, and the siblings if you are on one of the child pages, but the problem is that if the page have a featured image is recognized az a parent. this is the code I use:

    // check if page have children
        $children = get_children( get_the_ID() );
        if ( ! empty($children)) {
            echo '<ul class="submenu">';
            wp_list_pages(array(
                'child_of' => $post->ID,
                'title_li' => 0,
                'item_spacing' => 'discard'
                // 'exclude' => $post->ID
            ));
            echo '</ul>';
        } else if ($post->post_parent){
            echo '<ul class="submenu">';
    // get a back link to the parent
            echo '<li class = "bold parentbut"><a href ="' . get_permalink($post->post_parent) . '">' . get_the_title($post->post_parent) . '</a></li>';
            wp_list_pages(array(
                'child_of' => $post->post_parent,
                'title_li' => 0,
                'item_spacing' => 'discard'
                // 'exclude' => $post->ID
            ));
            echo '</ul>';
        } else {
            echo '<div class="dunga-albastra"></div>';
        }

the problem is that I do not get to the second else-if, if the child page have an atachment.

Share Improve this question asked Nov 6, 2020 at 16:46 Botond VajnaBotond Vajna 4714 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You should specify the post_type for the get_children() function, which can be one of attachment, page, revision or any and considered to be any by default:

$children = get_children( array( 'post_parent' => get_the_ID(), 'post_type' => 'page' ) );

本文标签: phpcheck if page have childrenbut not attachment