【问题标题】:wordpress query , that shows featured productswordpress 查询,显示特色产品
【发布时间】:2017-04-23 03:21:14
【问题描述】:

我想展示特色产品,但我的查询没有返回任何结果! 我展示了我的一种产品,但没有展示任何东西。

<?php
    $terms = array(
        "post_type" => "product",
        "orderby"     =>  "date",
        "order"       =>  "DESC",
        "posts_per_page" => 12,
        "meta_query" => array(array('key' => '_featured','value' => 'yes'))
         );
        $query = new WP_Query( $terms );
        while($query->have_posts()){
        $query->the_post();
        global $product;
?>

也谢谢

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    使用短代码 [featured_products per_page="12" columns="4"]

    或自定义逻辑为:

    $args = array(
        'post_type' => 'product',
        'meta_key' => '_featured',
        'meta_value' => 'yes',
        'posts_per_page' => 12
    );
    
    $featured_query = new WP_Query( $args );
    
    if ($featured_query->have_posts()) : 
    
        while ($featured_query->have_posts()) : 
    
            $featured_query->the_post();
    
            $product = get_product( $featured_query->post->ID );
    
            // Output product information here
            echo "<pre>";print_r($product);echo "</pre>";
    
        endwhile;
    
    endif;
    
    wp_reset_query(); // Remember to reset
    

    参考这里:http://biostall.com/how-to-display-woocommerce-featureds-product-without-a-shortcode/

    【讨论】:

    • 感谢兄弟短代码有效,但自定义无效!我想要使​​用自定义类型?你有什么想法吗?
    • 尝试启用调试器并查看完全形成的查询stackoverflow.com/questions/2473079/… 或stackoverflow.com/questions/2473079/…
    • 我输入了这段代码,它成功了。最初我也没有得到记录,因为数据库中没有产品被标记为精选。我相信您缺少将特色标志部分添加到 1 。快速编辑任何产品并将复选框标记为“精选”。我使用 woocomerce 来管理产品,这就是您需要将它们作为特色产品并确保您有 print_r 语句来打印产品对象的方式。参考:modernmarketingpartners.com/set-featured-products-woocommerce.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多