我很难只在每个类别的第一篇文章中设置特色图像,而其余的只显示文章标题。到目前为止,我一直设法用缩略图显示每个类别中的最新帖子,但我找不到方法,只能用缩略图显示第一篇帖子,其余的都是标题。有解决方案吗?这是我的代码:
<?php
//get terms (category ids 11,2,33,34), then display one post in each term
$taxonomy = \'category\';// e.g. post_tag, category
$param_type = \'category__in\'; // e.g. tag__in, category__in
$term_args=array(
\'include\' => \'4,5,6,7\',
\'orderby\' => \'name\',
\'order\' => \'ASC\'
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
foreach( $terms as $term ) {
$args=array(
"$param_type" => array($term->term_id),
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 1,
\'caller_get_posts\'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
//Panikos Remove echo \'Latest Post in \'.$taxonomy .\' \'.$term->name;
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="post_home_main">
<!-- Add timthumb to post featured image -->
<?php
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_url($image_id);
if ($image_url) {
?>
<img src="<?php bloginfo(\'template_directory\'); ?>/scripts/timthumb.php?src=<?php echo $image_url; ?>&w=196&h=115&q=1000" alt="<?php the_title();?>" />
<!-- // Add timthumb to post featured image -->
<?php } else { ?>
<img src="<?php echo bloginfo(\'template_directory\') . \'/images/no-image.jpg\'; ?>" alt="No image available" class="panikosule" />
<?php } ?>
<a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
<?php the_excerpt(); ?>
</div>
<?php
endwhile;
}
}
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
<span class="arrow"></span> </div>
SO网友:kaiser
2nd(还有afaik),你可以用船上的东西来做这件事,比如get_image_tag()
在你的情况下3rd:不要将那么多变量放入全局名称空间。如果您真的认为无法解决此问题:请在它们前面加上一个唯一的字符串,如wpse69602_
.尝试编写带有大量空白的干净代码。这有助于您发现以下错误:\'include\' => \'4,5,6,7\'
. 提示:大多数函数采用数组,而不是特殊的字符分隔字符串您清理的代码
我用来识别当前循环帖子编号的kool不是
$flag
. 这是
$wp_query
对象:The
current_post
, 这是WordPress已经设置的标志。
<div class="container">
<?php
$terms = get_terms(
\'category\'
,array(
\'include\' => array( \'4,5,6,7\' )
,\'orderby\' => \'name\'
,\'order\' => \'ASC\'
)
);
if ( ! empty( $terms ) )
{
foreach( $terms as $term )
{
$my_query = new WP_Query( array(
\'category__in\' => array( $term->term_id )
,\'post_type\' => \'post\'
,\'post_status\' => \'publish\'
,\'posts_per_page\' => 1
,\'caller_get_posts\' => 1
) );
if ( $my_query->have_posts() )
{
while ( $my_query->have_posts() )
{
$my_query->the_post();
?>
<div class="post_home_main">
<!-- Show the posts featured image -->
<?php
if ( 1 <= $my_query->current_post )
{
if ( $image_url = wp_get_attachment_url( get_post_thumbnail_id() ) )
{
echo get_image_tag(
get_post_thumbnail_id()
,__( \'Featured Image\', \'your_textdomain\' )
,get_the_title()
,\'none\'
// Check your image sizes and adjust this one
,\'thumb\'
);
}
else
{
?>
<img src="<?php echo get_template_directory().\'/images/no-image.jpg\'; ?>" alt="No image available" class="panikosule" />
<?php
}
}
?>
<a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
<?php the_excerpt(); ?>
</div>
<?php
} // endwhile;
}
}
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
<span class="arrow"></span>
</div>
SO网友:Mridul Aggarwal
就在while ($my_query->have_posts())
添加$flag = false;
, 然后将您的特色图像代码包装在另一个if条件中,如下图所示
if( !$flag ) {
$flag = true;
// put all you featured image code here
}
现在更改
post_per_page
参数设置为更高的数字。图像代码仅在每个类别的循环第一次运行时执行
UPDATE
<?php
if( !$flag ) {
$flag = true;
?>
<!-- Add timthumb to post featured image -->
<?php
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_url($image_id);
if ($image_url) {
?>
<img src="<?php bloginfo(\'template_directory\'); ?>/scripts/timthumb.php?src=<?php echo $image_url; ?>&w=196&h=115&q=1000" alt="<?php the_title();?>" />
<!-- // Add timthumb to post featured image -->
<?php } else { ?>
<img src="<?php echo bloginfo(\'template_directory\') . \'/images/no-image.jpg\'; ?>" alt="No image available" class="panikosule" />
<?php } ?>
<?php } ?>