【问题标题】:WordPress Custom Post query else dataWordPress 自定义帖子查询其他数据
【发布时间】: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


【解决方案1】:
elseif(!$query->have_posts()){//HTML here}

else{ //HTML here}

在检查查询结束后,如果您想遵循 OOP 规则以使代码更清晰,请将所有内容移入 function.php 文件中的函数并在那里调用该函数

【讨论】:

  • 感谢您的回答,能否请您编辑我的代码并解决它,以获得详细的解决方案。
【解决方案2】:
<div class="front_page_deal_holder">
    <?php
    // get the currently queried taxonomy term, for use later in the template file
    $term = get_queried_object();
    $args = array(
      'post_type' => 'products',
      'product_cat' => $term->slug
    );
    $query = new WP_Query( $args );
    if ($query->have_posts()) {
        echo'<h1>All Products about '. $term->name . '</h1>';
        echo'<div class="row">';           
            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>
        <div class="my_Custom_pagination">
            <?php   wp_pagenavi( array( 'query' => $query ) ); ?>
        </div>
    <?php }else{


                // Add your code here for else part

        }
        wp_reset_postdata();
    ?>
    <?php include_once('top-categories.php'); ?>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2015-10-24
    • 1970-01-01
    相关资源
    最近更新 更多