Archive for the ‘Wordpress Tips’ category

WordPress: Adding Bottom Menu/Navigation

August 11th, 2009

I’m going to keep this one short. Basically my template lacked a bottom menu – This is what I came up with.. You may want to tweak the settings in the wp_list_pages function to include/exclude pages or get sub-pages. See this for help.

Basically, you need to find a good spot to put this following code. I chose to put it underneath inside the “footer” div which appears in my footer.php file – Yours may be different.

	<?php
		$sortby = 'menu_order';

		$out = wp_list_pages( apply_filters( 'widget_pages_args', array( 'title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'link_before' => ' | ') ) );

		if ( !empty( $out ) )
		{
			?>

			<ul id="bottom_nav">
			<li><a href="<?php echo get_option('home'); ?>/">Home </a></li>
			<?php
				echo $out;
			?>
			</ul>
			<?php
		}
	?>

And then you add this code to your styles.css stylesheet:

	#bottom_nav
	{
		list-style-type:none;
		margin:0;
		padding:0 0 8px 0;
		text-align: center;
	}
	/* This style ensures even sub-pages will appear on the same line */
	#bottom_nav li, #bottom_nav ul, #bottom_nav ul li
	{
		display:inline;
	}

Modify this stylesheet to your liking.. you can even take out the seperator “|” character and apply it using CSS. This would require assigning an id to the first list-item (for the home item) so that you don’t apply the sepeartor to that item and then adding a left-border and some padding to the rest of the list items.

Hope this helps.

-Andrew

WordPress ‘Pages Widget’ Fix

August 10th, 2009

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.