Top

How To: Make your WordPress Search Results Unlimited

June 7, 2008 by Tony · Leave a Comment 

Sometimes you may not want your search results to be limited by the confines of the standard WordPress Loop. This is a quick code hack to allow a search to return unlimited results, altering the standard WordPress Loop by using a custom query. You can do this in a few different ways. If you have a search template, in search.php you can simple add the following line of code above your Loop.

Find:

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

Add:

<?php $posts=query_posts($query_string . '&posts_per_page=-1'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

Make sure you put this code in your search.php only, unless you want unlimited posts on your index or archive pages. The -1 you see can be changed to any positive integer to limit the posts to a number you choose as well.

If you don’t have a search.php in your theme, the next level down in the Template Hierarchy is your Main Index Template, or index.php. You can use a conditional tag for the same effect.

For this we’ll use the same code as above, except wrap it in the is_search() conditional tag, like so:

<?php if (is_search()) { $posts=query_posts($query_string . '&posts_per_page=-1'); } ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

You can use this technique to change the standard Loop limitations of archives, categories, tag pages, and even your main index template - but it would probably be easier to simply change your reading settings for that.

[del.icio.us] [Digg] [Facebook] [Google] [MySpace] [Newsvine] [Reddit] [Slashdot] [StumbleUpon] [Technorati] [Windows Live] [Yahoo!] [Email]

Improve Your Sites Behavior With WordPress Conditional Tags

June 7, 2008 by Tony · Leave a Comment 

If you are a WordPress designer, are you using conditional tags in your themes? For those unfamiliar with them, conditional tags are snippets of PHP code you can easily use to set up conditions where the code is used. The most common use for these would be for your sidebar or your pre/post meta sections of your posts where you display your post details. With conditional tags, you can display certain functions only on pages of your choice. A couple of months ago we talked a little bit about WordPress conditional tags on this site and when to use them, but we didn’t get into a great amount of detail.

Leland over at Theme Lab has been working on some great WordPress guides lately, and one of his recent posts that caught my attention is the Ultimate Guide to WordPress Conditional Tags. This post provides a more thorough explanation of conditional tags and how to use them. If you’ve been wanting to improve your coding, this would be a great post to check out.

[del.icio.us] [Digg] [Facebook] [Google] [MySpace] [Newsvine] [Reddit] [Slashdot] [StumbleUpon] [Technorati] [Windows Live] [Yahoo!] [Email]

WordPress Hack: Using WordPress Conditional Tags

June 7, 2008 by Tony · 1 Comment 

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?

[del.icio.us] [Digg] [Facebook] [Google] [MySpace] [Newsvine] [Reddit] [Slashdot] [StumbleUpon] [Technorati] [Windows Live] [Yahoo!] [Email]

Bottom