Wordpress can set a chain table, and drag and drop the chain table widget in the background appearance tool to display the link on the sidebar. But how can we only display the link on the homepage, what about links not displayed on other pages? The source code is not modified, so that the tool can be used without creating a sidebar file.
Remove the widget from the sidebar
You only need to find the related filter. The hook of widgets in this sidebar is sidebars_widgets. If you want to display a widget without a link on a non-homepage, add the following code:
| The code is as follows: |
Copy code |
// Display the widget of the link on the homepage only Function index_link ($ widgets ){ If (! Is_home ()){ Unset ($ widgets ['sidebar-1'] [8]); } Return $ widgets; } Add_filter ('inclubars _ widgets', 'index _ link ');
|
Pay attention to the fourth line, to modify according to your own situation, here the sidebar name is called sidebar-1, array 8th key value for links to judge as a link, so it unset off it. How can we know which is the array of links?
| The code is as follows: |
Copy code |
// Display the widget of the link on the homepage only Function index_link ($ widgets ){ Echo '<pre> '; Var_dump ($ widgets ); If (! Is_home ()){ Unset ($ widgets ['sidebar-1'] [8]); } Return $ widgets; } Add_filter ('inclubars _ widgets', 'index _ link ');
|
We only need to print out $ widgets to view the array and find the name of the sidebar used by the current topic. The key value of the chain table is link. After unset, it will be OK.