Basically, this quick post will be regarding the ‘Pages‘ widget, which, in hindsight, can be used in conjunction with wordpress “pages” in order to achieve a Content-Managed style site – with dynamic menu creation (in the sidebar in this instance).
My issue involved a few things.
Firstly: if I were to add a “Home” wordpress page, I’d be forced to name it “Home” in order to keep that name in the main menu. This was an undesired result, especially because most templates, including the one in question, by default, include a “Home” link anyway which links to the default page for wordpress to show (whether it be a page or the page where your blog posts are shown .. see Settings -> Reading in order to configure this).. And for my home page, I prefer the first heading to be something along the lines of “Welcome to xyz” or “Xyz Company” etc.. but at the same time I want to keep the main menu item as “Home” – this unfortuately required a workaround + hack.
The first thing I had to do was create a blog post as a ‘psuedo-home-page’ instead of simply just using a page. Then I had to make changes to the appropriate php files in order to remove unwanted elements (comments area, date/time, author etc..) – this may vary depending on your template – it is also out of the scope of this article.
The next thing was to make sure all the pages were in correct order – and that my “blog post page” was set up to be the home page (Settings -> Reading)..
The third thing was to find out where the pages-widget code was and put an extra line of code in to insert the “Home” link above all the other page links.
The file we need to edit can be found in wp-includes/default-widgets.php
Edit this file and find the function WP_Widget_Pages()
We then need to modify this code below..
function widget( $args, $instance )
{
extract( $args );
$title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Pages' ) : $instance['title']);
$sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby'];
$exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude'];
if ( $sortby == 'menu_order' )
$sortby = 'menu_order, post_title';
$out = wp_list_pages( apply_filters('widget_pages_args', array('title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'exclude' => $exclude) ) );
if ( !empty( $out ) )
{
echo $before_widget;
if ( $title)
echo $before_title . $title . $after_title;
?>
<ul>
<?php echo $out; ?>
</ul>
<?php
echo $after_widget;
}
}
You now need to edit the line <?php echo $out; ?> and insert some code BEFORE it.
This code will grab the default home page, whichever you set it to in the settings area
and create a list item accordingly. The code is:
<li><a href="<?php echo get_option('home'); ?>/">Home </a></li>
Your code should now look like this:
function widget( $args, $instance )
{
extract( $args );
$title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Pages' ) : $instance['title']);
$sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby'];
$exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude'];
if ( $sortby == 'menu_order' )
$sortby = 'menu_order, post_title';
$out = wp_list_pages( apply_filters('widget_pages_args', array('title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'exclude' => $exclude) ) );
if ( !empty( $out ) )
{
echo $before_widget;
if ( $title)
echo $before_title . $title . $after_title;
?>
<ul>
<li><a href="<?php echo get_option('home'); ?>/">Home </a></li>
<?php echo $out; ?>
</ul>
<?php
echo $after_widget;
}
}
Save the file and that’s it. You now have a valid Home link in your pages widget.