英文:
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.
<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 = [
"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>
答案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->have_posts() ) :
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
You need to add a branch to your if statement for when have_posts()
is false, like so:
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; ?>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论