将所有父页面显示为特色图像

时间:2013-07-15 作者:Johnathan Tebeau

下面是我当前显示当前post\\u父级的所有“子”页面的代码(了解如何显示自定义父级页面也会很有帮助…)

<?php
$args = array(
    \'post_parent\' => $post->ID,
    \'post_type\' => \'page\',
    \'orderby\' => \'menu_order\',
    \'order\'=>\'ASC\',
);
$my_query = new WP_Query($args);
if($my_query->have_posts()) :
    while($my_query->have_posts()) : 
        $my_query->the_post();
      // your stuff goes in this bit.
     if ( has_post_thumbnail() ) : 
         $imgdata = wp_get_attachment_image_src( get_post_thumbnail_id(), \'large\' );
         $imgwidth = $imgdata[1]; // thumbnail\'s width
         $imgheight = $imgdata[2]; // thumbnail\'s height
  ?>
   <div class="child element isotope-item" style="width:<?php echo $imgwidth;?>;height:<?php echo $imgheight;?>;">
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
      <?php echo the_post_thumbnail(large); ?>
      <span class="child_title"><?php echo the_title_attribute();?></span>
    </a>
  </div>
  <?php
    endif;
    // end your stuff.
    endwhile;
else :
    // Do the no posts found message 
endif;
?>
</div>

1 个回复
最合适的回答,由SO网友:Pat J 整理而成

要获取网站的所有“根”页面,请执行以下操作:

$args = array(
    \'post_type\' => \'page\',
    \'child_of\' => 0,
);
$pages = get_pages( $args );
foreach( $pages as $page ) {
    if( has_post_thumbnail( $page->ID ) {
        $imgdata = wp_get_attachment_image_src( get_post_thumbnail_id(), \'large\' );
        $imgwidth = $imgdata[1]; // thumbnail\'s width
        $imgheight = $imgdata[2]; // thumbnail\'s height
    }
}
// handle the image as in your posted code
要获取自定义帖子类型,您应该能够替换\'post_type\' => \'page\' 使用您的CPT名称(例如,\'post_type\' => \'my_post_type\').

参考get_pages()

结束

相关推荐