WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] category names that are to long don't work in wp_query() (3 posts)

  1. wscheer
    Member
    Posted 4 years ago #

    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?

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    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?

  3. wscheer
    Member
    Posted 4 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic