【问题标题】:Wordpress add get_template_part into shortcode phpWordpress 将 get_template_part 添加到简码 php
【发布时间】:2014-05-06 14:26:44
【问题描述】:

我为我的主题创建了一个短代码,它将向页面/帖子添加一个投资组合。因为投资组合的代码相当长且复杂,所以我将它放在一个单独的文件中,并尝试使用 get_template_part 从我的 shortcodes.php 文件中调用它。

这是我目前拥有的代码:

function portfolio_new($atts) 
{        
ob_start();  
get_template_part('/includes/shortcode_helpers/portfolio', 'shortcode');  
$ret = ob_get_contents();  
ob_end_clean();  
return $ret;    
}

add_shortcode('portfolio_new', 'portfolio_new');

但是,这不会返回任何内容。我的包含投资组合 php 的文件位于 incudes/shortcode_helpers/portfolio_shortcode.php

谁能指出我哪里出错了

【问题讨论】:

    标签: php wordpress shortcode


    【解决方案1】:

    您需要将模板文件重命名为portfolio-shortcode.php(使用破折号而不是下划线)。 get_template_part 正在检查以下文件:

    {current_theme}/includes/shortcode_helpers/portfolio-shortcode.php

    您还需要更改get_template_part的第一个参数以删除前导斜杠:

    get_template_part('includes/shortcode_helpers/portfolio', 'shortcode');

    请参阅 WordPress Codex 上的 get_template_part,了解此函数尝试包含的文件的顺序。

    【讨论】:

      猜你喜欢
      • 2014-04-26
      • 1970-01-01
      • 1970-01-01
      • 2017-10-07
      • 2011-08-14
      • 1970-01-01
      • 2019-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多