最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - check if page have children, but not attachment

programmeradmin1浏览0评论

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' ) );
发布评论

评论列表(0)

  1. 暂无评论