当前位置:网站首页>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
边栏推荐
- After screwing the screws in the factory for two years, I earned more than 10000 yuan a month by "testing" and counterattacked
- Introduction to interval DP
- 红象云腾完成与龙蜥操作系统兼容适配,产品运行稳定
- Berkeley, MIT, Cambridge, deepmind et d'autres grandes conférences en ligne: vers une IA sûre, fiable et contrôlable
- Visitor model -- generation gap between young and middle-aged people
- Does the developer want to change to software testing?
- Selenium crawl notes
- Docker deploy mysql5.7
- Static routing job supplement
- Berkeley, MIT, Cambridge, deepmind and other industry leaders' online lectures: towards safe, reliable and controllable AI
猜你喜欢

Sleep revolution - find the right length of rest

Am, FM, PM modulation technology

Summary of message protocol problems

Intermediary model -- collaboration among departments

使用gorm查询数据库时reflect: reflect.flag.mustBeAssignable using unaddressable value

VMware virtual machine setting static IP

Berkeley, MIT, Cambridge, deepmind and other industry leaders' online lectures: towards safe, reliable and controllable AI

Nifi fast authentication configuration

Prototype mode -- clone monster Army

Sequential stack traversal binary tree
随机推荐
JMeter response assertion
Postman assertion
Splicing audio files with ffmpeg-4.3
使用gorm查询数据库时reflect: reflect.flag.mustBeAssignable using unaddressable value
Batch capitalization of MySQL table names
网络安全审查办公室对知网启动网络安全审查
Appium desktop introduction
Markdown use
Sequential stack traversal binary tree
Open function
Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
Combination mode -- stock speculation has been cut into leeks? Come and try this investment strategy!
GDB debugging
Rip/ospf protocol notes sorting
Basic operation of sequence table
全上链哈希游戏dapp系统定制(方案设计)
在Dialog中使用透明的【X】叉叉按钮图片
畅直播|针对直播痛点的关键技术解析
Time standard and format
科创人·味多美CIO胡博:数字化是不流血的革命,正确答案藏在业务的田间地头