If和Else语句的帮助

时间:2012-03-29 作者:Johan Dahl

我有一个自定义循环,如果它没有得到任何结果,应该显示一些文本,比如“没有关于这个项目的新闻”。但我不知道怎么做。

代码:

<?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; ?>

1 个回复
最合适的回答,由SO网友:mor7ifer 整理而成

你要做的是完全重写这个超级复杂的结构,你必须这样做:

<?php if( $my_query->have_posts() ) : ?>
    <?php while( $my_query->have_posts() ) : $my_query->the_post(); ?>
        <!-- your output here -->
    <?php endwhile; else : ?>
        <!-- your failure output here -->
<?php endif; ?>

结束

相关推荐

simply loop through posts

我知道这是一个真正的新手问题,但我似乎无法从帖子中获得循环。它所做的只是从页面本身中提取。我制作了一个模板并添加了循环。<?php if( have_posts() ) { while( have_posts() ) { the_post(); ?> <h2><?php the_title(); ?></h2> <?php } } ?>