在尝试了Loops和Wordpress函数之后,这是我想到的唯一一种使用alt == excerpt
和title == title
自动显示"特色"图像的方法。
这是最有效的方法吗?
<?php
query_posts(array('category_name' => 'Featured'));
if (have_posts()) : while(have_posts()) : the_post();
$alt = get_the_excerpt();
$title = get_the_title();
the_post_thumbnail( 'full', array('alt' => $alt, 'title' => $title, 'class' => 'bigImg') );
endwhile; endif;
?>
最让我头疼的是我在循环中定义摘录和标题,因此我还必须在循环中显示我的哈希数组。只是有一点我不太满意。
为什么不只使用global $post;
,然后在需要的地方使用$post['post_title']
但是使用get_the_*()
函数也会对这些值应用过滤器,并且您想要!
PS:不确定我是否答对了问题,但你在抱怨两个对你来说似乎多余的变量。完成后,您可以随时unset($alt, $title)
它们
实际上,您可以使用<?php the_excerpt(); ?>
、<?php the_title(); ?>
和<?php the_excerpt(); ?>
,而无需定义它们。
我假设你展示的是一篇特色文章。为什么不添加一些HTML和CSS来设计循环的样式呢?下面是我要用的。
<?php query_posts( '$cat=1' . '&posts_per_page=1' );
if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="column">
<div class="thumbnail"><?php echo get_the_post_thumbnail($page->ID, array(254,254), 'thumbnail'); ?></div>
<h2 class="title"><?php the_title(); ?></h2>
<div class="post">
<p>Posted in <a href="<?php $category = get_the_category();?>">
<?php echo $category[0]->cat_name;?></a> <br />on <?php the_time('d/m/Y'); ?></p>
</div>
<p><?php the_excerpt(); ?></p>
</article>
<?php endwhile;?>
<?php endif;?>
$cat
是一个类别,你可以在wordpress上找到"特色"的类别。如果你想显示一个以上的帖子,你可以将&posts_per_page=1
更改为你想要的任何数字。