WordPress sidebar text widget - php

i'm using the Showcase Sidebar with a Text widget. I've inserting some HTML and Text and want that Text widget to display. What PHP code do I insert into the sidebar.php template to show the Text widget and any other ones that I add? Ideal I would like to only load the first Text Widget in from the Showcase Sidebar
would the code look something like this?
<?php if ( !function_exists('dynamic_sidebar')
|| !dynamic_sidebar('sidebar2') ) : ?>
<?php endif; ?>

yes, it would look something like code you posted:
<?php if ( !function_exists('dynamic_sidebar')
|| !dynamic_sidebar('sidebar2') ) : ?>
<?php endif; ?>
, but -
The code that you posted will only work if you have defined and registered such a sidebar (named : sidebar2)
in your themeĀ“s php you should have a register_sidebar() function call.
if (function_exists('register_sidebar')) {
register_sidebar(array(
'name'=> 'Sidebar 2',
'id' => 'sidebar2',
));
}
you can even customize it further with other parameters like .
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => '</li>',
'before_title' => '<h2 class="offscreen">',
'after_title' => '</h2>',
If your theme already have such a sidebar defined and registered , then the code you have posted will work and the front side will display all the widgets that you put to that sidebar (on the admin side) .
If it is not defined by your theme you will need to define it or ADD it to the definitions of other sidebars .
Read more on the codex here and here about defining sidebars.

Related

How to show different content in the sidebar on a static home page

I'm a Wordpress noob and trying to figure out the best way to show different content in the side bar only on home page. I have set the front page to be static and right now it shows the usual widgets from the blog posts page. I just need to show some custom content in the sidebar only on the home page. So far I have found solutions to install plugins, multiple sidebars etc. I was thinking more along the lines of having some conditions to show different content within the existing sidebar. Can someone please advise how can I achieve this task?
Thanks!
simple way is to edit sidebar.php file and find where you want to add sidebar (for example at the top) and check if is_front_page() then use a function to check if is_active_sidebar('frontpage_sidebar') and else the rest of global widgets.
after that you should register a sidebar named frontpage_sidebar.
functions.php
$args = array(
'name' => __( 'Sidebar name', 'theme_text_domain' ),
'id' => 'frontpage_sidebar', // ID should be LOWERCASE ! ! !
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>' );
register_sidebar($args);
sidebar.php
if(is_front_page()){
if(is_active_sidebar('frontpage_sidebar')){
dynamic_sidebar('frontpage_sidebar');
}else{
echo 'please set a sidebar for your frontpage';
}
}else{
if(is_active_sidebar('sidebar')){
dynamic_sidebar('sidebar');
}else{
echo 'please set default widgets for whole pages';
}
}
i didn't check this code, so please check and let me know if it works.

Missing Widgets in Wordpress dashboard?

I'm working on creating a WordPress blog using my own custom theme and am having difficulty displaying widgets in the sidebar. I have registered my sidebar in functions.php, and am using get_sidebar(); in index.php. If I put content in sidebar.php it will correctly display in the sidebar, but I want to be able to modify the sidebar dynamically. However, when I go to Appearance > Widgets, it shows my sidebar name, but no widgets are listed in the "Available Widgets" section.
This is where I register my sidebar in functions.php:
function blog_widgets_init() {
register_sidebar( array(
'name' => esc_html__( 'Sidebar', 'blog' ),
'id' => 'sidebar-1',
'description' => '',
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h4 class="widget-title">',
'after_title' => '</h4>',
) );
}
add_action( 'widgets_init', 'blog_widgets_init' );
This is where I call the sidebar in index.php:
<?php get_sidebar(); ?>
And this is sidebar.php:
<?php
if ( ! is_active_sidebar( 'sidebar-1' ) ) {
return;
}
?>
<div id="secondary" class="widget-area" role="complementary">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</div>
Any ideas why there no widgets available in Appearance > Widgets?
Edit: Solved it! There was actually a place in my code where the default widgets were being disabled. I didn't realise that was there, as it must've been included in the theme that I was basing mine off of. Removed those functions and the widgets show up again.
I think part of the issue is the call to is_active_sidebar()
From the documentation of that function:
Any sidebar that contains widgets will return TRUE, whereas any
sidebar that does not contain any widgets will return FALSE.
So your first line, since that sidebar contains no widgets (yet), will always just "return" and end any further processing in sidebar.php. I'd probably just remove that whole block all together.
I don't exactly know why you're not seeing widgets show up in the admin area, but I'd be curious what happens if you retry after the above change.

Replace sidebar for child pages without plugin

I'm trying to get a different sidebar to load within child pages of a particular parent without the use of a plugin or setting up another template file.
This is what I have so far:
register_sidebars(1, array(
'name' => 'Other Sidebar',
'id' => "other-sidebar",
'before_widget' => '<li class="widget">',
'after_widget' => '</li>',
'before_title' => '<span class="widgettitle">',
'after_title' => '</span>'
));
if ( $post->post_parent == '1164' ) {
dynamic_sidebar( 'other-sidebar' );
}
But I'm wondering if I need use a filter of some sort to replace the default sidebar that is being loaded instead? Not too sure if that's correct or not.
Since you can edit the theme (or child theme) you could add a Page Template that would override the current page template with the custom sidebar you want.
<?php
/*
Template Name: Custom Sidebar Page // All versions
Template Post Type: page // 4.7+ only
*/
// Page code here with the sidebar you want...
?>
<aside id="secondary" class="widget-area" role="complementary">
<?php dynamic_sidebar( 'other-sidebar' ); ?>
</aside><!-- #secondary -->
Then, in the edit for your page and it's child pages, set this template as the one you want.

Wordpress Template. Display contents of a Text Widget

I have created a wordpress template.
I have added some HTML into a Text widget and saved it.
Is there a way to add some code into my template so I can get that widget to be displayed?
Is this possible instead of having to enter the html code directly into my template?
Your Text widget will be inside of a sidebar. So if you have a sidebar called "left-sidebar", you can use code like this in your template.
<?php if ( is_active_sidebar( 'left-sidebar' ) ) : ?>
<ul id="sidebar">
<?php dynamic_sidebar( 'left-sidebar' ); ?>
</ul>
<?php endif; ?>
Now all the widgets you put in left-sidebar will be included in that template automatically.
There's more information about dynamic_sidebar here: https://codex.wordpress.org/Function_Reference/dynamic_sidebar
Note that you can also create new sidebars using register_sidebar in your functions.php, so if you want to use different sidebars in different templates you can. https://codex.wordpress.org/Function_Reference/register_sidebar
Yes it is infact it's very simple, you have to create a sidebar for where you wan that widget to go. In your main php file or wherever you want that widget to appear you can add the following code:
<?php if ( !function_exists('dynamic_sidebar')
|| !dynamic_sidebar('sidebar1') ) : ?>
<?php endif; ?>
Then you would also have to declare that sidebar within the functions.php file like so:
if ( function_exists('register_sidebar') )
register_sidebar(array('name'=>'sidebar1',
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
));
and you can have as many sidebars that you want, just give them a different name like sidebar2 etc..

WordPress Theme from scratch, widgets don't work

I know this topic has been discussed quite a bit but I've tried everything I can find online and nothing is working. I need a single widget area in my custom template. I wrote if from scratch and didn't use a barebones type starter therefore I didn't have a functions.php to start from.
In wp-admin it shows my widget as it should but when there is a widget in that area and the page is reloaded it resets. in other words the widget doesn't persist in the area it's supposed to. am I missing something? is my wordpress install bad?
here is my functions.php in it's entirety
<?php
register_sidebar
(
array(
'name' => 'Header Widget',
'id' => 'headerBanner',
'before_widget' => '<div id="banner">',
'after_widget' => '</div>',
'before_title' => '',
'after_title' => '',
)
)
?>
and my index.php
<?php if ( !dynamic_sidebar (1)) : ?>
<h1>it didn't work</h1>
<?php endif; ?>
try:
<?php if(!dynamic_sidebar ('headerBanner')) : ?>
<h1>it doesn't work</h1>
<?php endif; ?>
You need to pass the id of the sidebar you want to display when calling dynamic_sidebar
Also note that dynamic_sidebar will return false if there are no widgets added to the sidebar so add a widget to it and see if it works
Have you tried this other approach?
<?php dynamic_sidebar( 'headerBanner' ); ?>

Categories