在add_filter()之前运行快速代码;

发布于 2025-01-24 14:44:26 字数 920 浏览 2 评论 0原文

因此,我在WordPress网站上有短码[MY_EXAMPLE_SHORTCODE]。

add_shortcode('my_example_shortcode','shortcode_for_project');

我想优先考虑短代码 add_filter('the_content','my_example');

如果在页面上找不到快捷代码,则我希望应用上面的代码。

我已经尝试过:

if (shortcode_for_project) {
  echo do_shortcode('[my_example_shortcode]');
}
else
{
add_filter('the_content', 'my_example');
}

假设我有一个页面:

商业智能

lorem ipsum dolor sit amet,consectetur adipiscing elit。 Donec Sem 麦格纳(Magna

那我想要 add_filter('the_content','my_example');启用

但是如果

商业智能

[my_example_shortcode]

lorem ipsum dolor sit amet,consectetur adipiscing elit。 Donec Sem 麦格纳(Magna

那我想要 add_filter('the_content','my_example'); 被禁用

So I have the shortcode [my_example_shortcode] on my WordPress site.

add_shortcode( 'my_example_shortcode', 'shortcode_for_project' );

I want to prioritise the shortcode before
add_filter('the_content', 'my_example');

If the shortcode is not found on the page then I want the the code above to be applied.

I have already tried:

if (shortcode_for_project) {
  echo do_shortcode('[my_example_shortcode]');
}
else
{
add_filter('the_content', 'my_example');
}

Let's say I have a page:

Business Intelligence

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sem
magna, maximus eget odio vel, dictum ultrices nunc.

Then I want
add_filter('the_content', 'my_example');
to be enabled

But if

Business Intelligence

[my_example_shortcode]

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sem
magna, maximus eget odio vel, dictum ultrices nunc.

Then I want
add_filter('the_content', 'my_example');
to be disabled

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌上芳菲 2025-01-31 14:44:26

如果我了解问题,这是您可以做到这一点的一种方法:

// functions.php

add_filter("the_content", function($content) {
    if (shortcode_exists('my_example_shortcode')) {
        ob_start();
        
        echo do_shortcode('[my_example_shortcode]');

        return ob_get_clean();
    }

    return my_example($content);
});

If I understand the problem here is one way you can do this:

// functions.php

add_filter("the_content", function($content) {
    if (shortcode_exists('my_example_shortcode')) {
        ob_start();
        
        echo do_shortcode('[my_example_shortcode]');

        return ob_get_clean();
    }

    return my_example($content);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文