【问题标题】:wordpress featured image (thumbnail) on header标题上的 wordpress 特色图片(缩略图)
【发布时间】:2014-09-28 09:32:33
【问题描述】:

我正在修复某人的 wordpress 网站,我在 loop.php 和特定页面的模板文件中遇到了这个奇怪的代码。我的目标是更改它以在标题中显示特色图像。我对如何使用 get_thumbnail 语法输出特色图像进行了一些研究,我通常不会在 wordpress 的后端/FTP 中做太多事情,所以谢谢你的帮助和耐心。

<?php 
/**
* @package WordPress
* @subpackage WP-Skeleton
*/
?>
</div>
<div id="primary">
<div id="content">
    **<?php the_post(); ?>
        <?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id(           get_the_ID('full') ), 'page-header' );
        $url = $thumb['0'];?>
        <div id="page-header" style="background-image:url('<?php echo $url; ?>');">**
        </div>
    <div style="clear"></div>
    <div class="container">
        <section class="sixteen columns alpha">
            <header class="entry-header">
                <h2 class="entry-title">Events</h2>
            </header>
            <div class="entry-content ">
                <?php while ( have_posts() ) : the_post(); ?> <!--  the Loop -->
                <article id="post-<?php the_ID(); ?>" class="event">
                  <div class="title">            
                     <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title('<h3>', '</h3>'); ?></a>  <!--Post titles-->
                  </div>
                    <div class="event-img"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail('event-img'); ?></a></div>
                    <?php the_content("Continue reading " . the_title('', '', false)); ?> <!--The Content-->
                </article>
                <?php endwhile; ?><!--  End the Loop -->
            </div>
        </section>
    <div>
</div>  <!-- End two-thirds column -->
</div><!-- End Content -->

</diV>

【问题讨论】:

  • 显示的页面是merridees.jlbworks.net/events
  • 有什么问题?这一切看起来很正常。特色图像 (get_post_thumbnail_id) 正在显示为元素 #page-header 的背景图像。使用wp_get_attachment_image_src 检索此图像的 URL。 $thumb['0'] 是 URL(尽管 0 不应使用引号)。
  • 感谢您帮助我更好地理解这一点,目前这是循环文件夹,get_post_thumbnail 也在全角模板代码中,并且我假设调用了一个自定义模板代码特定类别的职位。 '事件'
  • 我还是不知道你在找什么。你有什么问题?

标签: php wordpress loops


【解决方案1】:

如果您想在标题中显示特色图片,请在其中使用以下代码。

<?php
    if(have_posts()) :
        while (have_posts()) : the_post();
            the_post_thumbnail();  
        endwhile;        
    endif;
?>

【讨论】:

  • 感谢您的解释,但如果我想显示页面上的特色图片而不是单个帖子怎么办。
  • 可以在page.php中使用同样的代码来获取页面的特色图片。
  • 我是否需要删除循环/模板文件中的代码?
  • 不,您可以在循环内显示所有页面/帖子属性。
猜你喜欢
  • 2011-03-11
  • 2012-04-02
  • 2016-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-02
  • 1970-01-01
  • 2019-03-22
相关资源
最近更新 更多