Sidebar does not show widgets but individual sidebar content - php

My dynamic sidebar does not show standard widgets like recent posts or archives but the individual content I put into the sidebar.php
This is the sidebar-part from my functions.php:
add_action( 'widgets_init', 'my_register_sidebars' );
function my_register_sidebars() {
register_sidebar( array(
'name' => __( 'Primäre Sidebar' ),
'id' => 'sidebar-main',
'description' => __( 'Rechte Hauptsidebar für Werbeflächen und Meta' ),
'before_title' => '<h4>',
'after_title' => '</h4>',
) );
and this is the code from sidebar.php:
<aside id="sidebar-main">
<div id="widgetarea">
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : endif; ?>
<h4>TESTESTEST</h4>
<p>HALLOHALLO</p>
</div>
</aside>
As always, I call the sidebar by <?php get_sidebar(); ?> in my theme.
How come the other stuff is being displayed but not the widgets? I have no clue.
Edit: I already tried deactivating plugins - no change.

Your call to your sidebar is wrong. This
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : endif; ?>
should be
<?php if ( is_active_sidebar( 'sidebar-main' ) ) : ?>
<?php dynamic_sidebar( 'sidebar-main' ); ?>
<?php endif; ?>

try this in sidebar.php:
<aside id="sidebar-main">
<div id="widgetarea">
<?php dynamic_sidebar('Primäre Sidebar') ?>
</div>
</aside>

Related

Sidebar is appearing on custom post pages without being called

I'm creating a Wordpress theme for a graphic portfolio website, and I'm utilizing the sidebar.php for a hero image on the index page that is not meant to appear on other pages. While this works for all static inside pages, I have two custom post categories, and the sidebar section has started appearing above those posts.
I should note that this hasn't always been a problem, and started cropping up for no apparent reason while working on something else.
On index.php, the sidebar is called as follows:
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile; ?>
<nav>
<ul class="pager">
<li><?php next_posts_link( 'Previous' ); ?></li>
<li><?php previous_posts_link( 'Next' ); ?></li>
</ul>
</nav>
<?php endif;?>
<?php get_footer(); ?>
While page.php is similar but lacks the php line calling the sidebar, as follows:
<?php get_header(); ?>
<div class="row">
<div class="col-sm-12">
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile; endif;?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<?php get_footer(); ?>
page-fine-art.php also lacks that php line, as follows:
<div class="row">
<div class="col-sm-12">
<?php
$args = array(
'post_type' => 'fine-art',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$custom_query = new WP_Query( $args );
while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
<div class="blog-post">
<h2 class="blog-post-title"><? php the_title(); ?></h2>
<p class="blog-post-meta"><?php the_date(); ?> by <?php the_author(); ?></p>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
<?php the_excerpt(); ?>
<?php endwhile; ?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<?php get_footer(); ?>
The function creating the the custom posts for "fine art" goes:
function create_my_custom_posts() {
register_post_type( 'fine-art',
array(
'labels' => array(
'name' => __( 'Fine Art' ),
'singular_name' => __( 'Fine Art' ),
),
'public' => true,
'has_archive' => true,
'supports' => array(
'title',
'editor',
'thumbnail',
'custom-fields'
)
));
I don't see where sidebar.php is being called in the final example, or where else it could be coming from.

Wordpress: Adding a widget to the main body (not the sidebar), widget not showing up

I followed the instructions here for how to add a widget to a template. I added this code to functions.php:
function arphabet_widgets_init() {
register_sidebar( array(
'name' => 'Carousel',
'id' => 'carousel_widget',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'arphabet_widgets_init' );
and I added this to my custom template:
<?php if ( is_active_sidebar( 'carousel_widget' ) ) : ?>
<div id="carousel-widget" class="carousel widget-area">
<?php dynamic_sidebar( 'carousel_widget' ); ?>
</div>
<?php endif; ?>
It worked, but my new widget showed up as a sidebar, and I want it to be in the main body of the page. Obviously this is because of the dynamic_sidebar function, but the documentation is not very helpful on how to place a widget on the page NOT as a sidebar.
I tried replaced the php with this:
<div id="carousel-widget" class="carousel widget-area">
<?php the_widget( 'carousel_widget' ); ?>
</div>
But now it doesn't show up at all.
How do I place this widget on my page without it showing up as an absolutely positioned sidebar?

WordPress: Adding a widget area to the primary page template?

I'm trying to add a widget area to the main body of page.php, so that I can place unique widgets on each page of my site. I've followed the instructions here: http://codex.wordpress.org/Widgetizing_Themes
I added this to functions.php in my theme:
function arphabet_widgets_init() {
register_sidebar( array(
'name' => 'Main widget',
'id' => 'main_widget_area',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'arphabet_widgets_init' );
and this is page.php:
get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php if ( is_active_sidebar( 'main_widget_area' ) ) : ?>
<div id="main-widget" class="widget-area main-widget">
<?php dynamic_sidebar( 'main_widget_area' ); ?>
</div><!-- #primary-sidebar -->
<?php endif; ?>
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
</div><!-- .entry-content -->
<footer class="entry-meta">
<?php edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>' ); ?>
</footer><!-- .entry-meta -->
</article><!-- #post -->
<?php comments_template(); ?>
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
(I added the top section, with main_widget_area)
However when I edit the page I see no way to drop in a widget. I'm not sure if I'm missing a step, or if I'm just not editing the page in the right way.
Take a look at the "Widgets" menu underneath "Appearance" in the WordPress backend. There will be a list of widgets on the left side of the page and a list of available sidebars on the right. Just drag-and-drop the widget you want into the sidebar you registered and you should be good to go.

Appy Wordpress Widget to Widget area

I'm working the first time with WordPress, now I'm trying to add a custom Widget to an HTML box.
The HTML: The blank News box
I started to read a bit about widgets and the tutorials told me to create a new Widget area inside my functions.php document, which i did with the following code:
<?php
/**
* Register our sidebars and widgetized areas.
*
*/
function arphabet_widgets_init() {
register_sidebar( array(
'name' => 'Home right sidebar',
'id' => 'home_right_1',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'arphabet_widgets_init' );
?>
Now i went into my index.php document, which stores the HTML so I tried to add the following code to the HTML blank news box:
<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
<div id="primary-sidebar" class="primary-sidebar widget-area" role="complementary">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</div><!-- #primary-sidebar -->
<?php endif; ?>
After i finished this also, i went into my WordPress admin menu and applied the plugin to the widget.
But it wouldn't show up...
Does anyone have a idea why?
update your index.php code as below:
<?php if ( is_active_sidebar( 'home_right_1' ) ) : ?>
<div id="primary-sidebar" class="primary-sidebar widget-area" role="complementary">
<?php dynamic_sidebar( 'home_right_1' ); ?>
</div><!-- #primary-sidebar -->
<?php endif; ?>

New Wordpress theme is not showing widgets in admin area

I am creating new Wordpress theme. It's working but not showing widgets bar in admin panel.
Here is my code:
<?php get_header(); ?>
<div class="wrapper">
<!--Navigation start-->
<div class="navigation_content">
<nav>
<ul>
<?php $args = array(
'depth' => 0,
'sort_column' => 'menu_order, post_title',
'menu_class' => 'menu',
'include' => '',
'exclude' => '',
'echo' => true,
'show_home' => true,
'link_before' => '',
'link_after' => '' );
?>
<li class=""><?php wp_page_menu( $args ); ?></li>
</ul>
</nav>
</div>
<!--Navigation start-->
<!-- body content start-->
<div class="body_content">
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<!--end post header-->
<div class="entry clear">
<?php if ( function_exists( 'add_theme_support' ) ) the_post_thumbnail(); ?>
<p><?php the_content(); ?></p>
<?php //edit_post_link(); ?>
<?php wp_link_pages(); ?>
</div><!--end entry-->
</div><!--end post-->
<?php endwhile; /* rewind or continue if all posts have been fetched */ ?>
<?php else : ?>
<?php endif; ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
And here is my function file code to register widgets:
function ccp_widgets_init() {
register_sidebar( array(
'name' => __( 'Main Widget Area', 'ccp' ),
'id' => 'sidebar-1',
'description' => __( 'Appears in the footer section of the site.', 'ccp' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
Am i missing code?
Thanks
Add the following code in functions.php after your ccp_widgets_init function.
add_action( 'widgets_init', 'ccp_widgets_init' );
There is another way.
Definitely use a child theme
In your child-theme folder:
Create a folder called widgets
Put your widget in there
ie.
wp-content/themes/my-child-theme/widgets/my_widget.php
at the end of your widget there is no need to 'hook' it into any action like this as most posts say
function register__my_widget() {
register_widget( 'my_widget_class_name_exends_WP_Widget_class' );
}
add_action( 'widgets_init', 'register__my_widget' );
So just register it as normal at the end with a single line:-
register_widget( 'my_widget_class_name_exends_WP_Widget_class' ) see below:
<?php function get_recent_post( $beforewidget, $afterwidget, $beforetitle, $aftertitle ){ ?>
<?php echo $beforewidget; ?>
<?php echo $beforetitle; ?>Recent Posts<?php echo $aftertitle; ?>
<ul class="rp-widget">
<?php query_posts( array ( 'category_name' => 'blog', 'posts_per_page' => -1 ) );
while ( have_posts() ) : the_post(); ?>
<li>....
....very boring widget code, yawn...
...instance ) {
// outputs the content of the widget
get_recent_post( $args['before_widget'], $args['after_widget'], $args['before_title'], $args['after_title'] );
}
}
register_widget( 'my_widget_class_name_exends_WP_Widget_class' );
The important bit is the last line - the "register_widget" bit.
Nb. all widgets extend WP_widget class (I think it's a class - would be in java/c++) so you register your class name
3.Then in your child-themes's functions.php add this line
get_template_part('widgets/my_widget');
and you should be groovy!
Nb:
The line added to your child-theme functions.php should be added outside of any function
The path should exist ie. widgets/
It should be the name of your file MINUS the .php part
(It is not essential to use a separate folder but it helps keep code organised and so if you have added a widgets folder in your child theme and put my_widget.php in there then point is valid.
IF you didn't add a widgets folder to your child-theme folder then you would just use
get_template_part('my_widget');
in your child functions.php
)
What's the advantage of doing it this way??
You can separate your code to be more modular
You don't end up having a monster sized functions.php file
It should be easier to maintain or modify

Categories