I would like to show a widget in only one page of my site, as i can see it is not possible right?
Should i add that for all my site pages or not?
I would like to show a widget in only one page of my site, as i can see it is not possible right?
Should i add that for all my site pages or not?
You must be logged in to post a comment.
It depends on where you want to show the widget.
Letâs start with the widget area (sidebar) registration:
This is quite simple. We register a new widget area with some custom markup.
Now we have to show it somewhere. We could add a custom action in our
page.php
template:Or we could use an existing action, this would limit the places where the widget is available. For example the action
loop_start
is called the first time we callthe_post()
in a loop. If we want to set the widget on top of the page content, we use that hook:For a custom action weâd use instead:
There are several plugins that allow to show widgets based on specific conditions:
is_page('about')
An alternative solution is given by the following plugin, which allows you to define a custom set of widgets on a per-page basis, directly from the edit page screen:
If you are comfortable editing the templates files for your theme:
Find where
is called, and either before or after it, or prettymuch anywhere on your site use:
If you want to be cleaner about it: Create a function in functions.php that would look like:
call the function with:
this has to be called in the loop or where the
$post
global variable is existant.Then call that function in your template file (page.php perhaps?) to display your target widgets on only your target page.
You can use
widget_display_callback
: https://developer.wordpress.org/reference/hooks/widget_display_callback/ and just change the page id on the code below.I hope this helps. Cheers!