我在Wordpress中创建了一个自定义帖子“新闻”,我想搜索帖子并在没有页面重新加载的情况下对它们进行分页。
I.m使用$ wpdb类,现在工作正常但是当我尝试进行ajax调用时,我没有得到任何结果。
以下是我遇到的错误:
<b>Notice</b>: Undefined variable: parent_slug in
<b>C:\xampp\htdocs\website\wp-content\themes\website\includes\news\output_more-news.php</b> on line <b>14</b>
<b>Fatal error</b>:
Call to undefined function get_terms() in <b>C:\xampp\htdocs\website\wp-content\themes\website\includes\news\output_more-news.php</b> on line <b>15</b>
这是我的ajax代码: jQuery(document).ready(function(){
jQuery('#news_form').on('submit', function(e) {
var
that = jQuery(this),
url = "http://localhost/website/wp-content/themes/website/includes/news/output_more-news.php",
type = that.attr('method'),
data = {};
that.find('[name]').each(function(index,value){
var
that = jQuery(this),
name = that.attr('name'),
value = that.val();
data[name]= value;
console.log(data);
});
jQuery.ajax({
url: url,
type: type,
data: data,
beforeSend : function (){
//do something like loading gif
},
success: function(data){
alert("form submited");
console.log(data);
},
error:function(){
alert("error");
}
});//ajax call
e.preventDefault()
});
这是output_more-news.php
$search_term = "%" . strtolower($_POST['name_search'])."%";
if(!empty($_POST['archive-dropdown'])){
$archive_selected = $_POST['archive-dropdown'];
}else{
$archive_selected = "";
}
$taxonomy = ucfirst($parent_slug);
foreach(get_terms('news_categories') as $term){
if($term->slug == $parent_slug){
$term_ID = $term->term_id;
}
}
$home_url = get_home_url();
$site_directory_url = get_template_directory_uri();
$archive_selected = $_POST['archive-dropdown'];
$archive_selected = str_replace("$home_url","",$archive_selected );
$archive_selected = str_replace("?post_type=news","",$archive_selected );
$archive_date = preg_split("/[\/]+/",$archive_selected );
$archive_month = $archive_date[2];
$archive_year = $archive_date[1];
global $wpdb;
$more_news_query =
"
SELECT $wpdb->posts.ID
FROM $wpdb->posts
INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
AND $wpdb->term_taxonomy.taxonomy = 'news_categories'
AND $wpdb->term_taxonomy.term_id IN ($term_ID)
WHERE $wpdb->posts.post_title LIKE '$search_term'
";
if(!empty($archive_selected)){
$more_news_query .=
"
AND month($wpdb->posts.post_date) = '$archive_month'
AND year($wpdb->posts.post_date) = '$archive_year'
";
}
$more_news_query .=
"
AND $wpdb->posts.post_type = 'news'
AND $wpdb->posts.post_status = 'publish'
ORDER BY $wpdb->posts.post_date
";
$count_results = "SELECT COUNT(1) FROM (${more_news_query}) AS combined_table";
$total = $wpdb->get_var($count_results);
$items_per_page = get_option( 'posts_per_page' );
$paged = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$offset = ( $paged * $items_per_page ) - $items_per_page;
$max_num_pages = ceil($total / $items_per_page);
$more_news = $wpdb->get_results( $more_news_query . " LIMIT ${offset}, ${items_per_page}" );
$prev_link = $paged - 1;
if($prev_link <= 0){
$prev_link = 1;
}
$nextlink = $paged + 1;
if($nextlink >= $max_num_pages){
$nextlink = $max_num_pages;
}
echo"<div class='panel-group' id='accordion'>
<div class='panel panel-default'>
<div class='panel-heading'>
<h4 class='panel-title''>
<a class='accordion-toggle' data-toggle='collapse' data-parent='#accordion' href='#collapseMore'>
<span>More News</span>
</a>
<a class='accordion-toggle' data-toggle='collapse' data-parent='#accordion' href='#collapseMore'>
<i class='pull-right fa fa-plus-circle' aria-hidden='true'></i>
</a>
</h4>
</div>";
echo "<div id='collapseMore' class='panel-collapse collapse '>";
echo "<div class='panel-body'>";
echo "<div class='search_bar clearfix'>";
echo "<div class='col-lg-10 col-md-9 col-sm-8 col-xs-7'>
<form id='news_form' method='post' action='' class='clearfix'>
<div class='form_fields_container col-md-7'>
<span class='btns cut_corner_btns news_search_label'>News</span><input placeholder='Search' type='text' name='name_search'>
<i class='fa fa-search' aria-hidden='true'></i>";
?>
</div>
<div class='form_fields_container select_container col-md-5'>
<span class="btns cut_corner_btns news_search_label"><?php echo strtoupper($parent_slug);?> archives</span>
<select name="archive-dropdown" >
<option value=""></option>
<?php wp_get_archives( array( 'news_categories' => $taxonomy, 'type' => 'monthly', 'format' => 'option', 'post_type'=>'news', 'show_post_count' => false ) ); ?>
</select>
</div>
</form>
</div>
<?php
echo "<div class='col-lg-2 col-md-3 col-sm-4 col-xs-5 news-pagination-container'>";
echo"<div class='news-pagination pull-right'>
<span>Page</span>
<a class='page-numbers current' href='$home_url/$parent_slug/news/?cpage=$paged'>$paged</a>
<span> of</span>
<a class='page-numbers' href='$home_url/$parent_slug/news/?cpage=$max_num_pages'>$max_num_pages </a>";
echo "<a class='prev page-numbers' href='$home_url/$parent_slug/news/?cpage=$prev_link'><i class='fa fa-angle-left' aria-hidden='true'></i></a>";
echo" <a class='next page-numbers' href='$home_url/$parent_slug/news/?cpage=$nextlink'><i class='fa fa-angle-right' aria-hidden='true'></i></a>";
echo "</div>";
echo "</div>";
echo "</div>";
if(!empty($total)) :
foreach($more_news as $news) {
$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($news->ID, 'thumbnail-size', true);
$thumb_url = $thumb_url_array[0];
$date = get_the_date("Y-m-d ",$news->ID);
$title = get_the_title($news->ID);
$content = get_the_content($news->ID);
$content = substr($content,0,30);
$permalink = get_the_permalink();
echo"<div class='col-md-6 col-sm-12 col-xs-12 more_post_container'>";
echo"<div class='more_post_thumb col-xs-4'><img class='img-responsive' src='$thumb_url'/></div>";
echo"<div class='more_news_content col-xs-8'>
<p>$date</p>
<p class='color-$parent_slug '>$title</p>
<p class=''>$content...</p>
<a class='more_post_permalink' href='$permalink'>more</a>
</div>";
echo "</div>";
}
echo"</div>";
echo"</div>";
else:
echo "no results found!";
endif;
echo"<div id='response'></div>";
echo"</div>";
echo"</div>";
?>
任何帮助都会很受欢迎。
提前致谢
答案 0 :(得分:0)
您有2种方法可以解决错误
在output_more-news.php中添加require '../../../wp-load.php'
,这将在WordPress核心文件中手动加载,但这是一个糟糕的方法。
记录正确的方法here