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 available

huangapple go评论58阅读模式
英文:

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 available

问题

我在一个自定义文章类型的问题上遇到了一个问题,如果没有可用的文章,我需要显示一条自定义消息。我尝试了下面的代码,但它不起作用。

    <div class="industry_item_grid">
            <div class="industry_item_head">
                <h3 class="industry_title">电信</h3>
                <a href="<?php echo site_url(
                    "/category/telecom"
                ); ?>" class="casestudy_viewall">查看全部</a>
            </div>
            <div class="industry_item_row">
                <?php
                $paged = get_query_var("paged")
                    ? get_query_var("paged")
                    : 1;
                $args = [
                    "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>
英文:

I'm facing an issue with a custom post type where, if no posts are available, I need to display a custom message. I have tried the below code, but it is not working.

&lt;div class=&quot;industry_item_grid&quot;&gt;
        &lt;div class=&quot;industry_item_head&quot;&gt;
            &lt;h3 class=&quot;industry_title&quot;&gt;Telecom&lt;/h3&gt;
            &lt;a href=&quot;&lt;?php echo site_url(
                &quot;/category/telecom&quot;
            ); ?&gt;&quot; class=&quot;casestudy_viewall&quot;&gt;View all&lt;/a&gt;
        &lt;/div&gt;
        &lt;div class=&quot;industry_item_row&quot;&gt;
            &lt;?php
            $paged = get_query_var(&quot;paged&quot;)
                ? get_query_var(&quot;paged&quot;)
                : 1;
            $args = [
                &quot;post_type&quot; =&gt; &quot;post&quot;,
                &quot;post_status&quot; =&gt; &quot;publish&quot;,
                &quot;category_name&quot; =&gt; &quot;Telecom&quot;,
                &quot;posts_per_page&quot; =&gt; 3,
                &quot;cat&quot; =&gt; 23,
                &quot;paged&quot; =&gt; $paged,
            ];
            $arr_posts = new WP_Query($args);
            $totalpost = $arr_posts-&gt;found_posts;

            if ($arr_posts-&gt;have_posts()):
                while ($arr_posts-&gt;have_posts()):
                    $arr_posts-&gt;the_post(); ?&gt;
                    //the_content();
            &lt;?php
                endwhile;
            endif;
            ?&gt;
        &lt;/div&gt;
&lt;/div&gt;

答案1

得分: 0

if ( $arr_posts->have_posts() ) :
    while ( $arr_posts->have_posts() ) :
        $arr_posts->the_post();
    endwhile;
else: ?>
    <p>抱歉,没有可用的文章。</p>
<?php endif; ?>
英文:
if ( $arr_posts-&gt;have_posts() ) :

                        while ( $arr_posts-&gt;have_posts() ) :
                            $arr_posts-&gt;the_post();

You need to add a branch to your if statement for when have_posts() is false, like so:

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

huangapple
  • 本文由 发表于 2023年2月23日 19:27:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定