我只想在库(插入[gallery]
快捷码)已插入到帖子/页面中。我做了以下工作:
gallery_shortcode($post->ID) ?
$gallery = 1 : $gallery = 0;
但是,这始终设置$gallery = 0
是否有画廊。我是不是用错了?我只想在库(插入[gallery]
快捷码)已插入到帖子/页面中。我做了以下工作:
gallery_shortcode($post->ID) ?
$gallery = 1 : $gallery = 0;
但是,这始终设置$gallery = 0
是否有画廊。我是不是用错了?try :
if (strpos($post->post_content,\'[gallery\') === false){
$gallery = 0;
}else{
$gallery = 1;
}
我在这里对建议的解决方案进行的迭代是我的主题函数中的以下函数。php:
function has_gallery($post_id = false) {
if (!$post_id) {
global $post;
} else {
$post = get_post($post_id);
}
return ( strpos($post->post_content,\'[gallery\') !== false);
}
然后我可以调用模板文件(不关心循环):$has_gallery = has_gallery($post_id) ? true : false;
或更直接:has_gallery($post_id); //will evaluate to true/false
$post_id
是可选的,以下将only 在循环内工作:has_gallery();
简单地使用get_post_gallery()
作用正是这样,大多数主题都在检查帖子是否有图库来打印正确的正文CSS类:
if ( get_post_gallery() ) {
// run your code in here
}
另外,我真的不喜欢在整个内容中搜索任何内容的解决方案,即使字符串函数在PHP中通常非常快,它仍然会带来巨大的性能开销。我有一个wordpress功能来显示用户最近的5条推文,这些推文可以通过两种方式显示,一种是通过在我创建的管理菜单中直接输入用户ID,另一种是通过自定义字段,在这里,我想避免自定义字段,并想从短代码开始,我知道我在短代码方面很弱,因为这是我的第二个与之相关的问题,但这个函数需要短代码这是密码function tweet_fetcher(){ global $post; $doc = new DOMDocument(); if((get_option(\'tweetID\'))!=n