当前位置:网站首页>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
边栏推荐
- Intermediary model -- collaboration among departments
- Microsoft Certification (dynamic 365) test
- How to apply agile development ideas to other work
- Berkeley, MIT, Cambridge, deepmind et d'autres grandes conférences en ligne: vers une IA sûre, fiable et contrôlable
- Postman assertion
- regular expression
- Dx12 engine development course progress - where does this course go
- JMeter parameterization
- Open function
- Builder mode -- Master asked me to refine pills
猜你喜欢

Read all text from stdin to a string

主数据建设的背景

DHCP operation

Adding subscribers to a list using mailchimp's API V3

Am, FM, PM modulation technology

Learn to use a new technology quickly

Camera rental management system based on qt+mysql

Background of master data construction

Prototype mode -- clone monster Army

微信小程序中使用vant组件
随机推荐
Common self realization functions in C language development
Camera rental management system based on qt+mysql
How Fiddler works
Mr. Hu Bo, CIO of weiduomei, a scientific innovator: digitalization is a bloodless revolution, and the correct answer lies in the field of business
yeb_ Back first day
Can the OPDS SQL component pass process parameters to the next component through context
Batch capitalization of MySQL table names
畅直播|针对直播痛点的关键技术解析
Handling of garbled JMeter response data - three solutions
Second understanding permutation and combination
Smooth live broadcast | analysis of key technologies for live broadcast pain points
Variable setting in postman
JMeter parameterization
Geek University cloud native training camp
How to apply agile development ideas to other work
Talking about the range of data that MySQL update will lock
GDB debugging
Common member methods of the calendar class
微信小程序自定义tabBar
Berkeley, MIT, Cambridge, deepmind and other industry leaders' online lectures: towards safe, reliable and controllable AI