【问题标题】:callback function for an ajax form in wordpresswordpress中ajax表单的回调函数
【发布时间】:2018-01-05 14:57:25
【问题描述】:

我使用 jquery 进行 wordpress 高级搜索。 当用户单击搜索按钮时,通过使用 jquery 我获取所有搜索参数并使用 $.post() 函数发送到 admin-ajax.php ... 我在 functions.php 文件中创建一个函数并接收所有搜索值并编写 wp_query 以获取 wp 帖子。

$args = array(
'category_name' => '3d');$query = new WP_Query($args);

if ($query->have_posts()) {
while ($query->have_posts()) {
    $query->the_post();
    echo '<a href="'. the_permalink() .'">test</a>';
}
wp_reset_postdata();

}

在上面的代码中,当我打印一个标签时,输出是这样的:

http://localhost/site/%d8%af%d8%a7%d9%86%d9%84%d9%88%d8%af-%d9%81%db%8c%d9%84%d9%85-zootopia/test0

问题 1:test 是一个链接,但没有任何 href!链接的href像字符串一样打印在链接之外! 问题2:所有输出值后自动打印“0”字符即使php中的空回显也会打印“0”字符! 请帮助我,谢谢...

【问题讨论】:

    标签: php jquery ajax wordpress search


    【解决方案1】:

    你需要用这个替换代码。因为您在 echo 内部使用的是 the_permalink() 而不是 get_the_permalink()

    https://developer.wordpress.org/reference/functions/get_the_permalink/ https://developer.wordpress.org/reference/functions/the_permalink/

    $args = array('category_name' => '3d');
    $query = new WP_Query($args);
    
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            echo '<a href="'. get_the_permalink() .'">test</a>';
        }
        wp_reset_postdata();
    }
    

    【讨论】:

    • 非常感谢...但是“0”字符是什么?这出现在从服务器到 jquery 的所有响应中
    • 你需要像这样在函数末尾添加 die() 方法: function yourFunction(){ //在这里写代码... die(); };
    • 帮我用正确的查询替换 $post->ID() ?像这样:get_post_meta($post->ID, 'movie250',true)
    • 你在哪里使用 $post->ID().
    • 您可以使用 ajax 发送帖子 ID,例如: 和 php 代码如下:get_post_meta( $_POST['post_id'], 'movie250',true)
    猜你喜欢
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2015-02-03
    • 1970-01-01
    相关资源
    最近更新 更多