将标题和描述添加到子页面的特色图像

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

嘿!我有一个子页面来显示它的子页面特征图像。我想在缩略图下面添加标题和描述。今天我的代码如下>

在函数中。php

function my_geturls($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_src( $tid ); // <-- 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 ( $img ) {
  // get the url of parent post thumbnail
  $urls = wp_get_attachment_src( $tid );  // <-- 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 );
}
这是我在页面中的代码。php文件。

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

      $thumbnails = my_get_thumbnails();

      if ( ! empty( $thumbnails[\'parent\'] ) ) {
        $format = \'<a href="<?php echo get_page_link( $child_page->ID ); ?><div class="parent-thumb"><img src="%s" class="img-responsive" id="img-center" max-height="255px" width="auto" alt="" /></div></a>\';
        printf( $format, $thumbnails[\'parent\'] );
      }

      if ( is_array( $thumbnails[\'children\'] ) && ! empty( $thumbnails[\'children\'] ) ) {
        $open = \'<a href="<?php echo get_page_link( $child_page->ID ); ?>"><div max-height="255px" width="auto" class="child-thumb"><img class="img-responsive" id="img-center" max-height="255px" width="auto" src="\';
        $close = \'" alt="" /></div><a>\';
        echo $open . implode( $close . $open, $thumbnails[\'children\'] ) . $close;
      }

    }
    ?>
提前感谢!汉内斯

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

我找到了方法!不使用该函数,可以添加查询和另一个函数。这是页面中的代码。php文件。

                <?php $subs = new WP_Query( array( \'post_parent\' => $post->ID, \'post_type\' => \'page\', \'meta_key\' => \'_thumbnail_id\' ));
    if( $subs->have_posts() ) : while( $subs->have_posts() ) : $subs->the_post();
    echo \'<div class="re-featured-image child-thumb">\' . get_the_post_thumbnail($post->ID, \'index-thumb\', array(\'class\' => \'img-responsive\', \'id\' => \'img-center\'));
    echo \'<div class="re-featured-image-title">\' . get_post(get_post_thumbnail_id($post->ID))->post_title . \'</div>\';
    echo \'<div class="re-featured-image-caption">\' . get_post(get_post_thumbnail_id($post->ID))->post_excerpt . \'</div>\';
    echo \'<div class="re-featured-image-description">\' . get_post(get_post_thumbnail_id($post->ID))->post_content . \'</div></div>\';

    endwhile; endif; wp_reset_postdata(); ?>
如果希望缩略图链接到其页面,请在函数中添加此函数。php文件。我在这里找到了密码http://wordpress.org/support/topic/making-post-thumbnail-link-to-post

add_filter( \'post_thumbnail_html\', \'my_post_image_html\', 10, 3 );

function my_post_image_html( $html, $post_id, $post_image_id ) {

$html = \'<a href="\' . get_permalink( $post_id ) . \'" title="\' . esc_attr( get_post_field( \'post_title\', $post_id ) ) . \'">\' . $html . \'</a>\';

return $html;
}

结束

相关推荐

Link images to post

将图像链接到帖子。大家好我相信这很简单,但我不知道怎么做。我知道如何在帖子中添加图像-单击帖子,添加媒体,选择缩略图。这给了我一个缩略图。我想我要做的是将缩略图链接到帖子,这样我就可以控制缩略图在页面上的显示位置。我有一个这样的模板。 <div class=\"content_div\"> <?php $car_args = array( \'post_type\' =>