0

So I have the following code:

    <?php
    query_posts("order=ASC&cat=4");
?>              
<?php if(have_posts()): while(have_posts()): the_post(); ?>
    <?php if(get_post_custom_values("show") != NULL): ?>
    <?php
        $categories = get_cat_ID(get_the_title());
        $url = get_category_link($categories);
    ?>
    <li class="thumb">
        <a href=""><?php the_post_thumbnail(array(215,200)); ?></a>
        <h2><a href=""><?php the_title(); ?></a></h2>
    </li>
    <?php endif; ?>
<?php endwhile; endif; ?>   
</ul>

This code works, but when you have & - in your title $categories = get_cat_ID(get_the_title()); .. get_cat_ID won't work, do you know a work around?

2
  • One thing I can see right off the bat is that you don't need the if(have_posts()) there... Commented Feb 9, 2011 at 20:25
  • Nop, doesn't do anything, I put it just for extra saftey :D Commented Feb 9, 2011 at 21:08

1 Answer 1

1

Try get_category_by_slug instead. Fetching the ID by the name of the category will get messy, especially with duplicates and characters, as you've discovered.

Sign up to request clarification or add additional context in comments.

6 Comments

Doesn't it doesn't return the id for the first item, $categories = get_category_by_slug(get_the_title());
Do you know another method? :-s
What are you trying to do exactly? Get the ID of a specific category? The ID of the post's category (or categories)?
I'm trying to list a bunch of posts(products) from a category, and each post has a category, let's say if I have a post with the name of "Darling" that post is posted in a category called "darling" and I'm trying to list those posts, but link them with the link of the catory which is posted in, I hope you get what I said :( 'cause I need to get this working, It works but doesn't work for the posts with - & in it you can see an example here: is.gd/6hBgbd , It works for the Section Vitrinen but the links won't work for Braut if you click on the images you will see
For Vitrinen I duplicated the same code, and It works, because of the titles
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.