Martin C
Martin C

Reputation: 395

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

Upvotes: 0

Views: 8550

Answers (2)

deemyBoy
deemyBoy

Reputation: 65

There is another way.

Definitely use a child theme

In your child-theme folder:

  1. Create a folder called widgets
  2. 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:

  1. The line added to your child-theme functions.php should be added outside of any function
  2. The path should exist ie. widgets/
  3. 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

Upvotes: 0

saifur
saifur

Reputation: 637

Add the following code in functions.php after your ccp_widgets_init function.

add_action( 'widgets_init', 'ccp_widgets_init' );

Upvotes: 1

Related Questions