首页 > 解决方案 > 随机产品未显示我包含在 posts_per_page 中的帖子数量

问题描述

随机产品未显示我包含在 posts_per_page 中的帖子数量

这是我用来称呼产品的标准

$cpsc_args = array(
    'post_type' => 'product',
    'posts_per_page' => 8,
    'order' => 'desc',
    'orderby' => 'rand',
    'tax_query' => array(
        array(
            'taxonomy' => 'pwb-brand',
            'field'    => 'term_id',
            'terms'    => $cpsc_term_id_display,
    'operator' => 'IN'
        )
    )
);
$cpsc_query = new WP_Query( $cpsc_args );

现在产品的结果数量有时会随机显示 7 个产品或 8 个有时或 9 个每次刷新。

这是我循环插件的插件文件代码。

<?php
/*
    Plugin Name: Sultan Custom Brands
    Description: This will Display All the Posts or any Post type as you want.
    Version: 1.0
    Author: Sultan Gillani
    Author URI: https://nya.smyckendahls.se/
    Text Domain: sultan-cpsc-plugin
*/
//include_once('functions.php');

function form_create($attr){
    $cpsc_terms = get_terms( array(
    'taxonomy' => 'pwb-brand',
    'hide_empty' => false,
    ) );

    $attr = shortcode_atts(array(
        "cpsc_posts_per_page" => -1,
        "cpsc_order" => "DESC",
        "cpsc_orderby" => "date",
        "cpsc_terms_show" => $cpsc_terms[0]->slug,
    "cpsc_columns" => 4
    ),$attr);

    $cpsc_term_id = get_term_by( 'slug', $attr['cpsc_terms_show'], 'pwb-brand');

    $cpsc_term_id_display = $cpsc_term_id->term_id;

    $cpsc_args = array(
        'post_type' => 'product',
        'posts_per_page' => $attr['cpsc_posts_per_page'],
        'order' => $attr['cpsc_order'],
        'orderby' => $attr['cpsc_orderby'],
        'tax_query' => array(
            array(
                'taxonomy' => 'pwb-brand',
                'field'    => 'term_id',
                'terms'    => $cpsc_term_id_display,
        'operator' => 'IN'
            )
        )
    );

    $cpsc_query = new WP_Query( $cpsc_args );

    ?>
    <div class="woocommerce columns-<?php echo $attr['cpsc_columns']; ?>">

    <?php
    wc_get_template_part('loop/loop','start');
    if ( $cpsc_query->have_posts() ) {
        while ( $cpsc_query->have_posts() ) {

        $cpsc_query->the_post();

        wc_get_template_part('content','product');
        }
        // Restore original Post Data
        wp_reset_postdata();
    }
    wc_get_template_part('loop/loop','end');
    ?>

    </div>
    <?php
}

add_shortcode('cpsc_code','form_create');

function cpsc_vc_element(){


    $brand_arr = [];
    $cpsc_terms_data = get_terms( array(
    'taxonomy' => 'pwb-brand',
    'hide_empty' => false,
    ) );
    $brand_arr['Select The Brand'] = '';
    foreach($cpsc_terms_data as $cpsc_term_data){
    $brand_arr[$cpsc_term_data->name] =$cpsc_term_data->slug;
    }

    vc_map(
    array(
        'name' => __('Brand Query Builder'),
        'base' => 'cpsc_code',
        'category' => __('Test Shortcodes'),
        "icon" => "icon-wpb-woocommerce", // Simply pass url to your icon here
        "content_element" => true,
        'admin_enqueue_css' => array('/wp-content/plugins/brand_plugin/cpsc.css'),
        'params' => array(
        array(
            "type" => "number",
            "holder" => "div",
            "class" => "cpsc_plugin_posts_per_page",
            "heading" => __( "Posts Per Page", "sultan-cpsc-plugin" ),
            "param_name" => "cpsc_posts_per_page",
            "value" => "0",
            "description" => __( "Enter the number of post to display.", "sultan-cpsc-plugin" )
        ),

        array(
            "type" => "dropdown",
            "holder" => "div",
            "class" => "cpsc_plugin_order",
            "heading" => __( "Order", "sultan-cpsc-plugin" ),
            "param_name" => "cpsc_order",
            'value' => array(
               __( 'Orber By',  "sultan-cpsc-plugin"  ) => '',
               __( 'Ascending',  "sultan-cpsc-plugin"  ) => 'ASC',
               __( 'Descending',  "sultan-cpsc-plugin"  ) => 'DESC',
             ),
            "description" => __( "Order Your Post", "sultan-cpsc-plugin" )
        ),

        array(
            "type" => "dropdown",
            "holder" => "div",
            "class" => "cpsc_plugin_orderby",
            "heading" => __( "Order By", "sultan-cpsc-plugin" ),
            "param_name" => "cpsc_orderby",
            'value' => array(
               __( 'Select The Orber With',  "sultan-cpsc-plugin"  ) => '',
               __( 'Date',  "sultan-cpsc-plugin"  ) => 'date',
               __( 'ID',  "sultan-cpsc-plugin"  ) => 'ID',
               __( 'About the Author',  "sultan-cpsc-plugin"  ) => 'author',
               __( 'Title',  "sultan-cpsc-plugin"  ) => 'title',
               __( 'Modified',  "sultan-cpsc-plugin"  ) => 'modified',
               __( 'Random',  "sultan-cpsc-plugin"  ) => 'rand',
               __( 'Comment Count',  "sultan-cpsc-plugin"  ) => 'comment_count',
               __( 'Menu Order',  "sultan-cpsc-plugin"  ) => 'menu_order',
               __( 'Menu Order & title',  "sultan-cpsc-plugin"  ) => 'menu_order title',
             ),
            "description" => __( "Order by the criteria.", "sultan-cpsc-plugin" )
        ),

        array(
            "type" => "dropdown",
            "holder" => "div",
            "class" => "cpsc_plugin_terms_show",
            "heading" => __( "Brands", "sultan-cpsc-plugin" ),
            "param_name" => "cpsc_terms_show",
            'value' => $brand_arr,
            "description" => __( "All Brands", "sultan-cpsc-plugin" )
        ),

        array(
            "type" => "textfield",
            "holder" => "div",
            "class" => "cpsc_plugin_columns",
            "heading" => __( "Columns", "sultan-cpsc-plugin" ),
            "param_name" => "cpsc_columns",
            "value" => "4",
            "description" => __( "", "sultan-cpsc-plugin" )
        ),

        )
    )
    );
}

add_action('vc_before_init','cpsc_vc_element');

?> 

标签: wordpresswoocommercevisual-composer

解决方案


var_dump 你的:

$cpsc_terms[0]->slug

我的问题是每个页面请求的 slug 都不同?如果是这样,查询将基于每个请求的不同 term_id(如果是随机的)。而 term_id 1 可能有 7 个相关的帖子,其中 term_id 2 有 9 个相关的帖子,等等。因此显示的帖子数。


推荐阅读