WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] redgister_sidebar() help (7 posts)

  1. core-basic
    Blocked
    Posted 1 year ago #

    I tried to name my post 'register_sidebar() to use <h3> instead of <h2>' But apparently there is a very small maximum title length..

    In order to make a theme work with widgets you basically need two things.

    Your functions.php needs this code

    register_sidebar();

    and your sidebar.php needs

    dynamic_sidebar()

    I've added both of those things and they work. My theme works with widgets. By default each title is wrapped with <h2> </h2> heading

    I want my titles to be wrapped <h3> </h3>

    So I made my functions.php look like

    <?
    register_sidebar(array(
      'name' => __( 'right-sidebar' ),
      'id' => 'right-sidebar',
      'description' => __( 'main sidebar' ),
      'before_title' => '<h3>',
      'after_title' => '</h3>'
    ));
    ?>

    and I made my sidebar.php look like

    <?php if ( !dynamic_sidebar('right-sidebar') ) : ?>
    
    <?php endif; ?>

    But my sidebar is not showing up. What could my problem be? Any advice on accomplishing on what I want to do?

  2. alchymyth
    Forum Moderator
    Posted 1 year ago #

    what theme are you modifying?

    do the templates use get_sidebar() ?

    what is the html code output of a page with a sidebar in the browser?

  3. core-basic
    Blocked
    Posted 1 year ago #

    It's a custom theme, I am using get_sidebar(), I am not sure what html output you mean? Do you mean my page.php?

    My page.php is very plain right now. (Just enough to make it work.)

    <?php
    /*
    Template Name: Index
    */
    ?>
    
    <?php get_header(); ?>
    
    <!-- Maincontent start -->
    <section id="content-wrap">
    <section class="content-posts">
    
    <h2><?php the_title(); ?></h2>
    
    <?php while (have_posts()) : the_post(); ?>
    
    <?php the_content(''); ?>
    
    <?php endwhile; ?>
    
    <!-- Maincontent end -->
    </section>
    
    <?php get_sidebar(); ?>
    
    </section>
    <?php get_footer(); ?>

    My sidebar will show up if I simply use

    <?php if ( !dynamic_sidebar('') ) : ?>

    But I'm wanting to use

    <?
    register_sidebar(array(
      'name' => __( 'right-sidebar' ),
      'id' => 'right-sidebar',
      'description' => __( 'main sidebar' ),
      'before_title' => '<h3>',
      'after_title' => '</h3>'
    ));
    ?>

    in my functions.php So I need to call right-sidebar.

  4. alchymyth
    Forum Moderator
    Posted 1 year ago #

    try (without the gettext code) (and with the full php tag):

    <?php
    register_sidebar(array(
      'name' => 'right-sidebar',
      'id' => 'right-sidebar',
      'description' => 'main sidebar',
      'before_title' => '<h3>',
      'after_title' => '</h3>'
    ));
    ?>
  5. core-basic
    Blocked
    Posted 1 year ago #

    Tried it, but no luck. I'm still tinkering with it.

  6. alchymyth
    Forum Moderator
    Posted 1 year ago #

    possibly check:

    http://codex.wordpress.org/Widgetizing_Themes#Another_complete_example

    and use the 'add_action' coding as in the example.

  7. core-basic
    Blocked
    Posted 1 year ago #

    Thanks, great resource. I figured out what had happened, and I feel really silly. At some point the widgets got taken out of my sidebar. I just had to re add them.

    Why would the default widgets that are in place when WP is installed be dumped out? Is it because I am using a custom sidebar? Or because the code change?

    EDIT
    It was the code changing. Appreciate all the help.

    Anyone else who wants to use a different heading other than <h2> for their sidebar titles but do not want to implement a custom sidebar check out the code below. But I do highly suggest reading up on multiple sidebars. It is a very useful feature of WP. (I just wanted to have one font size for my post title, and a second for my sidebar titles. By default WP uses <h2> for sidebar titles. I like to apply my headings in order. <h1> for the head, <h2> for post titles, and then <h3> for my sidebar title. Which is the reason I did this.)

    functions.php

    <?php
    /**
     * Register our sidebars and widgetized areas.
     *
     */
    
    	register_sidebar( array(
    		'before_title' => '<h3>',
    		'after_title' => '</h3>',
    	) );
    
    ?>

    sidebar.php

    <?php
    if ( dynamic_sidebar() ) :
    else :
    ?>
    <?php endif; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.