Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
所有产品上的wordpress循环_Wordpress_Woocommerce - Fatal编程技术网

所有产品上的wordpress循环

所有产品上的wordpress循环,wordpress,woocommerce,Wordpress,Woocommerce,我试图在我店里的每一种产品上循环使用。这是为了修改“相关产品”功能。但它只运行了6次,而我有49种产品 你能帮我找出原因吗 谢谢 if ( is_singular('product') ) { global $post; // get categories $terms = wp_get_post_terms( $post->ID, 'product_cat' ); $title = get_the_title($post); $pattern

我试图在我店里的每一种产品上循环使用
。这是为了修改“相关产品”功能。但它只运行了6次,而我有49种产品

你能帮我找出原因吗

谢谢

if ( is_singular('product') ) {
    global $post;
    // get categories
    $terms = wp_get_post_terms( $post->ID, 'product_cat' );

    $title = get_the_title($post);


    $pattern = '/[^a-zA-Z0-9 ]/';
    // $pattern = '/ /';
    $matches = array();
    $dummy = preg_match($pattern, $title, $matches);
    $posi = strpos($title, $matches[0]);
    // print_r($posi);
    $productname = substr($title, 0, $posi-1);
    // print_r($productname);
    $productcolor = substr($title, $posi+3);
    // print_r($productcolor);



    foreach ( $terms as $term ) $cats_array[] = $term->term_id;
    $query_args = array( 'orderby' => 'title',
                         'post__not_in' => array( $post->ID ),
                         'posts_per_page' => 200,
                         'no_found_rows' => 10,
                         'post_status' => 'publish',
                         'post_type' => 'product',
                         'tax_query' => array(
                                                array(
                                                    'taxonomy' => 'product_cat',
                                                    'field' => 'id',
                                                    'terms' => $cats_array
                                                )
                                            )
                        );
    $r = new WP_Query($query_args);


    if ($r->have_posts()) { ?>

        <div class="related products">
            <h2><?php _e( 'Related Products', 'woocommerce' ); ?></h2>

            <?php woocommerce_product_loop_start(); ?>

                <?php 
                    $counter = 0;
                    while ($r->have_posts()) : 
                        $r->the_post(); 
                        global $product; 
                        $counter = $counter + 1;


                        wc_get_template_part( 'content', 'product' ); 


                    endwhile; // end of the loop. 
                    print_r($counter)
                ?>

            <?php woocommerce_product_loop_end(); ?>

        </div>

        <?php

        wp_reset_query();
    }
}
if(单数('product')){
全球$员额;
//获取类别
$terms=wp_get_post_terms($post->ID,'product_cat');
$title=获取标题($post);
$pattern='/[^a-zA-Z0-9]/';
//$pattern='/';
$matches=array();
$dummy=preg_match($pattern,$title,$matches);
$posi=strpos($title,$matches[0]);
//印刷费($posi);
$productname=substr($title,0,$posi-1);
//打印($productname);
$productcolor=substr($title,$posi+3);
//打印(productcolor);
foreach($terms as$term)$cats\u数组[]=$term->term\u id;
$query_args=数组('orderby'=>'title',
'post\u not\u in'=>数组($post->ID),
“每页帖子数”=>200,
“未找到行”=>10,
“发布状态”=>“发布”,
“post_类型”=>“产品”,
“tax_query”=>数组(
排列(
“分类法”=>“产品分类”,
“字段”=>“id”,
“术语”=>$cats\u数组
)
)
);
$r=新的WP\u查询($Query\u args);
如果($r->have_posts()){?>

不确定它是否适用,但我建议只修改相关的产品模板,可以在这里找到:

为此:

  • 在主题根目录中创建一个“wooocommerce”文件夹
  • 从/wp content/plugins/woocommerce/woocommerce/templates/single product/related.php复制related.php文件
  • 新文件的位置应该是/wp content/your theme/wooocmerce/single product/related.php
  • 对所述文件进行所需的修改

  • 同样,这完全取决于您试图实现的目标。我非常乐意编辑您的代码,但需要知道您希望修改什么,以便不删除任何重要内容。

    我实际上很难转换相关的.php文件。我改变了策略,并直接在functions.php中编写了一些代码。感谢您的帮助lp!