我有一个自定义循环,如果它没有得到任何结果,应该显示一些文本,比如“没有关于这个项目的新闻”。但我不知道怎么做。
代码:
<?php
// Loop in the ten latest news with the same taxonomy term as the current post
$backup = $post; // backup the current object
$found_none = \'\';
$taxonomy = \'fastighetslista\';// e.g. post_tag, category, custom taxonomy
$param_type = \'fastighetslista\'; //e.g. tag__in, category__in, but genre__in will NOT work
$post_types = get_post_types( array(\'public\' => true), \'names\' );
$tax_args=array(\'orderby\' => \'none\');
$tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);
if ($tags) {
foreach ($tags as $tag) {
$args=array(
"$param_type" => $tag->slug,
\'post__not_in\' => array($post->ID),
\'post_type\' => $post_types,
\'showposts\'=> 10,
\'caller_get_posts\'=>1
);
$my_query = null;
$my_query = new WP_Query($args); ?>
<?php
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="press-entry-small">
<div class="press-entry-date-small">
<p class="press-day-small"><?php the_time(\'d\'); ?></p>
<p class="press-month-small"><?php the_time(\'M\'); ?></p>
</div>
<li class="news-link">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
</div>
<div class="clear"></div>
<?php $found_none = \'\';
endwhile;
}
}
}
if ($found_none) {
echo $found_none;
}
$post = $backup; // copy it back
wp_reset_query(); // to use the original query again
?>
我可以把我的“else”语句放在哪里?
谢谢
编辑(再次)。
好吧,我修改了代码,现在看起来是这样,但根本不起作用。
<?php
$taxonomy = \'fastighetslista\';// e.g. post_tag, category, custom taxonomy
$param_type = \'fastighetslista\'; // e.g. tag__in, category__in, but genre__in will NOT work
$post_types = get_post_types( array(\'public\' => true), \'names\' );
$tax_args=array(\'orderby\' => \'none\');
$tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);
if ($tags) {
foreach ($tags as $tag) {
$args=array(
"$param_type" => $tag->slug,
\'post__not_in\' => array($post->ID),
\'post_type\' => $post_types,
\'showposts\'=> 10,
\'caller_get_posts\'=>1
);
$my_query = null;
$my_query = new WP_Query($args); ?>
<?php if( $my_query->have_posts() ) : ?>
<?php while( $my_query->have_posts() ) : $my_query->the_post(); ?>
<div class="press-entry-small">
<div class="press-entry-date-small">
<p class="press-day-small"><?php the_time(\'d\'); ?></p>
<p class="press-month-small"><?php the_time(\'M\'); ?></p>
</div>
<li class="news-link"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
</div>
<div class="clear"></div>
<!-- your output here -->
<?php endwhile; else : ?>
<!-- your failure output here -->
<?php endif; ?>
编辑。好的,我把它弄好了。
代码:
<?php
$taxonomy = \'fastighetslista\';// e.g. post_tag, category, custom taxonomy
$param_type = \'fastighetslista\'; // e.g. tag__in, category__in, but genre__in will NOT work
$post_types = get_post_types( array(\'public\' => true), \'names\' );
$tax_args=array(\'orderby\' => \'none\');
$tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args); ?>
<?php
if ($tags) ?>
<?php foreach ($tags as $tag) ?>
<?php
$args=array(
"$param_type" => $tag->slug,
\'post__not_in\' => array($post->ID),
\'post_type\' => $post_types,
\'showposts\'=> 10,
\'caller_get_posts\'=>1
);
$my_query = null;
$my_query = new WP_Query($args); ?>
<?php if( $my_query->have_posts() ) : ?>
<?php while( $my_query->have_posts() ) : $my_query->the_post(); ?>
<div class="press-entry-small">
<div class="press-entry-date-small">
<p class="press-day-small"><?php the_time(\'d\'); ?></p>
<p class="press-month-small"><?php the_time(\'M\'); ?></p>
</div>
<li class="news-link"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
</div>
<div class="clear"></div>
<!-- your output here -->
<?php endwhile; else : ?>
Blablabla
<?php endif; ?>