当前位置:网站首页>Different WordPress pages display different gadgets

Different WordPress pages display different gadgets

2022-06-24 21:12:00 The smell of tobacco

problem

I want to make an article directory displayed on the right , Plug ins that use the article directory Easy Table of Contents, Add it to the sidebar on the right , It was easy to do this .

however , A new problem has arisen . Tools for this directory , Need to be displayed on the article page , On other pages, it doesn't show .

So here comes the question , How to make different pages display different sidebar tools ?

I found some tutorials on the Internet , Basically, it is recommended to install plug-ins , In this way, we can really solve , But installing plug-ins will slow down the loading speed of the website , Can you realize it by yourself ?

solve

Think about it. , WordPress So many hooks are provided , Is there a suitable hook to filter page tools ?

On the official website https://developer.wordpress.org/reference/hooks/ By keyword widget Search for relevant hooks , It turned out that they did provide . There are many official hooks , If necessary, you can check if there is anything you need .

Namely : sidebars_widgets, You can filter for gadgets .

The parameters received by the callback function are as follows :

{
    
  "wp_inactive_widgets": [
    "block-2",
    "block-3",
    "block-4",
    "block-6",
    "block-8",
    "block-10",
    "block-11",
    "block-12",
    "block-15",
    "block-16",
    "block-18",
    "block-19"
  ],
  "sidebar-1": [
    "block-20",
    "custom_html-2",
    "categories-2",
    "ezw_tco-2"
  ],
  "header-widget": [],
  "footer-widget-1": [],
  "footer-widget-2": [],
  "advanced-footer-widget-1": [],
  "advanced-footer-widget-2": [],
  "advanced-footer-widget-3": [],
  "advanced-footer-widget-4": []
}

among sidebar-1 This is the sidebar tool list . So the thinking is very clear , Just filter different tools on different pages , You can achieve the effect .

Here is the filter method I added , My requirement is to display only the catalog tool on the article page , Remove the catalog tool from other pages . For reference only ( Add code to functions.php In file ).

Note here , The management background cannot filter , Otherwise you won't see the gadget page in the background .

/** *  Sidebar widget filtering  * @author hujing */
add_filter( 'sidebars_widgets',  function ($widgets){
    
    //  Backend interface ,  No filtering 
    if(is_admin()) return $widgets;
    //  Determine whether it is a catalog part 
    $isEzwTco = fn($i) => strpos($i, 'ezw_tco-') === 0;
    //  Filter the sidebar content 
    $filterSidebar = function ($isKeep) use (&$widgets){
    
        foreach ($widgets as $type=>&$tmpList){
    
            //  Find the sidebar ,  To filter 
            if(strpos($type, 'sidebar-') === 0){
    
                $tmpList = array_filter($tmpList, $isKeep);
            }
        }
    };
    //  Article page ,  The sidebar values leave the table of contents 
    if(is_single()){
    
        $filterSidebar(fn($i) => $isEzwTco($i));
    }else{
     //  Non article page ,  Filter the contents of the sidebar 
        $filterSidebar(fn($i) => !$isEzwTco($i));
    }
    return $widgets;
});

Link to the original text : https://hujingnb.com/archives/679

原网站

版权声明
本文为[The smell of tobacco]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211318415928.html