如果有帖子显示div其他显示没有帖子声明

时间:2014-08-26 19:19:49

标签: php wordpress

我不知道为什么这段代码不起作用。我正在尝试创建一个条件语句,如果此自定义帖子类型有帖子,则显示customer-section div。如果帖子不存在则打印没有帖子声明。我做了我认为我想做的一切,但我必须做一些愚蠢的错误,因为即使没有帖子,我仍然可以看到客户部分div。

这就是我所拥有的:

 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <div class="customer-section case-study">
    <div class="case-study-container">
        <h2>Case Studies</h2>
        <?php $loop = new WP_Query( array( 'post_type' => 'case_study', 'posts_per_page' => 9 ) ); ?>
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <div <?php post_class(); ?>>

                    <?php if ( has_post_thumbnail() ): ?>
                        <div class="press-featured-image">
                            <?php the_post_thumbnail('', array('class' => 'th')); ?>
                        </div>
                    <?php endif; ?>

                    <div class="blog-post">
                        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                        <div class="entry-summery">
                            <?php the_excerpt(); ?>
                        </div>
                            <footer>
                                <?php $tag = get_the_tags(); if (!$tag) { } else { ?><p><?php the_tags(); ?></p><?php } ?>
                            </footer>
                    </div>
                    <hr />
            </div>
        <?php endwhile; wp_reset_query(); ?>
    </div>
    </div>

 <?php endwhile; else : ?>

<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>

 <?php endif; ?>

3 个答案:

答案 0 :(得分:2)

您目前正在做的是检查您的初始have_posts()(默认查询)条件是否为真,如果这是页面模板,则始终为真。实际上(对于页面模板)您根本不需要检查,因为如果找不到页面,WordPress将返回404。  您需要检查自定义查询的帖子:

<?php the_post(); ?>
<div class="customer-section case-study">
    <div class="case-study-container">
        <h2>Case Studies</h2>
        <?php $loop = new WP_Query( array( 'post_type' => 'case_study', 'posts_per_page' => 9 ) );

        if ( $loop->have_posts() ):

            while ( $loop->have_posts() ) : $loop->the_post(); ?>
                <div <?php post_class(); ?>>

                        <?php if ( has_post_thumbnail() ): ?>
                            <div class="press-featured-image">
                                <?php the_post_thumbnail('', array('class' => 'th')); ?>
                            </div>
                        <?php endif; ?>

                        <div class="blog-post">
                            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                            <div class="entry-summery">
                                <?php the_excerpt(); ?>
                            </div>
                                <footer>
                                    <?php $tag = get_the_tags(); if (!$tag) { } else { ?><p><?php the_tags(); ?></p><?php } ?>
                                </footer>
                        </div>
                        <hr />
                </div>
            <?php endwhile;

        else: ?>
            <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
        <?php endif;

        wp_reset_query();

        ?>
    </div>
</div>

答案 1 :(得分:0)

我认为问题是你的语法有两个循环。

  1. 您在if条件
  2. 之前错过了while声明
  3. 您正试图在标准页面或发布循环中加载$loop循环。
  4. 请改为尝试:

    <?php
    $case_studies = new WP_Query( array( 'post_type' => 'case_study', 'posts_per_page' => 9 ) );
    
    if ( $case_studies->have_posts() ) : ?>
    
    <div class="customer-section case-study">
        <div class="case-study-container">
            <h2>Case Studies</h2>
    
            <?php while ( $case_studies->have_posts() ) : $case_studies->the_post(); ?>
    
            <div <?php post_class(); ?>>
                <?php if ( has_post_thumbnail() ): ?>
                    <div class="press-featured-image">
                        <?php the_post_thumbnail('', array('class' => 'th')); ?>
                    </div>
                <?php endif; ?>
    
                <div class="blog-post">
                    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                    <div class="entry-summary">
                        <?php the_excerpt(); ?>
                    </div>
                    <footer>
                      <?php if (get_the_tags()) { ?>
                        <p><?php the_tags(); ?></p>
                      <?php } ?>
                    </footer>
                </div>
                <hr />
            </div>
        <?php endwhile; ?>
    
        </div>
    </div>
    <?php else : ?>
    
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; wp_reset_query(); ?>
    

答案 2 :(得分:0)

这对我有用。如果它在WP_Query中,则不需要结束while循环。您要做的就是关闭要显示的主div的if语句,为重复部分添加while,如果它不起作用,我将尝试删除您在页脚中设置的条件。

<?php $loop = new WP_Query( array( 'post_type' => 'case_study', 'posts_per_page' => 9 ) ); if($loop->have_posts()): ?>

    <div class="customer-section case-study">
    <div class="case-study-container">
        <h2>Case Studies</h2>

        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <div <?php post_class(); ?>>

                    <?php if ( has_post_thumbnail() ): ?>
                        <div class="press-featured-image">
                            <?php the_post_thumbnail('', array('class' => 'th')); ?>
                        </div>
                    <?php endif; ?>

                    <div class="blog-post">
                        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                        <div class="entry-summery">
                            <?php the_excerpt(); ?>
                        </div>
                            <footer>
                                <?php $tag = get_the_tags(); if (!$tag) { } else { ?><p><?php the_tags(); ?></p><?php } ?>
                            </footer>
                    </div>
                    <hr />
            </div>
            <?php   } wp_reset_postdata(); ?>
    </div>
    </div>

 <?php endif; ?>
相关问题