Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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
Php WordPress AJAX函数返回中的回显短代码_Php_Ajax_Wordpress - Fatal编程技术网

Php WordPress AJAX函数返回中的回显短代码

Php WordPress AJAX函数返回中的回显短代码,php,ajax,wordpress,Php,Ajax,Wordpress,我已经在这里和网上搜索过了,但似乎无法实现这一点——希望你们都能提供帮助 我正在尝试在分类页面上为WooCommerce设置产品过滤器(如根据颜色过滤产品等) 我有ajax,但我有一个短代码,我想为每个产品显示,但这不起作用-有什么想法如何让它显示 代码如下: PHP JS PHP获取帖子 // Script for getting posts function ajax_filter_get_posts( $taxonomy, $term ) { ob_start(); global $wo

我已经在这里和网上搜索过了,但似乎无法实现这一点——希望你们都能提供帮助

我正在尝试在分类页面上为WooCommerce设置产品过滤器(如根据颜色过滤产品等)

我有ajax,但我有一个短代码,我想为每个产品显示,但这不起作用-有什么想法如何让它显示

代码如下:

PHP

JS

PHP获取帖子

// Script for getting posts
function ajax_filter_get_posts( $taxonomy, $term ) {
ob_start();
global  $woocommerce, $product;

  // Verify nonce
  if( !isset( $_POST['afp_nonce'] ) || !wp_verify_nonce( $_POST['afp_nonce'], 'afp_nonce' ) )
    die('Permission denied');

  $taxonomy = $_POST['taxonomy'];
  $term = $_POST['term'];

  $term = str_replace("\\", '', $term);

  // WP Query
  $args = array(
    'post_type' => 'product',
    'post_status'    => 'publish',
    'posts_per_page' => -1,
      );

  if ($term == "''") {
}
  else {
    $args = array(
      'tax_query' => array(
      array(
          'taxonomy' => $taxonomy,
          'terms' => array($term),
          'field' => 'slug',
          'operator' => 'IN'
      ),
  )
      );
  }

?>
<h1> <?php echo $term ?> </h1>
<?php
  $query = new WP_Query( $args );

  if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

<li <?php wc_product_class(); ?>>
<?php echo do_shortcode('[product_shortcode]'); ?>
</li>


<?php

 ?>

  <?php endwhile; ?>
  <?php else: ?>
    <h2>No posts found</h2>
  <?php endif;

  die();
  return ob_get_clean();
}

add_action('wp_ajax_filter_posts', 'ajax_filter_get_posts');
add_action('wp_ajax_nopriv_filter_posts', 'ajax_filter_get_posts');

WordPress Ajax调用无法访问整个WordPress环境,这就是为什么您的短代码不能工作的原因。与其直接调用短代码,不如调用其回调。请参阅更多详细信息。

谢谢您的回答-您所说的现在有意义,但不确定如何调用其回调-我尝试添加代码(请参见更新下的问题-将比注释中更易于阅读)-这是您的意思吗?您能分享您的短码函数
[product\u shortcode]
here?我使用的是Divi主题,它输出了它的一个“布局”:--function product_shortcode($moduleid){return do_shortcode('[et_pb_section global_module=“79”][/et_pb_section];}add_showmodule('product_shortcode');我太不确定这个Divi短代码了,你能不能试着用
替换
,所以我做了一个测试短代码,它只回显了一个字符串,结果成功了。所以这一定是短代码的问题(不知道如何解决这个问题!
    jQuery(document).ready(function($) {

        $(".loading").hide();

var taxonomy = [];
var terms = [];

         $('.filter-option input').click( function(event) {

                 var taxonomy_idx = $.inArray($(this).closest(".filter-title-wrapper").attr('data-attribute'), taxonomy);
                 if (taxonomy_idx == -1) {
             taxonomy.push($(this).closest(".filter-title-wrapper").attr('data-attribute'));
             } else {
         taxonomy.splice(taxonomy_idx, 1);
            }

            var terms_idx = $.inArray($(this).val(), terms);
            if (terms_idx == -1) {
            terms.push($(this).val());
        } else {
        terms.splice(terms_idx, 1);
     }



            // Prevent default action - opening tag page
            if (event.preventDefault) {
                event.preventDefault();
            } else {
                event.returnValue = false;
            }

            // Get tag slug from title attirbute
                    var selecetd_taxonomy = taxonomy.toString();;


                    var selected_term = terms.toString();
                    var selected_term_speach = '\'' + selected_term.split(',').join('\',\'') + '\'';
                    console.log(selecetd_taxonomy);
                    console.log(selected_term_speach);

            // After user click on tag, fade out list of posts
            $('.products').fadeOut();
                    $(".loading").fadeIn();

            data = {
                action: 'filter_posts', // function to execute
                afp_nonce: afp_vars.afp_nonce, // wp_nonce
                taxonomy: selecetd_taxonomy, // selected tag
                            term: selected_term_speach,
                };

            $.post( afp_vars.afp_ajax_url, data, function(response) {
                        $(".loading").fadeOut();
                if( response ) {
                    // Display posts on page
                    $('.products').html( response );
                    // Restore div visibility
                    $('.products').fadeIn();

                };
            });
        });
    });
// Script for getting posts
function ajax_filter_get_posts( $taxonomy, $term ) {
ob_start();
global  $woocommerce, $product;

  // Verify nonce
  if( !isset( $_POST['afp_nonce'] ) || !wp_verify_nonce( $_POST['afp_nonce'], 'afp_nonce' ) )
    die('Permission denied');

  $taxonomy = $_POST['taxonomy'];
  $term = $_POST['term'];

  $term = str_replace("\\", '', $term);

  // WP Query
  $args = array(
    'post_type' => 'product',
    'post_status'    => 'publish',
    'posts_per_page' => -1,
      );

  if ($term == "''") {
}
  else {
    $args = array(
      'tax_query' => array(
      array(
          'taxonomy' => $taxonomy,
          'terms' => array($term),
          'field' => 'slug',
          'operator' => 'IN'
      ),
  )
      );
  }

?>
<h1> <?php echo $term ?> </h1>
<?php
  $query = new WP_Query( $args );

  if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

<li <?php wc_product_class(); ?>>
<?php echo do_shortcode('[product_shortcode]'); ?>
</li>


<?php

 ?>

  <?php endwhile; ?>
  <?php else: ?>
    <h2>No posts found</h2>
  <?php endif;

  die();
  return ob_get_clean();
}

add_action('wp_ajax_filter_posts', 'ajax_filter_get_posts');
add_action('wp_ajax_nopriv_filter_posts', 'ajax_filter_get_posts');
add_action( 'init', function() {
  ps_register_shortcode_ajax( 'ajax_filter_get_posts', 'ajax_filter_get_posts' ); 
} );

function ps_register_shortcode_ajax( $callable, $action ) {

  if ( empty( $_POST['action'] ) || $_POST['action'] != $action )
    return;

  call_user_func( $callable );
}