小编Jay*_*vat的帖子

WC_Product_Query无法使用have_posts()

我试图像使用自定义帖子类型一样遍历我的WooCommerce产品.但由于某种原因,这种方法不起作用.我收到的错误与我使用有关have_posts().我究竟做错了什么?

错误

未捕获错误:在数组上调用成员函数have_posts()

我的代码

<?php
 $query = new WC_Product_Query( array(
     'limit' => 10,
     'orderby' => 'date',
     'order' => 'DESC'
 ) );

 $products = $query->get_products();

 if( $products->have_posts() ) {
    while( $products->have_posts() ) {
      $products->the_post();
      echo the_permalink();
    }
} ?>
Run Code Online (Sandbox Code Playgroud)

更新

我发现使用foreach循环确实如下所示;

<?php
foreach( $products as $product ) {
    echo $product->get_title();
} ?>
Run Code Online (Sandbox Code Playgroud)

但是我仍然想知道为什么这种方法无效 have_posts()

wordpress loops product woocommerce

6
推荐指数
1
解决办法
485
查看次数

在块编辑器(WordPress 5.0.2)中允许内部块的节块

我正在创建一个自定义块名称部分,希望通过添加具有不同css属性的设置使其更加强大。

但是像列块一样,卡在此自定义块中的允许块上。

这是我所做的:

// All blocks located here
if( !defined( 'WP_BLOCKS_URL' ) ) {
    define( 'WP_BLOCKS_URL', get_template_directory_uri() . '/blocks/' );
}

// Register Gutenberg blocks
add_action( 'init', 'wp_register_gutenberg_blocks_assets' );
function wp_register_gutenberg_blocks_assets() {

    if( ! function_exists( 'register_block_type' ) ) {
        // Gutenberg is not active.
        return;
    }

    // Register 
    register_block_type( 'custom/section', array(
        'editor_script' => 'wp-section-block-script',
    ) );
}

// Manage editor scripts
add_action( 'enqueue_block_editor_assets', 'wp_custom_gutenberg_scripts' );
function wp_custom_gutenberg_scripts() {

    if( ! function_exists( 'register_block_type' ) ) {
        // Gutenberg is not active. …
Run Code Online (Sandbox Code Playgroud)

wordpress wordpress-gutenberg

3
推荐指数
1
解决办法
1906
查看次数