【问题标题】:Fetching wordpress page featured image to the header.php将 wordpress 页面特色图片提取到 header.php
【发布时间】:2014-04-19 00:51:22
【问题描述】:

我正在开发一个 wordpress 驱动的网站,其模板在首页上有一个 jcycle 滑块。我希望在除主页之外的所有页面中使用静态横幅。我在 header.php 文件中找到了一个函数,如下所示:

<?php   else : // NOT front page ?>`
    <div id="page-content-title">
        <div id="page-content-header" class="container_24">
            <div id="page-title">

如何在此处显示来自 PAGE FEATURED IMAGE 的图像?

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    首先,您可能需要该图像的特定尺寸。在您的functions.php 文件中使用add_image_size 来执行此操作。然后使用wp_image_attachment_src

    要在页面上获取该图像,请执行以下操作:

    <?php
        $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'slider-size' );
        echo ('<img src="' . $thumbnail[0] . '">');
    ?>
    

    【讨论】:

      【解决方案2】:

      如果帖子有以下脚本将帮助您获得特色图片...

      query_posts(); 
      while (have_posts()) : the_post(); 
      $featuredImage = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
      echo $featuredImage; 
      endwhile; 
      wp_reset_query();
      

      【讨论】:

        【解决方案3】:

        我觉得通过将参数传递给post_thumbnail 回调有一种更简洁的方法。这就是我想出的。希望这可以帮助。这只会打印特色图像(如果存在)并将标题图像包装在 div 中,以便您可以控制 DOM 中的位置。

        <?php // Loop used to show post-thumbnail as #header-image if it exists 
        while ( have_posts() ) : the_post(); ?>
        
                    <?php if ( has_post_thumbnail() ) { ?>
                    <div id="header-image">
                    <?php the_post_thumbnail('full');?>
                    </div>
        
                    <?php } else { ?>
        
                    <?php }?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-02-25
          • 1970-01-01
          • 2012-10-26
          • 2019-03-22
          • 2019-07-31
          • 1970-01-01
          • 2014-09-13
          相关资源
          最近更新 更多