duanlieshuang5330 2010-11-24 23:11
浏览 55
已采纳

如何在另一个wordpress函数中嵌入wordpress函数?

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.

  • 写回答

4条回答 默认 最新

  • dqo58772 2010-11-24 23:15
    关注

    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());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题