MyPosts查询无效的案例声明

时间:2015-06-04 18:59:55

标签: wordpress

我一直坚持查询我正在努力写作。我的目标是创建3个用例,具体取决于滑块类别24中的帖子数量。对于24类中没有帖子,我想隐藏所有代码。对于类别24中的一个帖子,我想输出用于设置单个帖子样式的HTML。对于多个帖子,我想输出用于在滑块中设置帖子样式的HTML。

我的问题是,如果没有帖子且有一个帖子,我的代码工作正常,但如果有多个帖子,它似乎会卡住。它只输出一个帖子的大小写。任何帮助,将不胜感激!

    <div id="posts">
        <?php
    $myposts = get_posts('posts_per_page=-1&category=24');
    // Check to make sure you have an array, set the postcount to zero if you don't.
    if (is_array($myposts)) {
        $postcount = count($myposts);
    } else {
        $postcount = 0;
    }
    switch($postcount) {
        case 0:
            //  Do nothing, so no code here for no posts.
        break;
        case 1:
            // Code here for what you want to do if there is only one post.
?>
            <?php query_posts($query_string . '&cat=24&posts_per_page=1'); ?>
 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <div class="onefeatured-post">
    <ul id="onefeatured-post-list"><div class="onefeatured-post-image">
                <?php featured_post_image(); ?>
            </div>

            <div class="onefeatured-post-text">
                <h2 class="onefeatured-post-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
                <div class="onefeatured-post-content"><?php limits(160, "Read more"); ?></div>
            </div>
            <div class="clearfix"></div>
        </li></ul>
    <div class="onefeatured-post-nav">
        <div id="onefeatured-post-pages"></div>
    </div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php
        break;
        default:
            // Code here for what you want to happen if there is more than one post.
            include (TEMPLATEPATH . '/featured-posts.php');
} ?>

1 个答案:

答案 0 :(得分:0)

使用普通条件可能会更直观一些:

<div id="posts">
  <?php
    $myposts = get_posts('posts_per_page=-1&category=24');

    if( ! empty( $myposts ) && 1 == count( $myposts ) ) {
        query_posts( $query_string . '&cat=24&posts_per_page=1' );

        if( have_posts() ) : while( have_posts() ) : the_post();
  ?>
            <div class="onefeatured-post">
                <ul id="onefeatured-post-list">
                    <li>
                        <div class="onefeatured-post-image">
                            <?php featured_post_image(); ?>
                        </div>

                        <div class="onefeatured-post-text">
                            <h2 class="onefeatured-post-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
                            <div class="onefeatured-post-content"><?php limits(160, "Read more"); ?></div>
                        </div>
                      <div class="clearfix"></div>
                    </li>
                </ul>
                <div class="onefeatured-post-nav">
                    <div id="onefeatured-post-pages"></div>
                </div>
            </div>
  <?php
        endif;
      endwhile;
    } elseif( ! empty( $myposts ) ) {
        include( TEMPLATEPATH . '/featured-posts.php' );
    } else {
        echo "<p>No posts found</p></div>";
    }
  ?>

第一个条件将捕获1的数组,第二个条件将捕获大于1的数组,然后我们只有一个else来捕获空。