SDAC Blog

Check out our blog for WordPress updates, WordPress plugin reviews, and general web development hints.

hr
hr

If you would like to exclude one post from your blog, archive, search results or wherever you need to on your blog, you can do so by putting in a small piece of code within the particular WordPress loop:

The following example will show all posts except for the post with the ID of 179:

PHP:
<?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == '179' ) continue; ?>
<?php the_content();?>
<?php endwhile;endif;?>

This is something that I get a lot of requests for and is very useful in a number of situations.

separate
hr
hr

If you ever need to exclude a single category from a WordPress page (archives, index, category page, etc) you can easily do so by using a little conditional tag code within the WordPress loop.

The example below will skip over any post that is in the category with the ID of 35:

PHP:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if (in_category('35')) continue; ?>
<?php the_content(); ?>
<?php endwhile;endif;?>

This can be helpful if you want to not show a particular category in your blog (if you have a category based site setup) - or if you want to hide some categories from your search results.

separate
hr
hr

I needed to be able filter out the text "Private:" for posts and pages that were password protected but did not want to edit any core WordPress files. To filter out that text, I added the following code into my functions.php file (within my theme directory):

PHP:
function remove_private_prefix($title) {
$title = str_replace(
'Private:',
'',
$title);
return $title;
}
add_filter('the_title','remove_private_prefix');

(I also posted this filter in the WordPress Support Forum)

separate
hr
hr

There are several tidbits of code that I have collected over the past few years that make it easier and easier to turn a simple WordPress installation into a very functional content management system (CMS). There are many times when it would be nice to show logged in members certain bits of information (certain categories, posts, or just a simple "Welcome back!") and of course there is a simple way of doing this:

PHP:
<?php if (is_user_logged_in()): ?>

That bit of code will allow you to do something like this which will allow you to show the logged in user's preferred user name (selected in the User preferences), and then a list of member only pages (private page parent is page ID 20 - I am showing all sub pages of the members-only pages marked as private). If the user is not logged in, they will get a Welcome visitor! greeting :

PHP:
<?php if (is_user_logged_in()) : ?>
    <?php global $user_identity;get_currentuserinfo();?>
   
    <p>Welcome back <?php echo $user_identity;?></p>
    <p>Here is a list of private pages only viewable by Members:</p>
        <ul>
            <?php wp_list_pages('title_li=&child_of=20'); ?>
        </ul>
<?php else :?>
    Welcome, visitor!
<?php endif;?>

You can make it as simple as a change in greeting for members and non-members, or put in specific logic like I did with showing the pages. The potential is limitless.

separate
hr
hr

There are some times when you want to limit your search to a particular category, or perhaps multiple categories. This is relatively simple to do in WordPress by adding a hidden field to your search code. (See example below)

PHP:
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<div><input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
<input type="hidden" name="cat" value="22" />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>

You can see I added my hidden input field on the third line. When I add this in, it then adds onto the query used to search. Your search will go from something like http://www.sandboxdev.com/?s=WordPress to http://www.sandboxdev.com/?s=WordPress&cat=22 and will only return posts in the category ID you choose.

separate