分割通过“the_content()”获得的内容;在 WordPress 中

发布于 2024-12-02 05:34:27 字数 756 浏览 3 评论 0原文


现在我正在使用以下插件 - 'shortcoder' 来允许我推送整个首页我的 WordPress 网站使用单个帖子。 以下是我的帖子的结构

 [shortcode 1]
 [shortcode 2]
 [shortcode 3]
 [shortcode 4]
 [shortcode 5]
 [shortcode 6]

- 现在这些都是静态的,仅包含 html 和 javascript 内容。这篇文章使用“the_content()”模板标签显示在我的index.php 上。

但现在我将从我的其他帖子中提取一些动态内容来代替其中一些短代码,这些短代码的逻辑将被硬编码在index.php 文件中。例如,

 [shortcode 1] static
 [shortcode 2] static
 [shortcode 3] dynamic
 [shortcode 4] static
 [shortcode 5] dynamic
 [shortcode 6] static

需要明确的是,所有静态部分都将通过短代码进行,但动态部分将硬编码在 index.php 文件中。 但是,由于串行顺序,此逻辑变得混乱。

我想以某种方式将通过“the_content()”传入的内容拆分为适当的静态部分。

抱歉发了这么长的帖子,但我希望有人能想出一个解决方案。

Right now I am using the following plugin- 'shortcoder' to allow me to push the entire frontpage of my wordpress site using a single post.
Following is the structure of my post-

 [shortcode 1]
 [shortcode 2]
 [shortcode 3]
 [shortcode 4]
 [shortcode 5]
 [shortcode 6]

Right now these are all static containing only html and javascript content. This post is getting displayed on my index.php using the 'the_content()' template tag.

But now I will be pulling some dynamic content from my other posts in place of some of these shortcodes whose logic will be hardcoded in the index.php file. For eg.-

 [shortcode 1] static
 [shortcode 2] static
 [shortcode 3] dynamic
 [shortcode 4] static
 [shortcode 5] dynamic
 [shortcode 6] static

Just to be clear all the static sections will come through the shortcode but the dynamic sections will be hardcoded in the index.php file.
However, because of the serial order this logic is getting messed up.

I want to somehow split up the content coming through 'the_content()' into the appropriate static sections.

Sorry for the long post but I hope someone can come up with a solution.

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

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

发布评论

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

评论(2

蓝眸 2024-12-09 05:34:27

您可以创建自己的短代码来调用要运行的脚本。

例如,在您的 functions.php 文件中,您可以添加如下内容:

<?php 
function my_shortcode_callback($atts){

   $name = attr('name');

   switch($name){
      case 'script_1':
         // run code for script_1, maybe through a custom function
         script_1();
      break;
      case 'script_2':
         // run code for script_2, maybe through another custom function
         script_2();
      break;

   }
}

add_shortcode('my_shortcode', 'my_shortcode_callback');
?>

现在,在您的帖子编辑器中,您可以添加自己的短代码与插件的交织在一起,例如:

 [shortcode 1] static
 [shortcode 2] static
 [my_shortcode name='script_1'] dynamic
 [shortcode 4] static
 [my_shortcode name='script_2'] dynamic
 [shortcode 6] static

所以,基本上,您' d 将您的插件用于静态代码,并使用您自己的短代码用于动态代码=)

我希望能帮助您或让您朝着正确的方向前进。让我知道!

You could create your own shortcode(s) to call the scripts you want to run.

For example, in your functions.php file you could add something like this:

<?php 
function my_shortcode_callback($atts){

   $name = attr('name');

   switch($name){
      case 'script_1':
         // run code for script_1, maybe through a custom function
         script_1();
      break;
      case 'script_2':
         // run code for script_2, maybe through another custom function
         script_2();
      break;

   }
}

add_shortcode('my_shortcode', 'my_shortcode_callback');
?>

Now, in your post editor, you could add your own shortcode intertwined with the plugin's, such as:

 [shortcode 1] static
 [shortcode 2] static
 [my_shortcode name='script_1'] dynamic
 [shortcode 4] static
 [my_shortcode name='script_2'] dynamic
 [shortcode 6] static

So, basically, you'd be using your plugin for static code and your own shortcode for dynamic code =)

I hope that helps or gets you in the right direction. Let me know!

风追烟花雨 2024-12-09 05:34:27

这就是我使用普通 phpexplode 解决问题的方法 -

我首先为所有短代码添加了“~”分隔符 -

 [Shortcode1]~[Shortcode2]~[Shortcode3]

然后使用以下代码分隔了短代码

  <?php
    ob_start();
    the_content();
    $content = ob_get_clean();
    $part = explode('~', $content);
    ?>

-之后我所做的一切都在正确的位置回显了所需的变量在模板中-

<?php echo $part[0]; ?>,....

This is how I solved my problem using normal php explode-

I first added the '~' separator for all my shortcodes like -

 [Shortcode1]~[Shortcode2]~[Shortcode3]

Then using the following code I separated the shortcodes-

  <?php
    ob_start();
    the_content();
    $content = ob_get_clean();
    $part = explode('~', $content);
    ?>

After that all I did was echoed the required variables at the right spots in the template-

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