wordpress goodies

I needed to have custom post type taxonomies (custom categories, really) but 2 of those would have only one post each, so instead of listing post title with a link, the entire post should display. This little tidbit goes in the category template works to show content if there is only one post, but the title and link if there is more than one post:

<?php if ($wp_query->found_posts == 1) : 
            echo get_field('member_content'); 
          else: ?>
            <h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
        <?php endif; ?>

It uses “get_field” because it’s pulling from a custom field instead of the default “the_content()” field.

But there was a hitch, because the second category had a single post but it uses a template part for it’s content. So it wasn’t showing anything. Luckily, I found this tidbit, that goes in the functions.php file:

        function stf_redirect_to_post(){
    global $wp_query;
 
    // If there is one post on archive page
    if( is_archive() && $wp_query->post_count == 1 ){
        // Setup post data
        the_post(); 
        // Get permalink
        $post_url = get_permalink();
        // Redirect to post page
        wp_redirect( $post_url );
    }   
 
} add_action('template_redirect', 'stf_redirect_to_post');

and this redirects categories that have just one post to the post’s page, instead of pulling content onto the category page (which is better anyway).

The second bit of code came from here. The first bit was found in the wordpress forums many web searches ago.

nice little bit to fix tag archive

Was having a problem with tag archive not pulling from custom post types, or from pages (I added a plugin to add tags to pages). After much searching and minor hair loss, I found this:

function post_type_tags_fix($request) { if ( isset($request['tag']) && !isset($request['post_type']) ) $request['post_type'] = 'any'; return $request; } add_filter('request', 'post_type_tags_fix');

from here:

http://wordpress.stackexchange.com/questions/13237/custom-post-type-tag-archives-dont-work-for-basic-loop

And now the archives work. Added that bit of code to functions.php to get it to work. Using WordPress 3.4.1, surprisingly enough.

hide form option using just css

OK, I know this is cheater. But I don’t know javascript/jquery/whatever, and the plugin people didn’t care to offer this help even as a paid ticket. And this is a deadline here, people. So, after a bit of digging and a very small amount of sweat, I tried this and it WORKS:

form.product select#quantity-48 option:nth-child(n+11){display:none;}

Basically, I’m saying on this form, for this one particular product (number 48) only show me the ability to select up to 10 items.

It doesn’t affect any other product or form, and is not part of the plugin so updates won’t change this in the future. Woot!

the challenge of categories with extra info, and displaying subcategories

I love working with WordPress, but it seems each project comes with an unforseen challenge. This was a new problem: how to only display subcategories on a parent category page, no posts. Because I’m more of a cut-and-paster than a coder, I rely on google to help find solutions. What I was looking for:
– what category are we in? if this category has sub-categories, show those, and if this category has no sub-categories, show posts.
Couldn’t find anything that includes finding the current category. So, I created a new template just for the one category that had subcategories.
This bit of code:
'mycategoryname' , 'post_type' => 'mycustomposttypename',);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>

wp_reset_query(); ?>
does what I need.