php [自定义边栏] - 向子类别存档添加侧边栏

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [自定义边栏] - 向子类别存档添加侧边栏相关的知识,希望对你有一定的参考价值。

 <?php

//From https://wordpress.org/support/topic/adding-a-sidebar-to-subcategory-archives/#post-10209291


// Filter Custom Sidebars to include sidebar in subcategory archives
add_filter( 'custom_sidebars_set_location', function( $options, $id, $sidebars, $data ){

  // Go through all registered custom sidebars, category id as index
  foreach ( $options['category_archive'] as $cat_id => $sidebar ) {

    // If the looped category has no sidebar set, just skip it to save time
    if ( empty( $sidebar ) )
      continue;

    // Go through all the category children
    $children = get_term_children( $cat_id, 'category' );
    foreach ( $children as $child ) {

      // If the child category has no sidebar set to it, copy the parent sidebar
      if ( empty( $options['category_archive'][$child] ) ) {
        $options['category_archive'][$child] = $sidebar;
      }

    }

  }

  return $options;
}, 10, 4 );

以上是关于php [自定义边栏] - 向子类别存档添加侧边栏的主要内容,如果未能解决你的问题,请参考以下文章

如何:在wordpress中自定义帖子类型的自定义类别存档页面

php 边栏获取侧边栏

使用自定义帖子类型时更改侧边栏的一侧(左/右)

Drawer实现侧边栏布局

WP Twenty12主题宽度边栏页脚再次。第2部分:侧边栏-页脚.PHP

WordPress中单个类别的存档页面