尝试在自定义主题中的帖子内容上方尝试获取单个帖子图片和标题。 我可以获取图像下方的图像标题和新div下面的剩余内容吗?
当前代码:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( '<h3 class="title no-underline">', '</h3>' ); ?>
</header>
<div class="entry-content news-item-copy">
<?php
$get_description = get_post(get_post_thumbnail_id())->post_excerpt;
the_post_thumbnail();
if(!empty($get_description)){//If description is not empty show the div
echo '<div class="image-captions">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>';
}
?>
<div class="news-sharing">
<a class="socialite twitter-share" href="" target="_blank" data-via="" data-text="New Website Launch — " data-url="" data-count="horizontal">Share on Twitter</a>
<a class="facebook-like socialite" data-href="" target="_blank" data-send="false" data-layout="button_count" data-width="450" data-show-faces="false">Like on Facebook</a>
</div>
<?php the_excerpt(); ?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'themeName' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
</article><!-- #post-## -->
这适用于显示帖子图像和摘录,但不能显示带有标题的图像,而不是完整的帖子文本。
取而代之:
<?php the_excerpt(); ?>
使用:
<?php the_content(); ?>
再次返回完整的帖子内容,包括图片。
答案 0 :(得分:0)
试试这个,获取post对象然后获取它的值没有什么特别之处。 post_excerpt也称为标题。
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( '<h3 class="title no-underline">', '</h3>' ); ?>
</header>
<div class="entry-content news-item-copy">
<?php
if(has_post_thumbnail())
{
the_post_thumbnail();
$thumbnail_id = get_post_thumbnail_id(get_the_ID());
$thumbnail_data = get_post($thumbnail_id);
$caption = $thumbnail_data->post_excerpt;
if(!empty($caption)){//If description is not empty show the div
echo '<div class="image-captions">' . $caption . '</div>';
}
}
?>
<div class="news-sharing">
<a class="socialite twitter-share" href="" target="_blank" data-via="" data-text="New Website Launch — " data-url="" data-count="horizontal">Share on Twitter</a>
<a class="facebook-like socialite" data-href="" target="_blank" data-send="false" data-layout="button_count" data-width="450" data-show-faces="false">Like on Facebook</a>
</div>
<?php
echo the_content();
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'themeName' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
</article><!-- #post-## -->
在此之后,您无需将精选/缩略图添加到帖子内容,并且它不会再次出现在内容中。我希望现在有道理。