Wordpress functions.php中的WP_Query返回空查询(对于自定义短代码)
我正在为一个ChildThemes编写一个自定义的短代码,我已经将它放入我的childThemes functions.php中。代码如下:Wordpress functions.php中的WP_Query返回空查询(对于自定义短代码),wordpress,shortcode,Wordpress,Shortcode,我正在为一个ChildThemes编写一个自定义的短代码,我已经将它放入我的childThemes functions.php中。代码如下: function get_featured_video(){ $video_query = new WP_Query('category_name=videos&order=ASC'); $videoPath = "/"; $videoText = ""; while ($video_query->have_posts()){ $v
function get_featured_video(){
$video_query = new WP_Query('category_name=videos&order=ASC');
$videoPath = "/";
$videoText = "";
while ($video_query->have_posts()){
$video_query->the_post();
$featured = get_post_meta($post->ID, "vid_feature", $single = true);
if(strtolower($featured)=='yes'){
$videoPath = get_permalink($post->ID);
$content = strip_tags($post->post_content);
$contentArr = str_word_count($content,1);
if(count($contentArr>50)){
$videoText = join(" ",array_slice($contentArr,0,50));
$videoText .= " <a href='$link'><read more></a>";
} else {
$videoText = $content;
}
break;
}
}
$returnStr = "<h1><a href='$videoPath'>You've Got to See This!</a></h1>\n";
$returnStr .= $videoText;
return $returnStr;
}
add_shortcode('getfeaturedvideo','get_featured_video');
函数获取特色视频(){
$video\u query=new WP\u query('category\u name=videos&order=ASC');
$videoPath=“/”;
$videoText=“”;
而($video\u query->have\u posts()){
$video_query->the_post();
$featured=get\u post\u meta($post->ID,“vid\u feature”,$single=true);
如果(strtolower($featured)=“是”){
$videoPath=get\u permalink($post->ID);
$content=strip\u标签($post->post\u内容);
$contentArr=str\u word\u count($content,1);
如果(计数($contentArr>50)){
$videoText=join(“,数组_切片($contentArr,0,50));
$videoText.=“”;
}否则{
$videoText=$content;
}
打破
}
}
$returnStr=“\n”;
$returnStr.=$videoText;
return$returnStr;
}
添加快捷码(“getfeaturedvideo”、“get_特色视频”);
我遇到的问题是,它返回的是一个空白查询。我知道在视频类别中有一个帖子。我从未在functions.php中使用过WP_查询。是否需要使用其他方法?在函数顶部尝试声明:
global $post;
在函数顶部,尝试声明:
global $post;
是的。意识到这一点之前,我忘了张贴。谢谢你的回答。谢谢你的回答,我也被困在这个问题上了。是的。意识到这一点之前,我忘了张贴。谢谢你的回答。谢谢你的回答,我也被困在这个问题上了。