如何在模板外部使用do_shortcode

时间:2012-02-25 23:07:36

标签: php wordpress

全部, 我正在尝试使用do_shortcode函数。我有以下jQuery来调用post函数:

category = $("#past_vendor_category").val();
search_term = $("#past_vendor_name").val();

$.post("http://localhost/wordpress/wp-content/search_past_vendors.php", { category: category, search_term: search_term }, function(results){
    $("#past_vendor_search_results").html(results);
});

然后调用此PHP文件:

<?php
global $shortcode_values, $theLayout;
include "config.php";
$category = mysql_real_escape_string($_POST['category']);
$search_term = mysql_real_escape_string($_POST['search_term']);

$qry = "Select wp_posts.ID, wp_posts.post_name, wp_posts.post_title, custom_vendor_info.city, custom_vendor_info.city
from wp_posts
join custom_vendor_info on wp_posts.ID=custom_vendor_info.post_id
join wp_term_relationships on wp_posts.ID=wp_term_relationships.object_id
where wp_posts.post_title like '%$search_term%' and wp_term_relationships.term_taxonomy_id='$category'";
$result = mysql_query($qry);
$resultrows = mysql_num_rows($result);
$x=0;
if($resultrows>0){
    $show_results = '<br>';
    while($resultset = mysql_fetch_array($result)){
        if($x==0){
        $show_results .= '<div class="vendor_search_result"><span class="vendor_search_result_name"><a href="'.$resultset['post_name'].'">'.$resultset['post_title'].'</a></span><span class"vendor_search_result_button">'.do_shortcode("[button]Text[/button]").'</span>';
        }else{
        $show_results .= '<div class="vendor_search_result"><span class="vendor_search_result_name"><a href="'.$resultset['post_name'].'">'.$resultset['post_title'].'</a></span><span class"vendor_search_result_button">'.do_shortcode("[button]Text[/button]").'</span>';
        $x=0;
        } 
    }
    echo $show_results;
}else{
    echo '<br>Sorry, there were no search results for "<b>'.$search_term.'</b>". Please try again!';
}
?>

此文件不在Wordpress页面内,因此我收到错误消息: 致命错误:在第19行的D:\ My Documents \ xampp \ htdocs \ wordpress \ wp-content \ search_past_vendors.php中调用未定义的函数do_shortcode()

知道如何在Wordpress页面之外使用do_shortcode吗?

由于

2 个答案:

答案 0 :(得分:3)

do_shortcode函数取决于wordpress的全局状态。在您的示例代码中,您不加载wordpress,它不可用。

我建议你自己创建一个提供Ajax接口的插件,然后由默认的wordpress Ajax后端处理。

请参阅5 tips for using AJAX in WordPressoriginal article archived),了解如何正确地将PHP函数绑定到Ajax请求。

答案 1 :(得分:2)

  

知道如何在Wordpress页面之外使用do_shortcode吗?

获取来源http://core.trac.wordpress.org/browser/tags/3.3.1/wp-includes/shortcodes.php