获取wordpress页面的特色图像到header.php

时间:2012-06-28 15:18:27

标签: wordpress

我正在使用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中显示图像?

3 个答案:

答案 0 :(得分:0)

首先,您可能想要该图像的特定大小。在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] . '">');
?>

答案 1 :(得分:0)

如果帖子有...

,脚本下面的

将帮助您获得精选图片

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

答案 2 :(得分:0)

我觉得通过将参数传递给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 }?>