If you’ve been following the recent trends with premium WordPress themes, you’ve probably noticed that the two-tiered navigation menu has become extremely popular. An example of this is the popular Revolution WordPress themes. This style of menu can be used in a variety of ways, but the most common seems to be to display your children pages in a menu below your blog’s parent pages.
If you’ve wondered how to hack your existing WordPress theme to accomplish something along these lines, Darren Hoyt has taken the time to explain this in his post Creating Two-Tiered Conditional Navigation in WordPress. Here is the PHP you’ll need:
<ul id="nav">
<?php wp_list_pages('title_li=&depth=1'); ?>
</ul>
<?php if($post->post_parent)
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) { ?>
<ul id="subnav">
<?php echo $children; ?>
</ul>
<?php } else { ?>
<?php } ?>
Once you’ve got the code in place, you’ll want to style it to look the way you want it to. Click over to Darren’s post to get the styling information!
For most of us, traffic is the driving force behind our blogs and motivation to blog. Therefore, it may seem silly to think that you would want to prevent a lot of potential traffic from Google’s image search.
However, some bloggers like to post personal pictures, or custom make their pictures and don’t want others to take them when possible. If you fit into this category, you can easily prevent Google from indexing your pictures by placing the following code into your blog’s header file above the < /head > tag:
<meta name="robots" content="noimageindex">
If your site has a problem with people taking your content (including the pictures), then there is a chance Google will still index them when they index that person’s website. Another route you can take is to place images into a folder then add a disallow to your Robots.txt file. For WordPress users, this is fairly easy as by default, we already have pictures in either our Images folder of our theme, or the uploads folder (unless you’ve assigned a custom path for your images). You can add something like the following to your Robots.txt file:
User-agent: *
Disallow: /images/
or
User-agent: *
Disallow: /uploads/
[via]
One thing I’ve always loved to do is set up my blogs comments to display alternating background colors (although ironically I have not done so for this site yet). I’ve always felt that this helps give the comments a little more separation from each other and improve the overall look of the blog.
Because WordPress is full of loops (homepage, category, tag, etc.), this same theory can truly be applied all over your blog. David of CyberCoder posted yesterday a great hack to alternate the background of your blog posts. Here are the steps you need to take:
Create a loop counter. Right before the Loop begins set a variable to zero.
<?php
$x=0;
if (have_posts()) : ?>At this point I would want the Background Color to change, insert code to test for an even number post, and if this is true, add an inline CSS style for the background that would override the stylesheet.
<?php if ($odd = $x%2){?>
<div style=”background-color:#404040;padding:10px 0px 10px 10px;”>
<?php }?>Then, where the background color should return to the original state, run the same test for the even numbered post and close the div tag set by the Inline Style.
<?php if( $odd = $x%2 ){?>
</div>
<?php }?>The final step is to add 1 to the counter for each post, which is done at the end of the loop.
<?php
$x++;
endwhile; ?>
Thanks for sharing this trick David!
I recently got an e-mail from a reader wanting to know how to hide their sub-categories in WordPress. If you use WordPress 2.1 or newer, this is actually really easy to do.
All you need to do is open your stylesheet and add the following anywhere:
.children {
display:none;
}
You can also style the children accordingly this way if you’d like!
Have you ever noticed how some pages on blogs behave different than on other pages? One of the most under-utilised features of WordPress is the wide variety of conditional tags available in WordPress. Through the use of conditional tags, you can instruct certain plugins, pictures, or code to only appear on designated pages.
Recently I ran across a post by the Undersigned explaining Conditional Tags in WordPress, which appears was written in 2006 but is still valid with current versions of WordPress.
Here is a list of the conditional tags available:
- is_home()
- is_single()
- is_page()
- is_category()
- is_author()
- is_date()
- is_year()
- is_month()
- is_day()
- is_time()
- is_archive()
- is_search()
- is_paged()
- is_404()
One of my favorite places to use conditional tags is in the post meta area. For example, on most pages I want the comments button to display, but I don’t need it to appear on the single page because the comments are displayed on that page. At the same time, I like having an edit button on the single page, but I don’t need it on any other pages.
Here is the code I use for the above example on one of my sites:
<?php if (is_single()){ ?> <?php edit_post_link(__(”*Edit*”), ”); ?> <?php } else { ?> | <?php comments_popup_link(’0 comments’, ‘1 comment’, ‘% comments’); ?> <?php } ?>
The bold code is the conditional tags I’ve set up. You can see from the above code that I am telling WordPress to only display an Edit button on single pages, and on all other pages display the comments link.
What other good uses have you found for conditional tags?
Roughly four months ago I talked a bit about how to set up your blog so only the admin can see an edit button on blog posts. For whatever reason, I’ve always set this up on each of my websites and really get a lot of use out of it. Much along those same lines, Michael of WPCandy has written a post explaining how to display content so only the admin can view it. This goes well beyond the edit button and allows you to setup just about anything to display for the admin.
In hist post, Michael provides some code that looks at the user id to determine if the user has admin privileges and then displays whatever you tell it to for that user. Here is the code:
<?php global $user_ID; if( $user_ID ) : ?>
<?php if( current_user_can('level_10') ) : ?>
<a href=”http://yourdomainurl.com/stats/“>Stats</a>
<?php else : ?>
<?php endif; ?>
<?php endif; ?>
If you run a multi-author blog and would like to make this stuff available to editors, authors, contributors, and more, you can easily adjust the user level to determine who sees your display.
Great find Michael!











