Wordpress Template. Display contents of a Text Widget - php

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..

Related

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 - dynamic sidebar will not hold any widgets

Here is the code in functions.php:
register_sidebar(array(
'name'=> 'Home Page Advanced Search',
'id' => 'custom'
));
register_sidebar(array(
'name'=> 'Profile Picture Upload',
'id' => 'pic upload'
));
Then, I use the two sidebars in my template like this:
?php if ( is_active_sidebar( 'custom' )) : ?>
<div id="widget-area">
<?php dynamic_sidebar( 'custom' ); ?>
</div>
<?php endif; ?>
<?php if ( is_active_sidebar( 'pic upload' )) : ?>
<div id="upload-photo-area">
<?php dynamic_sidebar( 'pic upload' ); ?>
</div>
<?php endif; ?>
The sidebar with id='custom' works fine. I am able to put widgets in it in my dashboard and display them. The sidebar with id='pic upload' is not working correctly. All widgets that I put inside of it do not stay there (in the dashboard API) and, therefore, do not display on my page.
All help is appreciated. Thanks in advance.
You can't have a space in the name of a sidebar. As per the codex
If you name your own ID values in the register_sidebar() WordPress
function, you might increase readability of the code. The ID should be
all lowercase alphanumeric characters and not contain white space. You
can also use the - and _ characters. IDs must be unique and cannot
match a sidebar name. Using your own IDs can also make the sidebar
name translatable.
I added the emphases.
Change pic upload to pic-upload or pic_upload should fix that.

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' ); ?>

WordPress sidebar text widget

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.

Categories