当在ob_start内时,WordPress POST AJAX调用,var_ump($_POST)为空,AJAX响应为空

时间:2021-08-27 作者:OmarAc

我有导航标签enter image description here

当我单击某个选项卡时,我希望它返回特定类型的帖子,下面是其中一个选项卡的html代码:

<ul id="menu-menu-secondaire-blog" class="nav nav-tabs">
<li class="nav-item"">
    <a id="test" href="#articles"  class="nav-link tabs-link border-white active" role="tab" data-toggle="tab">Tous les articles</a>
</li>
这是我的文件:articleTabs。php(包含在我的快捷码回调函数中):

<?php
?>
<ul id="menu-menu-secondaire-blog" class="nav nav-tabs">
    <li class="nav-item" style="/*border-bottom: 1px solid black;*/">
        <!-- class= active: means it is the tab opened by default-->
        <a id="test" href="#articles"  class="nav-link tabs-link border-white active" role="tab" data-toggle="tab">Tous les articles</a>
    </li>

    <li class="nav-item">
        <a href="#palette-europe" class="nav-link tabs-link border-white " role="tab" data-toggle="tab">Palette Europe</a>
    </li>

    <li class="nav-item">
        <a id="tab-transport" href="#transport" class="nav-link tabs-link border-white" role="tab" data-toggle="tab">Transport</a>
    </li>

    <li class="nav-item">
        <a href="#supply-chain" class="nav-link tabs-link border-white" role="tab" data-toggle="tab">Supply Chain</a>
    </li>

    <li class="nav-item">
        <a href="#environnement" class="nav-link tabs-link border-white" role="tab" data-toggle="tab">Environnement</a>
    </li>

    <li class="nav-item">
        <a href="#start-up" class="nav-link tabs-link border-white" role="tab" data-toggle="tab">Start-up</a>
    </li>
</ul>

<?php
    $tab_title = "articles";
    // $tab_title = $_POST["article_href"];
?>
<div class="tab-content" style="gap: 1em !important;">
    <div role="tabpanel" class="tab-pane active tab-content container-fluid d-flex" style="gap: 1em !important; flex-wrap: wrap;" id="<?=$id;?>" >
<?php
    $i =\'\';
        $args = array(
        \'post_type\'=> \'post\',
        \'orderby\'    => \'ID\',
        \'post_status\' => \'publish\',
        \'order\'    => \'DESC\',
        \'posts_per_page\' => 9
        );

        $result = new WP_Query( $args );
        if ( $result-> have_posts() ) :
        while ( $result->have_posts() ) : $result->the_post();

        $category = get_the_category(get_the_ID());

            if ($tab_title === "articles"){
            $post_category =  $category[0]->name;
            $post_title = get_the_title();
            $post_date = get_the_date( \'d M Y\' );
            } elseif ($tab_title === "palette-europe" && $category[0]->name === "Palette Europe"){
                $post_category = $category[0]->name;
                $post_title = get_the_title();
                $post_date = get_the_date( \'d M Y\' );
            } elseif ($tab_title === "transport" && $category[0]->name ==="Transport") {
                $post_category = $category[0]->name;
                $post_title    = get_the_title();
                $post_date     = get_the_date( \'d M Y\' );
            } elseif ($tab_title === "supply-chain" && $category[0]->name ==="Supply Chain") {
                $post_category = $category[0]->name;
                $post_title    = get_the_title();
                $post_date     = get_the_date( \'d M Y\' );
            } elseif ($tab_title === "environnement" && $category[0]->name ==="Environnement") {
                $post_category = $category[0]->name;
                $post_title    = get_the_title();
                $post_date     = get_the_date( \'d M Y\' );
            } elseif ($tab_title === "start-up" && $category[0]->name ==="Start-up") {
                $post_category = $category[0]->name;
                $post_title    = get_the_title();
                $post_date     = get_the_date( \'d M Y\' );
            }


        ?>

            <div class="card margin-top-sections">
                <a class=""  href="#">
                <div class="card-img-top article-bg-card w-100"></div>
                <div class="card-body" style="min-height: 216px">
                    <p class="card-title section-first-title section-first-title margin-05"><?=$post_category?></p>
                    <h3 class="article-card-title margin-05"><?=$post_title?></h3>
                    <p class="article-card-date mt-5 mb-0"><?=$post_date?></p> <!--todo: date: make it flex en bas a gauche-->
                </div>
                </a>
            </div>

<?php
        endwhile;
        the_posts_pagination();      
        ?>
        </div>
        <?php>

        <?php /*else :*/ ?>
            <!--<h3>Pas d\'articles</h3>-->
        <?php endif;
        ?>
        </div>
        <?php
        wp_reset_postdata();
        ?>
因此,我选择使用AJAX获取单击选项卡的href并将其发送到我的前面,下面是我的文件函数中的函数。php,我想在其中发送ajax响应:

function js_scripts (){
wp_enqueue_script( \'cool-stuff\',get_stylesheet_directory_uri() . \'/js/script.js\', array(\'jquery\'), false, false);
wp_localize_script( \'cool-stuff\', \'ajaxurl\', admin_url( \'admin-ajax.php\' ) );}
add_action( \'wp_enqueue_scripts\', \'js_scripts\');


add_action( \'wp_ajax_get_attr_href\', \'get_attr_href\');
add_action( \'wp_ajax_nopriv_get_attr_href\', \'get_attr_href\');
function get_attr_href() {
var_dump($_POST);
ob_start();
include \'articlesTabs.php\';
return ob_get_clean();
}
add_shortcode(\'shortcode-articles\', \'get_attr_href\');
这是我脚本中的jQuery代码。js:查询(文档)。就绪(函数($){

 // ---------------------------------------------------------------------------------------
    $(\'a[data-toggle="tab"]\').on(\'shown.bs.tab\', function (event) {
        console.log($(this).attr(\'href\'));           
        var href = $(this).attr(\'href\');    
        var newData = {
            "action": "get_attr_href",
            "href_post": href
        };

        $.ajax({
            url: ajaxurl,
            type: \'POST\',
            data: newData,
            success: function (response) {
                console.log(\'success\');
            }
        });
    });

});
当我单击选项卡时,我进入检查->;网络和我得到了这样的回应:enter image description here

在我的控制台中获得成功,

我有两个问题:

1-如果查看我的函数get\\u attr\\u href(),我有var\\u dump($\\u POST),它返回空:var(),它应该返回包含AJAX调用返回的值的$\\u POST数组。

2-如果我将$\\u POST放在ob\\u start中,它也不再返回响应。

在这种情况下我能做什么?

提前感谢!

1 个回复
最合适的回答,由SO网友:A. Joahny 整理而成

如果要使用相同的函数,必须从短代码(return)检测ajax调用(echo),如下所示:

function get_attr_href() {
  ob_start();
  var_dump($_POST);
  include \'articlesTabs.php\';
  $html = ob_get_clean();
  if(wp_doing_ajax()){
    echo $html;
    wp_die();
  }
  return $html;
}

相关推荐

Unctions.php中的多个AJAX处理程序函数冲突

我有两个不同的AJAX 在我的网站中加载更多功能。一个用于我的归档页面,第二个用于分类页面。问题是,当两者都AJAX 处理程序函数位于“我的函数”中。php。当只有一个处理程序函数时,后者可以正常工作。两种AJAX操作的JS代码:/* * AJAX Load More for archive page */ $(\'.load-more-archive\').click(function(){ var button = $(this), data