孩子的孩子作为链接&缩略图

时间:2014-01-17 06:22:47

标签: php wordpress hyperlink thumbnails parent-child

孩子的孩子?

所以这是我用来将子页面作为链接及其缩略图(特色图片)的代码:

$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT');

    if ( $child_pages ) :
        foreach ( $child_pages as $pageChild ) :
            setup_postdata( $pageChild );
            $thumbnail = get_the_post_thumbnail($pageChild->ID, 'thumbnail', true);
            if($thumbnail == "") continue; // Skip pages without a thumbnail
            $thumbShort = explode('src="', $thumbnail);
            $thumbnailShortStripped = substr($thumbShort[1], 0, strpos( $thumbShort[1], '"'));
    ?>
            <li><a href="<?= get_permalink($pageChild->ID) ?>" rel="bookmark"><img src="<?= $thumbnailShortStripped; ?>" class="thumbnail" alt="<?= $pageChild->post_title ?>" 0="" /></a></li>
    <?
    endforeach;
    endif;
    ?>

但我怎么能得到孩子的孩子?

所以我只想要“孙子”,我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

谢谢大家,我想出了自己! :)

 <?
$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT');
$i = 0;
if ( $child_pages ) :
foreach ( $child_pages as $pageChilds ) :
    $gchild_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$pageChilds->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT');
    if($gchild_pages) :
    foreach ($gchild_pages as $pageChild):
    setup_postdata( $pageChild );
    $thumbnail = get_the_post_thumbnail($pageChild->ID, 'thumbnail', true);
    if($thumbnail == "") continue; // Skip pages without a thumbnail

?>
    <a href="<?= get_permalink($pageChild->ID) ?>" rel="bookmark">
       <div class="child-thumb">
      <li> <?= $thumbnail ?></li>
       <div class="child-text"><?= $pageChild->post_title ?></div>
       </div>
    </a>
<?

    endforeach;
    endif;
endforeach;
endif;
?>

全部谢谢!

答案 1 :(得分:0)

你可以使用以下功能

$args = array(   
   'child_of' => $parentpostID,
   'post_status' => 'publish',
 );

$data = get_pages( $args );