如何将一个 WordPress 函数嵌套在另一个 WordPress 函数中?
我正在开发一个 WordPress 主题,并尝试调用父类别的名称来提取适当的页面模板。
我可以让调用函数回显正确的名称,但是当我尝试嵌套它时,该函数不会运行。我发现我需要使用 { } 因为我已经在 php 中了,但它仍然无法正常工作。有人可以纠正我吗?
这给出了正确的输出:
<?php $category = get_the_category();
$parent = get_cat_name($category[0]->category_parent);
if (!empty($parent)) {
echo '' . $parent;
} else {
echo '' . $category[0]->cat_name;
}
?>
。 。 。所以我创建了一个category_parent.php 文件,其中包含该文件。
这就是我试图将其嵌套在其中的内容:
<?php get_template_part( ' ' ); ?>
像这样:
1.
<?php get_template_part( '<?php get_template_part( 'category_parent' ); ?>' ); ?>
或这个
2.
<?php get_template_part( '{get_template_part( 'category_parent' ); }' ); ?>
两者都不起作用。
I'm working on a wordpress theme and I'm trying to call the parent category's name to pull the appropriate page template.
I can get the call function to echo the correct name, but when I try to nest it the function doesn't run. I saw that I needed to use { } since I was already inside php but it still isn't working right. Can someone straighten me out?
This gives the correct output:
<?php $category = get_the_category();
$parent = get_cat_name($category[0]->category_parent);
if (!empty($parent)) {
echo '' . $parent;
} else {
echo '' . $category[0]->cat_name;
}
?>
. . . so I created a category_parent.php file with that in it.
This is what I'm trying to nest it in:
<?php get_template_part( ' ' ); ?>
Like this:
1.
<?php get_template_part( '<?php get_template_part( 'category_parent' ); ?>' ); ?>
or this
2.
<?php get_template_part( '{get_template_part( 'category_parent' ); }' ); ?>
Neither one works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我真的不知道这是否是你想要的,因为我没有试图理解你在做什么。然而,一般来说,你这样做:
编辑:
我查了一下
get_template_part()
在 WP 中的作用,我认为 Felix Kling 的答案就是你所需要的。将某些内容发送到屏幕和将其分配给变量之间存在很大差异。如果包含该文件,您将在浏览器中看到
filename
。 PHP对此一无所知。 (好吧,如果你使用输出缓冲函数,那就可以了,但这不是重点......)但是,如果你做了类似的事情:
你现在可以在你的函数中使用它:
那么Felix告诉你要做的就是把您当前在函数中拥有的逻辑。在此示例中:
现在
foo()
返回的任何值都将发送到您的get_template_part()
。获取您的代码:
您可以获取 Felix 的答案并将其放入名为
category_parent.php
的文件中,然后使用它,如下所示:I really don't know if this is what you want as I did not try to make sense of what you are doing. However, generally speaking, you do this:
Edit:
I looked up what
get_template_part()
does in WP, and I think Felix Kling's answer is what you need. There is a big difference between sending something to the screen and assigning it to a variable.If you include that file, you will see
filename
in the browser. PHP knows nothing about it. (Okay, it could if you made use of output buffering functions, but that's besides the point...)However if you do something like:
You can now use it in your function:
So what Felix is telling you to do is to put the logic that you currently have into a function. In this example:
Now whatever value
foo()
returns will be sent to yourget_template_part()
.Taking your code:
You could take Felix's answer and put it into a file called
category_parent.php
, and then use it like:老实说,我对 WordPress 不太熟悉,但在我看来,你可以这样做:
Honestly I am not so familiar with Wordpress, but it seems to me, you could do:
konforce 的语法是正确的,就像 konforce 一样,我不知道你想做什么。您不需要使用 { } 因为您没有尝试动态命名变量,并且您当然不需要使用
转义到 php,因为 (1) 您已经在 php 中,并且 (2) 当它遇到第一个“?>”时,它将停止解释 PHP 并假定为 html。
嵌套函数没有特殊的语法。简单地说:
是语法,但我不知道该函数是什么或做什么,所以我不知道这是否有效。
要调试,为什么不试试这个:
konforce is correct about the syntax and, like konforce, I have no idea what you are trying to do. You do not need to use { } because your are not trying to dynamically name a variable and you certainly don't need to escape to php using
<?php ?>
, as (1) you are already in php and (2) it will stop interpreting PHP and assume html the second it hits the first '?>'.There is no special syntax for nesting functions. Simply:
is the syntax, but I have no idea what the function is or does, so I have no idea if that will work.
To debug, why don't you try this:
在 php 字符串中使用变量时,您需要使用双引号 (")。我认为选项 2 应该可以工作。
When using variables in php strings, you will need to use double quotes ("). I assume option 2 should work then.