在调用php函数通过ajax显示我的帖子之后似乎无法访问$ wpdb
hi I wanted to have a list that displays archives of posts group by year when users click on the years, the posts will be displayed
I am using ajax to call a functions.php, and within there's a function will grab the posts, but I can't seem to access the $wpdb ?
Thanks a lot!
html:
<ul id="years">
<?php
$months = $wpdb->get_results("SELECT DISTINCT YEAR( post_date ) AS year,post_title as title, ID as post_id, COUNT( id ) as post_count FROM $wpdb->posts WHERE post_status = 'publish' and post_date <= now( ) and post_type = 'post' GROUP BY year ORDER BY post_date DESC");
foreach($months as $month) : ?>
<li>
<a href="" onClick="year_to_post_titles(<?php echo $month->year; ?>)">
<?php if(in_category("photography",$month->post_id)){
echo $month->year;
} ?>
</a>
</li>
<?php endforeach; ?>
</ul>
ajax:
<script>
function year_to_post_titles(year){
var find_titles="find_titles";
//request ajax
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
//state change
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4&& xmlhttp.status==200){
document.getElementById("work_items").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","<?php bloginfo(template_directory) ?>/functions.php?func=find_titles&y="+year,true);
xmlhttp.send()
}
</script>
functions.php:
<?php
$which_func=$_GET["func"];
if(function_exists($which_func)){
find_titles();
};
function find_titles(){
global $wpdb;
$which_year=$_GET["y"];
$titles = $wpdb->get_results("SELECT DISTINCT YEAR( post_date ) AS year,post_title as title, ID as post_id, COUNT( id ) as post_count FROM $wpdb->posts WHERE post_status = 'publish' and post_date <= now( ) and post_type = 'post' GROUP BY year ORDER BY post_date DESC");
foreach($titles as $var_title){
echo "<li><a href=''>";
if(in_category("photography",$var_title->post_id)){
echo $var_title->title;
}
echo "</a></li>";
}
}
?>
嗨我希望有一个列表,按年份显示帖子档案 当用户点击年份时, 将显示帖子 p>
我使用ajax来调用functions.php,并且在其中有一个函数 将获取帖子,但我似乎无法访问$ wpdb? p>
非常感谢! p>
html: p>
&lt; ul id =“years” &gt;
&lt;?php
$ months = $ wpdb-&gt; get_results(“SELECT DISTINCT YEAR(post_date)AS year,post_title as title,ID as post_id,COUNT(id)as post_count FROM $ wpdb-&gt; posts WHERE post_status ='publish'和post_date&lt; = now()和post_type ='post'GROUP BY年ORDER BY post_date DESC“);
foreach($ month as $ month):?&gt;
&lt; li&gt;
&lt; ; a href =“”onClick =“year_to_post_titles(&lt;?php echo $ month-&gt; year;?&gt;)”&gt;
&lt;?php if(in_category(“photography”,$ month-&gt; post_id) ){
echo $ month-&gt; year;
}?&gt;
&lt; / a&gt;
&lt; / li&gt;
&lt;?php endforeach; ?&GT;
&LT; / UL&GT;
代码> PRE>
AJAX: p>
&LT;脚本&GT;
function year_to_post_titles(year){
var find_titles =“find_titles”;
//请求ajax
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject(“Microsoft。 XMLHTTP“);
}
//状态更改
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4&amp;&amp; xmlhttp.status == 200){
document.getElementById(”work_items“ ).innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open(“GET”,“&lt;?php bloginfo(template_directory)?&gt; /functions.php?func = find_titles&amp; y =”+ year, 真);
xmlhttp.send()\ N}
&LT; /脚本&GT;
代码> PRE>
的functions.php: p>
&lt;?php
$ which_func = $ _ GET [“func”];
if(function_exists($ which_func)){
find_titles();
};
函数find_titles(){
global $ wpdb;
$ which_year = $ _ GET [“y”];
$ titles = $ wpdb-&gt; get_results(“SELECT DISTINCT YEAR(post_date)AS年份,post_title为标题,ID为post_id,COUNT( id)作为post_count FROM $ wpdb-& gt;帖子WHERE post_status ='publish'和post_date&lt; = now()和post_type ='post'GROUP BY年ORDER BY post_date DESC“);
foreach($ titles as $ var_title){
echo”&lt; li&gt; &lt; a href =''&gt;“;
if(in_category(”photography“,$ var_title-&gt; post_id)){
echo $ var_title-&gt; title;
}
echo”&lt; / a&gt; &lt; / li&gt;“;
}
}
?&gt;
code> pre>
div>
When you call functions.php
via AJAX, the file does not have a global variable called $wpdb
. That's no wonder, because normally functions.php
does not care about this. Instead you should register an AJAX PHP callback function within wordpress. Then $wpdb
is available.
Furthermore, you shouldn't do this :
<?php
$which_func=$_GET["func"];
if(function_exists($which_func)){
$which_func();
};
?>
If you do it, an user will be able to call any existing functions (like phpinfo() for exemple, but it could be worst with a little bit of imagination). It's a huge security breach.