>我在我的网站上显示来自供应商服务器的页面,并希望使用短代码来设置内容布局的样式,此页面有一个 url http://mysite.com/vendor_page
。
做了一些研究,并了解到我可以使用do_shortcode()
来回显短代码,但不确定如何在没有实际内容的外部页面上使用它,我可以使用我的 WordPress 可视化 html 编辑器进行编辑。
有点迷失在这里,希望有人能指出我一个正确的方向。
要在template/php file
中使用do_shortcode()
函数,您必须首先在functions.php
中创建/注册一个shortcode
,例如
add_shortcode('my_test_shortCode', 'my_test_shortCode_func');
function my_test_shortCode_func()
{
return "my_test_shortCode_func working...!"; // or whatever you want
}
然后在你的模板/php文件中,你可以像这样直接调用它
<?php echo do_shortcode('[my_test_shortCode]');?>
结果它会在do_shortcode
的地方回响my_test_shortCode_func working...!
。
WordPress与您的网站集成:
为了将常规PHP页面转换为使用WordPress的页面,您需要将以下代码片段之一添加到每个页面的开头。
<?php /* Short and sweet */ define('WP_USE_THEMES', false); require('./wp-blog-header.php'); ?> <?php require('/the/path/to/your/wp-blog-header.php'); ?>
我从未尝试过这个,所以我不确定在哪里可以找到该文件,但是在包含它之后,您应该可以访问Wordpress功能。通过do_shortcode()
传递任何包含短代码的字符串,您将获得带有替换短代码的结果。