SDAC Blog

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

hr
hr

If you ever wanted to only show the content of the category you were on in the category view of WordPress (instead of the category you are on plus all of it's subcategories - you can easily do so by adding the code below to the template that is used to show your category content

You would start your loop with this:

PHP:
<?php $cat = intval( get_query_var('cat') ); while (have_posts()) : the_post(); if (in_category($cat) ) : ?>

Put your title, content functions here

and where you normally see the

PHP:
<?php endwhile;?>

call, put

PHP:
<?php endif;endwhile; ?>

to close the additional (in_category) if statement.

This code gets the current category and then only displays categories that are in that category (not sub category)

separate
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