【发布时间】:2018-08-02 14:39:15
【问题描述】:
我最近开发了一个 Wordpress 主题,这个主题有一点复杂的自定义帖子类型。
下面是我的自定义帖子查询代码。每当请求未返回任何帖子时,我都想要else 内容。它目前只是空白。
我不是 PHP 开发人员,有人可以帮我添加这样的替代内容吗?
谢谢。
<div class="front_page_deal_holder">
<?php
// get the currently queried taxonomy term, for use later in the template file
$term = get_queried_object();
//second query - posts
// Define the query
$args = array(
'post_type' => 'products',
'product_cat' => $term->slug
);
$query = new WP_Query( $args );
if ($query->have_posts()) {
// output the term name in a heading tag
echo'<h1>All Products about '. $term->name . '</h1>';
echo'<div class="row">';
// Start the Loop
while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col-lg-3">
<a style="display:block;text-decoration:none" href="<?php the_permalink(); ?>">
<div class="single_front_sec">
<?php $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'post-image'); ?>
<img class="post_thumb_cus" src="<?php echo $featured_img_url ?>" alt="Product Image">
<h2 class="featured_product_heading">
<?php the_title(); ?>
</h2>
<span class="offer_note"><p><?php echo get_post_meta($post->ID, 'Offer Note', true); ?></p></span>
<div class="vew_d_button_holder">
<a class="view_d_button" href="<?php the_permalink(); ?>">View Details</a>
</div>
</div>
</a>
</div>
<?php endwhile; ?>
</div>
<!-- row -->
<div class="my_Custom_pagination">
<?php wp_pagenavi( array( 'query' => $query ) ); ?>
</div>
<?php } // end of check for query having posts
// use reset postdata to restore orginal query
wp_reset_postdata();
?>
<?php include_once('top-categories.php'); ?>
<!--==== Top catogory section ====-->
<!-- front deal closed -->
</div>
【问题讨论】:
-
这个问题似乎不清楚。举例说明您想要什么、您拥有什么以及为什么它不起作用。
-
您的代码很难阅读它的格式。清理它会帮助你得到答案。
-
我已经发布了您的代码的其他部分,请查看
-
我想在这个循环中添加一个 else 数据,@ravipatel 给了我解决方案,非常感谢
标签: php wordpress custom-post-type