使用wordpress的dynamic Sidebar After Hook钩子

使用WordPress的dynamic_sidebar_after hook钩子

使用WordPress的dynamic_sidebar_after hook钩子

  在 WordPress CMS内容管理系统中,dynamic_sidebar_after是 WordPress 中用于显示动态侧边栏的动作钩子。在动态侧边栏中呈现小部件后触发。在动态侧边栏中呈现小部件后,
dynamic_sidebar_after WordPress PHP 操作将触发,此操作也会在空侧边栏以及前端和后端触发,包括小部件屏幕上的非活动小部件侧边栏。

  推荐:[最新版]WordPress SEO插件Rank Math Pro

dynamic_sidebar_after基本语法

描述

  在动态侧边栏中呈现小部件后触发

用法

add_action('dynamic_sidebar_after', 'your_custom_function', 10, 2);

function your_custom_function($index, $has_widgets) {
  // your custom code here
}
  • $index(int|string):动态侧边栏的索引、名称或 ID。
  • $has_widgets(bool): 侧边栏是否填充小部件。默认为 true。

  推荐:7个WordPress捐赠插件

dynamic_sidebar_after

  在 WordPress 中,该dynamic_sidebar_after通常是在动态侧边栏中呈现小部件后触发

do_action( 'dynamic_sidebar_after', $index, true );

  推荐:Win/Mac/Linux下载并安装ChatGPT程序

如何使用dynamic_sidebar_after

  在特定侧边栏后添加自定义消息

add_action('dynamic_sidebar_after', 'add_custom_message', 10, 2);

function add_custom_message($index, $has_widgets) {
  if ($index == 'sidebar-1' && $has_widgets) {
    echo '<p>**Thanks for visiting our sidebar!**</p>';
  }
}

  将自定义CSS类添加到带有小部件的侧边栏主体中

add_filter('body_class', 'add_sidebar_body_class');

function add_sidebar_body_class($classes) {
  if (is_active_sidebar('sidebar-1')) {
    $classes[] = 'has-sidebar';
  }
  return $classes;
}

  当侧边栏中没有小部件时显示一条消息

add_action('dynamic_sidebar_after', 'display_no_widgets_message', 10, 2);

function display_no_widgets_message($index, $has_widgets) {
  if (!$has_widgets) {
    echo '<p>**No widgets are currently active in this sidebar.**</p>';
  }
}

  在特定侧边栏后添加自定义包装器

add_action('dynamic_sidebar_after', 'add_custom_wrapper', 10, 2);

function add_custom_wrapper($index, $has_widgets) {
  if ($index == 'footer-sidebar') {
    echo '</div> <!-- Close custom wrapper -->';
  }
}

  在所有侧边栏后添加自定义跟踪代码

add_action('dynamic_sidebar_after', 'add_tracking_code', 10, 2);

function add_tracking_code($index, $has_widgets) {
  if ($has_widgets) {
    echo '<!-- Custom tracking code goes here -->';
  }
}

  推荐:怎么安装Genesis Framework主题框架

  推荐:WordPress函数使用手册


晓得博客,版权所有丨如未注明,均为原创
晓得博客 » 使用WordPress的dynamic_sidebar_after hook钩子

转载请保留链接:https://www.pythonthree.com/wordpress-dynamic-sidebar-after/

滚动至顶部