开发者

How do I nest a wordpress function inside another wordpress function?

开发者 https://www.devze.com 2023-01-27 08:13 出处:网络
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'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 cr开发者_运维知识库eated 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.


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:

<?php get_template_part( get_template_part( 'category_parent' ) ); ?>

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.

<?php
  echo 'filename';
?>

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:

<?php
   $x = 'filename';
?>

You can now use it in your function:

<?php
  get_template_part($x);
?>

So what Felix is telling you to do is to put the logic that you currently have into a function. In this example:

<?php
  function foo()
  {
    return 'filename';
  }

  get_template_part(foo());
?>

Now whatever value foo() returns will be sent to your get_template_part().

Taking your code:

$category = get_the_category();
$parent = get_cat_name($category[0]->category_parent);
if (!empty($parent)) {
  $name = $parent;
} else {
  $name = $category[0]->cat_name;
}

get_template_part($name);

You could take Felix's answer and put it into a file called category_parent.php, and then use it like:

require_once 'category_parent.php'
get_template_part(getName());


Honestly I am not so familiar with Wordpress, but it seems to me, you could do:

function getName() {
    $category = get_the_category();
    $parent = get_cat_name($category[0]->category_parent);
    if (!empty($parent)) {
        return '' . $parent;
    } else {
        return '' . $category[0]->cat_name;
    }
}

get_template_part(getName());


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:

get_template_part(get_template_part('category_parent'));

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:

$parent = get_template_part('category_parent');
echo 'parent: ' . $parent . '<br />';
$result = get_template_part($parent);
echo 'result: ' . $result . '<br />';


When using variables in php strings, you will need to use double quotes ("). I assume option 2 should work then.

0

精彩评论

暂无评论...
验证码 换一张
取 消