【问题标题】:Wordpress add_action inside another functionWordpress add_action 在另一个函数中
【发布时间】:2014-02-03 19:32:22
【问题描述】:

我想根据简码参数给头部添加一个样式标签。我为样式编写了一个函数,并希望在简码函数中调用它的add_action。

但是,样式并没有添加到头部。如果我在函数之外调用 add_action ,它会成功运行。 请告诉我如何在 php 函数中调用语句“add_action”,以及如何向函数发送参数以调用 add_action。

这是我的代码:

function ag_appearance( $year ){
?>
<style type="text/css">
    /*Some style here*/
</style>
<?php
}

add_action( 'wp_head', 'ag_appearance' ); /*  this is working */

function dp_agenda( $atts, $content = null ){
   extract( shortcode_atts( array(
    'year' => '',
    'day' => '',
    'summit' => '',
    'complexity' => '',
    'certification' => '',
    'event' => '',
    'make' => ''
    ), $atts ) );
    add_action( 'wp_head', 'ag_appearance' ); 
  /*  this is not working, while i want to call here by $year parameter */
}

【问题讨论】:

标签: wordpress


【解决方案1】:

我建议创建一个 CSS 文件,其中包含您要添加到标题的样式(例如 appearance.css)并将其放在主题文件夹中,以便您知道它在哪里(我通常使用 /css 作为样式) .然后在调用包含函数时使用wp_enqueue_style 设置标题中的样式。有点像这样:

function dp_agenda( $atts, $content = null ){
   extract( shortcode_atts( array(
    'year' => '',
    'day' => '',
    'summit' => '',
    'complexity' => '',
    'certification' => '',
    'event' => '',
    'make' => ''
    ), $atts ) );
    wp_enqueue_style('appearance',get_stylesheet_directory_uri().'/css/appearance.css'); 
  /*  this is not working, while i want to call here by $year parameter */
}

默认情况下,WordPress 中的排队样式通过wp_head() 放置在头部,而脚本则放置在您放置wp_footer() 的任何位置,通常位于&lt;/body&gt; 标记之前。

您也可以使用wp_enqueue_script 来提取您的javascript 文件。

【讨论】:

  • 如果您根本没有费心阅读这个问题,您就会知道您的答案是 100% 不相关的。
猜你喜欢
  • 2011-04-05
  • 2014-11-07
  • 1970-01-01
  • 2018-11-04
  • 2015-07-17
  • 1970-01-01
  • 2014-03-13
  • 2015-11-12
  • 1970-01-01
相关资源
最近更新 更多