“条件标记”的问题:如果为空

时间:2011-10-16 作者:japanworm

我已经尝试了一段时间,如果没有相关的帖子,就不会显示任何内容。当前显示的是标题和容器(当然是空的)。

正如我所说,我做了很多尝试,我得出的结论是,如果不是不可能的话,可能很难摆脱所有东西,因为代码非常复杂:

<h2>Related Posts</h2>                      

<!-- "previous page" action -->
<a class="prev browse left"></a>

<!-- root element for scrollable -->
<div class="scrollable" id=chained>   

    <!-- root element for the items -->
    <div class="items">

    <!-- post 1-4 -->
        <div>

        <?php 
            $backup = $post;
            $tags = wp_get_post_tags($post->ID);
            if ($tags) {
                $tag_ids = array();
                foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;

                $args=array(
                    \'tag__in\' => $tag_ids,
                    \'post__not_in\' => array($post->ID),
                    \'showposts\'=>4, // Number of related posts that will be shown.                                                                                                            
                    \'caller_get_posts\'=>1
                );
                $my_query = new wp_query($args);
                if( $my_query->have_posts() ) {
                    while ($my_query->have_posts()) {                                                               
                        $my_query->the_post();
        ?>

        <div class="relatedPosts"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_post_thumbnail(array(120,80)); ?></a>
        <div class="relatedPosts_title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></div></div>

        <?php
                    }
                    echo \'\';
                }
            }
            $post = $backup;
            wp_reset_query();
        ?>

        </div>

        <!-- post 5-8 -->
        <div>

   ......
      </div>

   </div>

</div>

<!-- "next page" action -->
<a class="next browse right"></a>
相反,我想保留标题和容器,但要确保容器不是空的。

为此,我想使用“if”或“elseif”标记

诸如此类:

<!-- If there are no related posts:-->
<p class="noposts">Sorry, but there are no related posts for this particular entry.</p>
我似乎不知道如何正确地实施它。如果有人对条件标记有一点了解,能帮我弄清楚就好了。非常感谢!:)

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

我不明白为什么做你想做的事是不可能的。我想你只需要重新安排一下。首先放置自定义查询代码,然后将包含HTML的标记放在if ( $my_query->have_posts() ), 然后将相关的贴子标记放在while ( $my_query->have_posts() ), 然后将不相关的贴子标记放在else {} 语句,然后将包含HTML标记的结束语放在else {} 声明,然后关闭while 声明:

<?php
// First, backup the default $postdata
$backup = $post;

// Now, override the default
$tags = wp_get_post_tags($post->ID);

// Now, open the if ( $tags ) statement
if ($tags) {
    $tag_ids = array();
    foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;

    // Now, setup your custom query args                    
    $args=array(
        \'tag__in\' => $tag_ids,
        \'post__not_in\' => array($post->ID),
        \'showposts\'=>4, // Number of related posts that will be shown.                                                                                                            
        \'caller_get_posts\'=>1
    );
    // Now, perform your custom query
    $my_query = new wp_query($args);

    // Next, open your custom query IF statement
    if( $my_query->have_posts() ) {
        // We have posts, so let\'s output the opening-containing markup
        ?>

        <h2>Related Posts</h2>                      

        <!-- "previous page" action -->
        <a class="prev browse left"></a>

        <!-- root element for scrollable -->
        <div class="scrollable" id=chained>   

            <!-- root element for the items -->
            <div class="items">

                <!-- post 1-4 -->
                <div>

                <?php 
                // Now, open your custom query WHILE statement
                while ($my_query->have_posts()) {                                                               
                    $my_query->the_post();
                    ?>

                    <div class="relatedPosts"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_post_thumbnail(array(120,80)); ?></a>
                    <div class="relatedPosts_title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></div></div>

                    <?php
                    // Now, close the while $my_query->have_posts() statement
                } 
                // I\'m not sure why this is here?
                echo \'\';
                // Now, output your closing-containing HTML content
                ?>    
                </div>

            </div>

        </div>

        <!-- "next page" action -->
        <a class="next browse right"></a>
        <?php
    // Now, close the if $my_query->have_posts() statement
    // and open the ELSE statement, for your no-posts content
    } else {
        ?>
        <!-- If there are no related posts:-->
        <p class="noposts">Sorry, but there are no related posts for this particular entry.</p>
        <?php
    // Now, close the ELSE statement
    }
// Now, close the if ( $tags ) statement
}
// Now, reset the default query
$post = $backup;
wp_reset_query();
?>
注意:我删除了“Post 5-8”div,因为我不确定它们应该如何适应这个标记。

结束

相关推荐