I would like to add a php code into a shortcode code in HTML editor. My shortcode looks like this:
我想在HTML编辑器中将PHP代码添加到短代码中。我的短代码如下:
<?php echo do_shortcode('[eapi keyword="KEYWORD" n=25]'); ?>
It works, if I put just a word instead of KEYWORD. Now I would like to add an individual title with this php code:
如果我只使用一个单词而不是KEYWORD,它就可以工作。现在我想用这个PHP代码添加一个单独的标题:
<?php the_title(); ?>
So I inserted it into the shortcode like this:
所以我把它插入到这样的短代码中:
<?php echo do_shortcode('[eapi keyword="<?php the_title(); ?>
" n=25]'); ?>
But unfortunately it did not work. How can I successfully insert this php code into the shortcode?
但不幸的是它没有用。如何成功将此PHP代码插入短代码?
3 个解决方案
#1
0
Here is a possible solution but I am not familiar with the plugin/context you are using this in. But maybe this will give you some ideas:
这是一个可能的解决方案,但我不熟悉你使用它的插件/上下文。但也许这会给你一些想法:
<?php echo do_shortcode("[eapi keyword=\"" . the_title() . "\" n=25]"); ?>
So I removed the inner php tags for one. I switched the outer quotes to double quotes since you can't use functions/variables within single quotes. At the same time I concatenated the title function since I don't think it can be interpreted within the quotes. That also might negate the need for double quotes, but force of habit.
所以我删除了内部的php标签。我将外引号切换为双引号,因为您不能在单引号内使用函数/变量。同时我连接了title函数,因为我认为它不能在引号内解释。这也可能否定双引号的需要,但习惯的力量。
EDIT - Some other use examples outside of the original question for clarity.
编辑 - 为了清楚起见,在原始问题之外的其他一些使用示例。
Variable in the string requires double quotes:
字符串中的变量需要双引号:
<?php echo do_shortcode("[eapi keyword='{$variable}' n=25]"); ?>
Variable concatenated can use single or double quotes:
变量连接可以使用单引号或双引号:
<?php echo do_shortcode("[eapi keyword='" . {$variable} . "' n=25]"); ?>
Keywords and functions must be concatenated:
必须连接关键字和函数:
<?php echo do_shortcode("[".KEYWORD." keyword='".func()."' n=25 x={$var}]"); ?>
#2
1
Use something like
使用类似的东西
<?php do_shortcode('[eapi keyword="' . get_the_title() . '"]'; ?>
You'll want get_the_title because it will return the title instead of directly outputting it (as the_title will). As you are already in PHP mode, you don't need any additional
你会想要get_the_title因为它会返回标题而不是直接输出它(就像the_title一样)。由于您已经处于PHP模式,因此无需任何其他操作
#3
0
I think that you need to modify your eapi plug in to support your requirement.
我认为您需要修改您的eapi插件以支持您的要求。
For example, you can make a rule on your eapi plug to analysis the parameter's keyword.
例如,您可以在eapi插件上制定规则来分析参数的关键字。
1) if it is a simple string, return directly
1)如果它是一个简单的字符串,直接返回
2) if it is a string as similar as "{{{......}}}", use eval function to run it as a php script and then return.
2)如果它是一个类似于“{{{......}}}”的字符串,请使用eval函数将其作为php脚本运行然后返回。
#1
0
Here is a possible solution but I am not familiar with the plugin/context you are using this in. But maybe this will give you some ideas:
这是一个可能的解决方案,但我不熟悉你使用它的插件/上下文。但也许这会给你一些想法:
<?php echo do_shortcode("[eapi keyword=\"" . the_title() . "\" n=25]"); ?>
So I removed the inner php tags for one. I switched the outer quotes to double quotes since you can't use functions/variables within single quotes. At the same time I concatenated the title function since I don't think it can be interpreted within the quotes. That also might negate the need for double quotes, but force of habit.
所以我删除了内部的php标签。我将外引号切换为双引号,因为您不能在单引号内使用函数/变量。同时我连接了title函数,因为我认为它不能在引号内解释。这也可能否定双引号的需要,但习惯的力量。
EDIT - Some other use examples outside of the original question for clarity.
编辑 - 为了清楚起见,在原始问题之外的其他一些使用示例。
Variable in the string requires double quotes:
字符串中的变量需要双引号:
<?php echo do_shortcode("[eapi keyword='{$variable}' n=25]"); ?>
Variable concatenated can use single or double quotes:
变量连接可以使用单引号或双引号:
<?php echo do_shortcode("[eapi keyword='" . {$variable} . "' n=25]"); ?>
Keywords and functions must be concatenated:
必须连接关键字和函数:
<?php echo do_shortcode("[".KEYWORD." keyword='".func()."' n=25 x={$var}]"); ?>
#2
1
Use something like
使用类似的东西
<?php do_shortcode('[eapi keyword="' . get_the_title() . '"]'; ?>
You'll want get_the_title because it will return the title instead of directly outputting it (as the_title will). As you are already in PHP mode, you don't need any additional
你会想要get_the_title因为它会返回标题而不是直接输出它(就像the_title一样)。由于您已经处于PHP模式,因此无需任何其他操作
#3
0
I think that you need to modify your eapi plug in to support your requirement.
我认为您需要修改您的eapi插件以支持您的要求。
For example, you can make a rule on your eapi plug to analysis the parameter's keyword.
例如,您可以在eapi插件上制定规则来分析参数的关键字。
1) if it is a simple string, return directly
1)如果它是一个简单的字符串,直接返回
2) if it is a string as similar as "{{{......}}}", use eval function to run it as a php script and then return.
2)如果它是一个类似于“{{{......}}}”的字符串,请使用eval函数将其作为php脚本运行然后返回。