php do_shortcode 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5282987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:51:28  来源:igfitidea点击:

do_shortcode not working

phpwordpress-pluginwordpress

提问by sjobe

I've been stuck on this for a while. I'm working on a wordpress site where I wrote the theme from scratch, I use php calls to get the wordpress functionality that I need in certain sections.

我已经坚持了一段时间。我正在一个 wordpress 网站上工作,在那里我从头开始编写主题,我使用 php 调用来获取某些部分所需的 wordpress 功能。

I'm trying to use a plugin, but calling it via

我正在尝试使用插件,但通过调用它

echo do_shortcode('[STORE-LOCATOR]');

just isnt working. Even when I switch to the default template and post that code, it still doesnt work. It simply echoes "[STORE-LOCATOR]"

只是不工作。即使我切换到默认模板并发布该代码,它仍然不起作用。它只是回声"[STORE-LOCATOR]"

Any help would be greatly appreciated.

任何帮助将不胜感激。

回答by birdsonwire

[STORE-LOCATOR]is probably not a 'shortcode' in WordPress sense.

[STORE-LOCATOR]可能不是WordPress 意义上的“短代码”。

I encountered this on different plugin, Stream media player. They use the same syntax as shortcodes, but they are actually not.

我在不同的插件流媒体播放器上遇到了这个问题。它们使用与短代码相同的语法,但实际上并非如此。

Try using:

尝试使用:

echo apply_filters( 'the_content',' [STORE-LOCATOR] ');

instead of do_shortcode, and see if it helps.

而不是do_shortcode,看看它是否有帮助。

回答by ggzone

do_shortcode()returns a string. I get it working by doing:

do_shortcode()返回一个字符串。我通过以下方式让它工作:

<?php echo do_shortcode(...); ?>

回答by gbanks

This is specific to the Store Locator plugin, not do_shortcodein general.

这是特定于 Store Locator 插件的,而不是do_shortcode一般的。

apply_filterscan be an acceptable workaround for other plugins, but this does not work for Store Locator; you will only see an empty space and some controls. This is because it is looking for that shortcode in the page/post body to determine whether or not to include all of its js references at the top of the page. And without these references, nothing will work. See the sl_head_scriptsfunction in sl-functions.php.

apply_filters对于其他插件可以是一个可接受的解决方法,但这不适用于 Store Locator;您只会看到一个空白区域和一些控件。这是因为它正在页面/帖子正文中查找该短代码,以确定是否在页面顶部包含其所有 js 引用。没有这些参考,什么都行不通。请参阅sl_head_scriptssl-functions.php 中的函数。

To change this behavior, simply modify that function to match based upon page title instead. In my instance I wanted it only on a "shop" page, so I commented out the entire $on_sl_pagetest and replaced it with this:

要更改此行为,只需修改该函数以根据页面标题进行匹配即可。在我的例子中,我只希望在“商店”页面上使用它,所以我注释掉了整个$on_sl_page测试并将其替换为:

$on_sl_page = ( strpos($pagename, 'shop') === 0 );

$on_sl_page = ( strpos($pagename, 'shop') === 0 );

I then called it from my page with apply_filters as indicated in the other answer:

然后我从我的页面使用 apply_filters 调用它,如另一个答案所示:

echo apply_filters( 'the_content','[STORE-LOCATOR]');

echo apply_filters( 'the_content','[STORE-LOCATOR]');

And this appears to work perfectly.

这似乎完美地工作。

回答by Fabio

echo do_shortcode('[STORE-LOCATOR][/STORE-LOCATOR]');

回答by PhysicsIsRelativelyCool

If you're writing the whole thing from scratch, you'll want to make sure that the function you create is in the root php file of your plugin. The function might look something like this, but you'll have to sub in whatever logic you're using to arrive at the store location:

如果您从头开始编写整个内容,您需要确保您创建的函数位于插件的根 php 文件中。该函数可能看起来像这样,但您必须使用用于到达商店位置的任何逻辑:

<?php
function doCoolStuff () {
$var1 = "value1";
$var2 = "value2";
$output = $var1+$var2;
}
return $output;
}
add_shortcode('SOTRE-LOCATIOR', 'doCoolStuff');
?>

Then in your template put the code:

然后在您的模板中放入代码:

<?php echo do_shortcode('[STORE-LOCATOR]');?>

Happy coding and good luck!

编码愉快,祝你好运!