• Resolved wscheer

    (@wscheer)


    So I have a site using the sidebar to display a all the posts in a specific category. I noticed that this only worked on certain categories. After a little investigation, I discovered that by shortening the length of nonworking categories, I could get them to work.
    Perhaps I’m going about this the wrong way, but here is my code:

    <?php //get the current category and put into $category var
    $category = get_the_category();
    echo "<h2 class='headline'>".$category[0]->cat_name."</h2>";
    // some posts are in multiple categories, i'll just grab the first one
    $cat = $category[0]->cat_name;
    ?>
    
    <?php $recent = new WP_Query(); ?>
    
    // for testing what the 'get_the_category()' output was
    <?php echo "cat: ".$cat; ?>
    
    // this was working for some categories but not others
    <?php $recent->query('category_name='.$cat.'&showposts=-1'); ?>
    
    <?php while($recent->have_posts()) : $recent->the_post(); ?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
    <?php endwhile; ?>

    So basically here is what I was able to determine:
    category name: condos & beach houses —- too long
    category name: condos and beach houses — too long
    category name: condos beach houses —— works ok

    weird?

    anyone else ever come across this?

Viewing 2 replies - 1 through 2 (of 2 total)
  • I’ve had sites with much longer catgeory titles/slugs than that and not had a single problem. From what I can tell, the relvant db field in wp_terms can hold up to 200 characters.

    Are you pasting any of these titles in from elsewhere?

    Thread Starter wscheer

    (@wscheer)

    Actually, I found a fix.
    Instead of using “cat_name” you can use “category_nicename”. The nice name is the category slug. IE: Condos & Beach Houses -> condos-beach-houses
    There seems to be no issue with length using the slug.

    I have highlighted the change below with =================

    <?php //get the current category and put into $category var
    $category = get_the_category();
    echo "<h2 class='headline'>".$category[0]->cat_name."</h2>";
    //
    //
    // ==========================================
    $cat = $category[0]->category_nicename;
    // ==========================================
    //
    //
    ?>
    
    <?php $recent = new WP_Query(); ?>
    
    // for testing what the 'get_the_category()' output was
    <?php echo "cat: ".$cat; ?>
    
    // this was working for some categories but not others
    <?php $recent->query('category_name='.$cat.'&showposts=-1'); ?>
    
    <?php while($recent->have_posts()) : $recent->the_post(); ?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
    <?php endwhile; ?>

    Hope that helps someone

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘category names that are to long don’t work in wp_query()’ is closed to new replies.