Skip to primary content

Ken Judy

Trusted Adviser and Coach, Purpose-Oriented Leader

Ken  Judy

Main menu

  • Home
  • About Ken Judy
  • Book List
  • Contact
  • Technical Writing
  • Thanks

Post navigation

← Previous Next →

How to make wordpress sidebars only appear on certain pages for search engine optimization

Posted on December 27, 2009 by Ken Judy

I was reading the recommendations in yoast.com’s post on wordpress seo.

Do you really need to link out to all your buddies in your blogroll site wide? Or is it perhaps wiser to just do that on your front page?

I want to link out on my homepage but I don’t need my sidebar to extend two screens below the content on my individual post pages.

So, it appears desirable to have elements of the sidebar (e.g. the blogroll and external social networking arcana) only appear on the homepage of my blog.

Thanks to some sample code from wpcandy.com, this was trivially easy to implement.

I modified the functions.php in my theme to register three sidebars. I called them Top Right Sidebar, Front Page Sidebar, and Bottom Right Sidebar.


if ( function_exists('register_sidebar') )
register_sidebar(array(
'name' => 'Top Right Sidebar',
'before_widget' => '', // Removes <li>
'after_widget' => '', // Removes </li>
'before_title' => '<h2>',
'after_title' => '</h2>',
));

if ( function_exists('register_sidebar') )
register_sidebar(array(
'name' => 'Front Page Sidebar',
'before_widget' => '', // Removes <li>
'after_widget' => '', // Removes </li>
'before_title' => '<h2>',
'after_title' => '</h2>',
));

if ( function_exists('register_sidebar') )
register_sidebar(array(
'name' => 'Bottom Right Sidebar',
'before_widget' => '', // Removes <li>
'after_widget' => '', // Removes </li>
'before_title' => '<h2>',
'after_title' => '</h2>',
));

Then I modified my sidebar.php to render those three sidebars if the dynamic_sidebar function exists also making the Front Page Sidebar conditional on is_front_page().

<?php
if ( function_exists('dynamic_sidebar') && dynamic_sidebar('Top Right Sidebar') ) : else :
?>
//default markup for the top sidebar goes here
<?php endif; ?>
//renders only if the current page is the front page, i.e. is_front_page()
<?php if ( function_exists('dynamic_sidebar') &&
is_front_page() && dynamic_sidebar('Front Page Sidebar') ) {} ?>
<?php
if ( function_exists('dynamic_sidebar') && dynamic_sidebar('Bottom Right Sidebar') ) : else :
?>
//default markup for the bottom sidebar goes here
<?php endif; ?>

Wordpress Widget Admin PageIn my widgets admin I now have three sidebars listed:

  • They are defined in my theme’s functions.php.
  • They render stacked one on top of the other based on the markup in sidebar.php.
  • I can populate them in the widget admin.
  • The middle bar only appears on the homepage.
This entry was posted in software development and tagged blogroll site, external social networking arcana, php, search engine, search engine optimization, seo, widget, wordpress by Ken Judy. Bookmark the permalink.

About Ken Judy

I am an executive leader, software developer, father and husband trying to do more good than harm. I am an agile practitioner. I say this fully aware I say nothing. Sold as a tool to solve problems, agile is more a set of principles that encourage us to confront problems. Broad adoption of the jargon has not resulted in wide embrace of these principles. I strive to create material and human good by respecting co-workers, telling truth to employers, improving my skills, and caring for the people affected by the software I help build.
View all posts by Ken Judy →
Proudly powered by WordPress