wordpress会自动理解$ post-> id还是我需要自己设置它

时间:2018-06-11 13:12:32

标签: php wordpress menu themes

我已经设置了一个短代码,可以将所有子页面拉到它所放置的页面上,但它会将网站中的所有页面拉到菜单中

function my_function() {

         $args = array(
    'post_type'      => 'page',
    'posts_per_page' => -1,
    'post_parent'    => $post->ID,
    'order'          => 'ASC',
    'orderby'        => 'menu_order'
 );


        $children = get_pages($args); 

        $result = "<div class='row inner'>";
        foreach ( $children as $child )
        {
            $child_id = $child->ID;
            $url  = get_permalink( $child_id );
            $thumb = get_the_post_thumbnail($child_id, array(240, 240));
            $title= $child->post_title;

            $link = "<a href='$url'>$thumb<div class='product-title'>$title</div></a>";

            $result .= "<div class='col-md-3 product-item'>$link</div>";
        }

        $result .= "</div>";

        return $result;
}

add_shortcode('show_child_pages', 'my_function');

在post_parent我有$ post-&gt; id但我不确定是否必须将此设置为我尝试的get_Id函数的变量,它只是搞砸了菜单更多我觉得我做错了虽然。 非常感谢任何帮助

这已被标记为重复,但我的问题没有得到回答

甚至在添加全球$ post后;在函数的顶部,它仍然会吐出所有菜单项

1 个答案:

答案 0 :(得分:0)

$ post还没有任何值,它在一个简单的数组($ args)里面,只有php,$ post在你之前设置之前不会有任何价值。

Function Reference/$post

正如您在上面的文档中看到的那样,$ post ref在wordpress循环中工作