【问题标题】:I'm facing issue in custom post type like if there's no post then we have to show custom message like sorry no post availableI'm facing issue in custom post type like if there's no post then we have to show custom message like sorry no post available
【发布时间】:2023-02-24 03:13:29
【问题描述】:

如果没有不可用的帖子,我将面临自定义帖子类型的问题,然后需要显示自定义消息。所以我试过下面的代码但是

                <div class="industry_item_grid">
                    <div class="industry_item_head">
                        <h3 class="industry_title">Telecom</h3>
                        <a href="<?php echo site_url('/category/telecom')?>" class="casestudy_viewall">View all</a>
                    </div>
                    <div class="industry_item_row">
                    <?php 

                        $paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1;
                        $args = array(
                            'post_type' => 'post',
                            'post_status' => 'publish',
                            'category_name' => 'Telecom',
                            'posts_per_page' => 3,
                            'cat'   => 23,
                            'paged' => $paged,
                        );
                        $arr_posts = new WP_Query( $args );
                        $totalpost = $arr_posts->found_posts;

                        if ( $arr_posts->have_posts() ) :

                            while ( $arr_posts->have_posts() ) :
                                $arr_posts->the_post();

                    ?>
                    

//the_content();

        <?php
            endwhile;
            endif;
                
        ?>
                    </div>
                </div>

【问题讨论】:

    标签: wordpress backend


    【解决方案1】:
    if ( $arr_posts->have_posts() ) :
    
                            while ( $arr_posts->have_posts() ) :
                                $arr_posts->the_post();
    

    have_posts() 为 false 时,您需要在 if 语句中添加一个分支,如下所示:

    if ( $arr_posts->have_posts() ) :
        while ( $arr_posts->have_posts() ) :
            $arr_posts->the_post();
    ?>
    //the_content();
    <?php
        endwhile;
    else: ?>
        <p>Sorry, no posts available.</p>
    <?php endif; ?>
    

    【讨论】:

      猜你喜欢
      • 2022-12-02
      • 2014-07-05
      • 1970-01-01
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多