首先,PHP不是我的强项,但我们来试试吧。
我在functions.php中有一个函数,它获取特色图片并将其设置为背景。然后这个函数在header.php中被调用。
function set_post_background() {
if(query_posts(array ('category_name' => 'results')));
if (have_posts()) : while (have_posts()) : the_post();
global $post;
$bgimage = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "Full");
if (!empty($bgimage)) {
return '<style type="text/css">body {background:#fff url('.$bgimage[0].') no-repeat top center;}</style>';
}
endwhile; endif;
wp_reset_query();
}
昨晚,我尝试修改函数,并将其包装在
query_posts()
中。我设法让它有点工作了。现在,如果文章特色图像位于“结果”类别中,它将仅获取该图像并将其设置为背景。但是,这段代码中有问题,因为现在我的页面内容都没有显示出来。禁用该函数后,内容就会恢复显示。
我做错了什么?
[编辑] 我认为是我查询类别名称的方式有问题。因为page.php类似的查询可以获得
the_content()
,我认为该函数正在覆盖该查询,因此未显示页面内容。
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?> <?php if ( is_front_page() ) { ?> <h2><?php the_title(); ?></h2> <?php } else { ?> <h1><?php the_title(); ?></h1> <?php } ?> <?php the_content(); ?> <?php wp_link_pages( array( 'before' => '' . __( 'Pages:', 'twentyten' ), 'after' => '' ) ); ?> <?php edit_post_link( __( 'Edit', 'twentyten' ), '', '' ); ?> <?php comments_template( '', true ); ?> <?php endwhile; ?>
- peter beard