【问题标题】:Wordpress- A plugin that displays HTML content (Database) with a shortcodeWordpress- 使用简码显示 HTML 内容(数据库)的插件
【发布时间】:2017-08-21 23:42:51
【问题描述】:

我运行一个项目,该项目在 Google 表格中有一个数据库。我需要在交互式表格中显示该信息。

我使用 Sheetrock.js 和 Handlebar.js 来执行此操作(我编写了代码,它可以按我的意愿工作)。我有一个 WordPress 网站,我想制作一个插件,在其中输入简码,我编码的表格出现在页面上。

我有一个带有嵌入样式的单独 HTML 文件(在 <style></style> 标签下,没有外部 css 文件)。该文件有一个 HTML 样板,在 <head></head> 中有指向 sheetrock.js、handlebar.js 和 bootstrap 的链接。

据我了解,php 不能显示那些 HTML 标签? 我尝试执行以下操作...

<?php
function showIco() {
?>
<!DOCTYPE html>
<head>
<!-- all the CDN links  -->
</head>
<style>
<!-- all the styling is here, and I know, it's not DRY -->
</style>
<body>
<!-- The content I want to display (it has <script> tags, and variables) -->
</body>
</html>
<?php
return showIco();
}
add_shortcode ('add_ico','showIco');
?>

也许我做错了什么,因为简码根本不显示内容。我需要做什么才能完成这项工作?

【问题讨论】:

    标签: php html css wordpress


    【解决方案1】:

    您正在返回函数,这将导致递归。

    试试:

    <?php
    function showIco() {
        ob_start();
    ?>
    <!DOCTYPE html>
    <head>
     all the CDN links
    </head>
    <style>
    all the styling is here, and I know, it's not DRY
    </style>
    <body>
    The content I want to display (it has <script> tags, and variables)
    </body>
    </html><?php
        return ob_get_clean();
    }
    add_shortcode ('add_ico','showIco');
    ?>
    

    【讨论】:

    • 嗯,它不起作用 - 页面根本不显示任何内容,即使是默认的 wordpress 主题(它只是一个空白页面)。也许代码本身需要用 PHP 编写,而不是显示完整的 HTML 文件?
    • 检查您的错误日志..这将是某处的语法错误。 (不在我的例子中)
    • 奇怪的是没有显示错误日志。。当我激活插件时,Wordpress显示这个“插件在激活过程中产生了1个字符的意外输出。”
    • 您的完整代码中可能存在问题。见:stackoverflow.com/questions/4074477/…
    • 我重新检查了代码 - 一切似乎都已经到位......也许还有其他方法可以使用 php 显示 HTML 内容(具有交互式样式)?
    【解决方案2】:

    这是因为您试图在短代码中返回整个 HTML 文档,而该短代码本身已经在 HTML 文档中。

    对于 head 中的脚本,你应该使用 enqueue 方法并在使用短代码时返回它们,然后从 HTML 中去除 head/body/html 标签。

    <?php
    function show_ico_scripts() {
            wp_register_script("show-ico-scripts", "//link-to-CDN-here", array(), "1.0", false);
        }
        function showIco() {
            wp_enqueue_script( 'show-ico-scripts' );
            ob_start();
        ?>
        <style>
        all the styling is here, and I know, it's not DRY
        </style>
        The content I want to display (it has <script></script> tags, and variables)
        <?php
            return ob_get_clean();
        }
        add_shortcode ('add_ico','showIco');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-13
      • 2018-08-21
      • 1970-01-01
      • 2014-08-10
      • 2014-11-11
      • 1970-01-01
      相关资源
      最近更新 更多