如何对缩略图使用wp_GET_ATTACHING_METADATA

时间:2015-12-17 作者:Pikk

我想添加正确的富文本片段,以便Goole结构化数据验证器能够传递我的所有代码。唯一缺少的是缩略图的大小。

这是Wordpress主页循环中的缩略图的具体示例。

我有以下代码:

<div itemprop="image" itemscope itemtype="https://schema.org/ImageObject">
                        <meta itemprop="url" content="<?php echo wp_get_attachment_thumb_url( get_post_thumbnail_id( $post->ID ) ); ?>">
                    <?php if ( has_post_thumbnail() ) {
the_post_thumbnail( \'full\', array(\'class\'=>\'post_thumbnail_common\', \'alt\' => get_the_title() , \'title\' => get_the_title() ));
echo contentnoimg(41);} else { echo content(41); } ?>
                        </div>
在此,我想添加以下内容:

<meta itemprop="width" content="800">
<meta itemprop="height" content="800">
因此,最终代码将如下所示:

<div itemprop="image" itemscope itemtype="https://schema.org/ImageObject">
                        <meta itemprop="url" content="<?php echo wp_get_attachment_thumb_url( get_post_thumbnail_id( $post->ID ) ); ?>">
<meta itemprop="width" content="<?php XXX ?>">
<meta itemprop="height" content="<?php XXX ?>">
                    <?php if ( has_post_thumbnail() ) {
the_post_thumbnail( \'full\', array(\'class\'=>\'post_thumbnail_common\', \'alt\' => get_the_title() , \'title\' => get_the_title() ));
echo contentnoimg(41);} else { echo content(41); } ?>
                        </div>
我应该如何放置,而不是,以便图像的高度和宽度将得到很好的收集?

以下是content和contentnoimg函数-它们对缩略图后显示的摘录进行格式化,并在X个字符后进行裁剪:

function content( $limit ) {
    global $post;

    if( has_excerpt() ){
        $content = the_excerpt();
    } else {
      $content = explode( \' \', get_the_content(), $limit );
      if ( count($content) >= $limit ) {
        array_pop( $content );
        $content = implode( " ", $content );
        $content = wp_strip_all_tags( $content, true );
// $content .= \'...<br><a href="\'. get_permalink($post->ID) . \'" class="awesomebtn">\'.__(\'Read full post\',\'language\') .\'</a>\';
      } else {
        $content = implode( " ", $content );
      }

      $content = preg_replace( \'/\\[.+\\]/\',\'\', $content );
      $content = apply_filters( \'the_content\', $content ); 
      $content = str_replace( \']]>\', \']]&gt;\', $content );
    }

    return $content;
}



function contentnoimg($limit) {
     global $post;
         if( has_excerpt() ){
        $content = the_excerpt();
    } else {     
      $content = explode(\' \', get_the_content(), $limit);
      if (count($content)>=$limit) {
        array_pop($content);
        $content = implode(" ",$content);
$content = wp_strip_all_tags($content, true);
// $content .= \'...<br><a href="\'. get_permalink($post->ID) . \'" class="awesomebtn">\'.__(\'Read full post\',\'language\') .\'</a>\';
      } else {
        $content = implode(" ",$content);
      }   
      $content = preg_replace(\'/(<img.+?>)/\',\'\', $content);
      $content = apply_filters(\'the_content\', $content); 
      $content = str_replace(\']]>\', \']]&gt;\', $content);
    }
      return $content;
    }
谢谢

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

结构应该是这样的。

检查是否有缩略图获取元数据为文件添加元数据,然后渲染图像

if ( has_post_thumbnail() ) : // check if the post has a Post Thumbnail assigned to it.

$upload_dir = wp_upload_dir();

$size = \'full\';

$post_thumbnail_id = get_post_thumbnail_id( $post_id );
$post_thumbnail_meta = wp_get_attachment_metadata ( $post_thumbnail_id );
$main_file = $post_thumbnail_meta [ \'file\' ];
$dirname = dirname ( $main_file );
$base_url = trailingslashit ( $upload_dir[\'baseurl\'] ) . $dirname . \'/\';

// fallback to known file if the size doesn\'t exist
if ( ! isset($post_thumbnail_meta [ \'sizes\' ][ $size ] ) )
{
    $size = \'full\'; // use this size when we\'re missing data
}

// full is at the root, alternate sizes exist in the sizes prop
$imgInfo = $size === \'full\' ? $post_thumbnail_meta : $post_thumbnail_meta [ \'sizes\' ][ $size ];
$filename = basename ( $imgInfo[ \'file\' ] );
$width = $imgInfo[ \'width\' ];
$height = $imgInfo[ \'height\' ];
$file = $base_url . $filename;

?>
<div itemprop="image" itemscope itemtype="https://schema.org/ImageObject">
    <meta itemprop="url" content="<?php echo $file; ?>">
    <meta itemprop="width" content="<?php echo $width; ?>">
    <meta itemprop="height" content="<?php echo $height; ?>">

    <?php
        the_post_thumbnail( \'full\', array(\'class\'=>\'post_thumbnail_common\', \'alt\' => get_the_title() , \'title\' => get_the_title() ) );

        echo contentnoimg(41);
    ?>
</div>
<?php

else:  // end has_post_thumbnail else

    // no thumbnail
    echo content(41); 

endif; // end has_post_thumbnail block

相关推荐