简单的短代码(WordPress)不起作用



这是我的"代码":

   function whoop_function(){
        return "whoop whoop!";
    }
add_shortcode('whoop', 'whoop_function' );

现在,当我想在帖子中使用它时,我得到的只是:

[whoop]

您可以看到,我非常新颖,没有使用它,所以答案也许真的很简单,也许我只是提前错过了一件事情。我都检查了在functions.php和content.php

中的定义函数

对于短代码函数需要回声。

请检查以下代码

function whoop_function(){
 $responseData = "whoop whoop!";
 echo $responseData;
 return true;
}
add_shortcode('whoop', 'whoop_function' );

将您的代码放入 themes/function.php文件中,然后从content.php中删除函数重复,因此函数已经定义了PHP错误。

function whoop_function(){
        return "whoop whoop!";
    }
add_shortcode('whoop', 'whoop_function' );

并在任何页面内容部分中添加您的短代码[whoop]

如果您使用 do_shortcode('[whoop]');,则像下面一样回荡。

<?php echo do_shortcode('[whoop]'); ?>

您的方法正确。我认为您正在模板中使用它。只需使用如下所述使用它:

在文件中:

<?php echo do_shortcode('[whoop]'); ?>

在管理页面或帖子中:

[whoop]

以下获取内容:

        $post_content = get_post(get_id_by_slug('short')); // assuming that you have already defined get_id_by_slug function as you are using it in your code and "short" is a slug for post you are fetching content
        $content = $post_content->post_content;
        echo do_shortcode( $content );

您在不考虑简短代码的情况下获取内容。如上所述更新您的代码。希望它对您有用。

最新更新