【问题标题】:WordPress - Displaying Subpages (Title and Thumbnail) ON SubpagesWordPress - 在子页面上显示子页面(标题和缩略图)
【发布时间】:2011-08-09 09:02:28
【问题描述】:

当在父页面上时,我正在使用以下代码在侧边栏中显示子页面列表和这些子页面的精选帖子图像。

<?php $args = array(
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'post_parent' => $post->ID,
        'post_type' => 'page',
        'post_status' => 'publish'
        ); 
        $postslist = get_posts($args);
        foreach ($postslist as $post) : setup_postdata($post); 
    ?>
    <div class="top10">
    <a href="<?php the_permalink();?>">
    <?php the_post_thumbnail('large'); ?>
    </a>
    </div>
    <?php endforeach; ?>
    <?php wp_reset_query(); ?>

但是,当在其中一个子页面上时,我还需要在侧边栏中显示相同的列表。目前,在子页面上使用相同的代码而不进行调整不会显示任何内容。

我尝试将“'post_parent' => $post->ID”行更改为“'post_parent' => $post->ID.”echo=0””,它显示了一些子页面,但不是全部,所以我显然搞砸了。

有人可以帮我修改代码以在父页面和父页面上工作吗?

谢谢 扎克

【问题讨论】:

  • 你不能用wordpress菜单吗?
  • 一个 WordPress 菜单通常只会显示页面标题,而演示的方法允许我拉出缩略图、标题,如果我想要的话,甚至是摘录!
  • 那么您遇到的问题是自动获取父页面ID?我说的对吗?
  • 我相信是的。看看74.54.17.66/~roiminis/top-10 - 这是父页面,你可以看到它列出了左侧的所有子页面,根据上面的代码。现在单击其中一个子页面,您现在可以看到左侧没有显示任何内容,并且代码相同。我需要修改代码以在子页面以及父页面的左侧显示项目
  • 我想我明白你的问题了...

标签: wordpress


【解决方案1】:

使用此函数为您的菜单生成 ID。它将确定页面是否有父页面,并使用该 ID,否则返回当前页面 ID。

function get_menu_id(){
    if ($post->post_parent)  {
        $parent = get_post_ancestors($post->ID);
        return $parent[0];
    } else {
        return $post->ID;
    }
}

完整代码

<?php 


function get_menu_id(){ //this function would be better off in your functions.php file
        if ($post->post_parent)  {
            $parent = get_post_ancestors($post->ID);
            return $parent[0];
        } else {
            return $post->ID;
        }
    }
$args = array(
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'post_parent' => get_menu_id(),
        'post_type' => 'page',
        'post_status' => 'publish'
        ); 
        $postslist = get_posts($args);
        foreach ($postslist as $post) : setup_postdata($post); 
    ?>
    <div class="top10">
    <a href="<?php the_permalink();?>">
    <?php the_post_thumbnail('large'); ?>
    </a>
    </div>
    <?php endforeach; ?>
    <?php wp_reset_query(); ?>

【讨论】:

    猜你喜欢
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多