我用下面的代码将图像调用到single.php中以添加CSS
single.php
<? if( has_post_thumbnail( $post_id ) ): ?>
<div class="post-image">
<img src="<?=wp_get_attachment_url( get_post_thumbnail_id() ); ?>">
</div>
<? endif; ?>问题是,我现在发现,当我点击编辑媒体时,通过CMS添加的元数据没有显示。
有人知道如何将它们编码到我的single.php中吗?
我的完整的single.php
<?php get_header(); ?>
<? if( has_post_thumbnail( $post_id ) ): ?>
<div class="single-featured-image post-image">
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<img src="<?=wp_get_attachment_url( get_post_thumbnail_id() ); ?>">
</div>
<? endif; ?>
<img src="<?php echo $url; ?>" />
<div class="blog-post">
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'content-single', get_post_format() );
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile; endif;
?>
</div>
</div> // closes of container
<?php get_footer(); ?>发布于 2017-01-05 11:32:31
要获得"alt“属性,您需要使用get_post_meta作为附件id。但是对于标题和标题,您需要直接检索附件帖子,因为该信息存储在wp_posts中。
所以你可以这样做:
<?php if( has_post_thumbnail( $post_id ) ):
$pic_id = get_post_thumbnail_id();
$pic = get_post( $pic_id );
$caption = $pic->post_excerpt;
$image_alt = get_post_meta( $pic->ID, '_wp_attachment_image_alt', true );
$image_title = $pic->post_title;
?>
<div class="post-image">
<h4><?php echo $image_title; ?></h4>
<img src="<?php echo wp_get_attachment_url( $pic_id); ?>" alt="<?php echo $image_alt; ?>">
<span class="description"><?php echo $caption;?></span>
</div>
<? endif; ?>更改HTML以适应您的需要,我不知道您想在哪里显示图例和标题。
发布于 2017-01-05 11:26:05
您可能需要的是元数据。示例:
$meta_value = get_post_meta( get_the_ID(), 'meta_key_name', true );get_the_ID()将是当前帖子的Id。用我们自己的元密钥替换meta_key_name
https://stackoverflow.com/questions/41483441
复制相似问题