首页 > 解决方案 > Wordpress 子页面包含精选图片

问题描述

所以这是我的代码,它显示了子页面的所有标题,但我也想要子页面的所有特征图像。我不知道如何做到这一点。我有包含 page.php 和我的 function.php 脚本。我知道很多人都已经问过这种类型的问题,但我似乎无法弄清楚。

页面.php

 <?php 

    get_header();

    if(have_posts()) :
        while (have_posts()) : the_post(); ?>


    <article class="post page">

        <?php 

        if ( has_children() OR $post->post_parent > 0 ) { ?>

        <nav class="site-nav children-links clearfix">

    <span class="parent-link"><a href="<?php echo get_the_permalink(get_top_anestor_id); ?>"><?php echo get_the_title(get_top_ancestor_id()); ?> </a> </span>
        <ul>
            <?php

            $args = array( 
                'child_of' => get_top_ancestor_id(),
                'title_li' => ''
                );

            ?>
            <?php wp_list_pages($args); ?>

        </ul>
    </nav>
    <?php } ?>

    </article>  
        <?php endwhile;

        else :
        echo '<p> No content found</p>';

        endif;

    get_footer();

    ?>

函数.php

function get_top_ancestor_id() {

    global $post;

    if ($post->post_parent) {
        $ancestors = array_reverse(get_post_ancestors($post->ID));
        return $ancestors[0];
    }

    return $post->ID;

}

// Does page have children?

function has_children() {

        global $post;

        $pages = get_pages('child_of=' . $post->ID);
        return count($pages);

}

标签: phpwordpress

解决方案


所以我终于找到了同样挣扎的人的答案:

<?php 
        $args = array( 
            'child_of' => get_top_ancestor_id(),
            'title_li' => ''
            );

        ?>
           <?php $our_pages = get_pages($args); ?>
           <?php if (!empty($our_pages)): ?>
            <?php foreach ($our_pages as $key => $page_item): ?>
                <div class="col-*-* product-object">
                <a class="product-article" href="<?php echo esc_url(get_permalink($page_item->ID)); ?>">
                <div class="product-image" style="background: url(<?php echo get_the_post_thumbnail_url($page_item->ID,'product-image');?>); ">
                <h2 class="product-h2"><?php echo $page_item->post_title ; ?></h2>  
                </div>
                </a>
                </div>
            <?php endforeach ?>
           <?php endif ?>
</article>  
</div>
<?php 

推荐阅读