将子页面作为数组抓取
// Get an array of child pages.
$child_pages = get_posts( array(
\'posts_per_page\' => 15,
\'post_type\' => page,
\'post_parent\' => $post->ID,
) );
然后检查
$child_pages
看看里面有没有什么。
if ( $child_pages )
show_child_pages( $child_pages );
else
get_sidebar();
然后编写一个用户函数来显示子页面。
/**
* Show page subpages.
*
* @param array $child_pages An array of post objects.
*/
function show_child_pages( array $child_pages ) {
$sub_page_format = \'
<div class="subpages">
<h2><a href="%s">%s</a></h2>
<div class="subpages-img" style="background: url(%s) no-repeat center !important; height: 400px; width: 150px;">
%s
</div>
</div>
\';
foreach ( $child_pages as $post ) {
$src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), array( 5600, 1000 ), false, \'\' );
printf(
$sub_page_format,
get_permalink( $post->ID ),
get_the_title( $post->ID ),
$src[0],
get_the_post_thumbnail( $post->ID )
);
}
}
如果您喜欢使用
Template Tags 就像你在做的那样,用这个:
/**
* Show page subpages.
*
* @param array $child_pages An array of post objects.
*/
function show_child_pages( array $child_pages ) {
global $post;
foreach ( $child_pages as $post ) {
setup_postdata( $post );
$src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), array( 5600,1000 ), false, \'\' );
$image_thumb = get_post_meta( $post->ID, \'image-thumb\', true );
?>
<div class="subpages">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="subpages-img" style="background: url(<?php echo $src[0]; ?> ) no-repeat center !important; height: 400px; width: 150px;">
<?php the_post_thumbnail(); ?>
</div>
</div>
<?php
}
wp_reset_postdata();
}