使用特色缩略图显示子项、父项和祖先的缩略图

时间:2014-03-30 作者:HannesH

我试图实现主页显示其所有子页面的特征图像,而一些子页面则显示其子页面的特征图像。这是我到目前为止的代码!

<?php
        global $post;

        if ( is_page() && $post->post_parent ) : 

        elseif ( is_page() && $post->post_parent && $post->child_page ) : ?>

        <?php else : ?>
            <?
            $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, \'index-thumb\');
                    if($thumbnail == "") continue; // Skip pages without a thumbnail
            ?>
                    <div class="child-thumb">
                    <?php
                        echo \'<div class="re-featured-image">\' . get_the_post_thumbnail($pageChild->ID, \'index-thumb\', array(\'class\' => \'img-responsive\', \'id\' => \'img-center\')) . \'</div>\';
                        echo \'<div class="re-featured-image-title">\' . get_post(get_post_thumbnail_id($pageChild->ID))->post_title . \'</div>\';
                        echo \'<div class="re-featured-image-caption">\' . get_post(get_post_thumbnail_id($pageChild->ID))->post_excerpt . \'</div>\';
                        echo \'<div class="re-featured-image-description">\' . get_post(get_post_thumbnail_id($pageChild->ID))->post_content . \'</div>\';
                    ?>                      
                    </div>
            <?
                endforeach;
            endif;
            ?>
        <?php endif; ?>

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

我认为您可以使用另一种方法:编写一个自定义sql查询,获取子页面缩略图附件的ID,如果找到,则调用wp_get_attachment_image_src 在此ID上检索URL:

function my_get_thumbnails( $post = NULL, $which = \'both\' ) {
  // first we get the post, if no post is passed we use global post
  if ( empty( $post ) ) global $post;
  if ( is_numeric($post) ) $post = get_post( $post );
  if ( ! $post instanceof WP_Post ) return;
  $children = FALSE;
  $parent = FALSE;
  // if we want children posts thumbnail...
  if ( $which !== \'parent\' ) {
    // run a query to get attachment id set as thumbnail in children posts
    global $wpdb;
    $q = "SELECT {$wpdb->postmeta}.meta_value FROM {$wpdb->postmeta}
    JOIN {$wpdb->posts} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id
    WHERE {$wpdb->postmeta}.meta_key = \'_thumbnail_id\'
    AND {$wpdb->posts}.post_parent = %d";
    $thumb_ids = $wpdb->get_col( $wpdb->prepare( $q, $post->ID ) );
    // if some results, call wp_get_attachment_image_src on all ids to get image urls
    if ( ! empty( $thumb_ids ) ) {
      $children = array_map( function($tid) {
         $img = wp_get_attachment_image_src( $tid, \'large\' ); // <-- SET SIZE HERE
         return $img[0];
      }, $thumb_ids );
    }
  }
  // if we want parent post thumbnail...
  if ( $which !== \'children\' && $post->post_parent ) {
    $tid = get_post_thumbnail_id( $post->post_parent );
    if ( $tid ) {
      // get the url of parent post thumbnail
      $img = wp_get_attachment_image_src( $tid, \'large\' );  // <-- SET SIZE HERE
      $parent = $img[0];
    }
  }
  // if we want only children posts thumbnail return them
  if ( $which === \'children\' ) return $children;
  // if we want only parent post thumbnail return it
  if ( $which === \'parent\' ) return $parent;
  // if we want bot return an array with both
  return array( \'children\' => $children, \'parent\' => $parent );
}
在中输入上一个函数后functions.php, 在模板中(可能是page.php), 这样使用:

<?php
global $post;
if ( is_page() ) {

  $thumbnails = my_get_thumbnails();

  if ( ! empty( $thumbnails[\'parent\'] ) ) {
    $format = \'<div class="parent-thumb"><img src="%s" alt="" /></div>\';
    printf( $format, $thumbnails[\'parent\'] );
  }

  if ( is_array( $thumbnails[\'children\'] ) && ! empty( $thumbnails[\'children\'] ) ) {
    $open = \'<div class="child-thumb"><img src="\';
    $close = \'" alt="" /></div>\';
    echo $open . implode( $close . $open, $thumbnails[\'children\'] ) . $close;
  }

}
?>
my_get_thumbnails 函数也可用于

获取非当前帖子的父/子帖子缩略图,只需将帖子ID或帖子对象作为第一个参数传递

结束

相关推荐

show all the posts thumbnails

我想通过每个帖子的特色图片显示所有帖子。有什么捷径吗?或者你能给我一些如何实现这一点的建议吗?谢谢