function.php 中的 wordpress 函数未从模板文件中调用

wordpress functions in function.php not being called from a template file

我创建了一个子主题并且最近安装了 woocommerce。

我在模板中添加了一个 sidebar-shop.php 文件,当我将虚拟内容放入其中时效果很好。

在我的函数文件中有:

function lls_woo_before_widget(){
    $content='<aside class="rightHandColumn">';
    return $content;
}
add_action('init','lls_woo_before_widget');

在侧边栏-shop.php我有:

lls_woo_before_widget();

dynamic_sidebar('secondary-aside');

功能lls_woo_before_widget似乎不​​起作用,但边栏很好。

为什么我不能使用 sidebar-shop.php 中的函数,我怎么能(我知道我可以将代码写入 sidebar-shop.php 文件...)

谢谢

如果您的目标是将 secondary-aside 侧边栏包裹在 <aside> 标记中,您应该可以直接在 sidebar-shop.php 文件中这样做:

?>
<aside class="rightHandColumn">
    <?php dynamic_sidebar('secondary-aside'); ?>
</aside>
<?php

关于您上面提到的错误:

  • init 调用此函数还为时过早,init 挂钩不应生成任何输出,因为 headers haven't been sent yetinit 挂钩不会修改 the_content.
  • the_content用于循环输出帖子,不会像你期望的那样在侧边栏结束。
  • dynamic_sidebar() prints the sidebar markup. If you wanted to act on the widgets in this sidebar as they are rendered, it has hooks
  • 如果您想从一个文件调用另一个 php 文件中的 php 函数,您需要先 include 包含该函数的文件,然后再尝试调用它。