Support » Themes and Templates » get_cat_ID and a variable

  • Hello,

    Please help me see what I’m doing wrong.

    I was getting a list of posts to display where the category is the same as the title of the page they are being displayed on. However I now need to display posts from this category but only when they are in another category.

    This means getting the ID of the category that shares the name with the page. So I’ve been doing this:

    $cat_title = wp_title('', false);
    $category_id = get_cat_ID($cat_title);
    		echo $cat_title;
    		echo $category_id; ?>

    Now the echo $cat_title variable prints out the title of the page(also the category name/slug) perfectly. But the echo $category_ID always prints 0; a fail.

    I have played with every orientation of the syntax but nothing seems to work.

    I have seen other example passing a variable to get_cat_ID that people said worked fine….. What am I missing?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Once this is sorted I am going to attach the output to another variable to pass to query_posts.

    Can anyone tell me what is/isn’t wrong with this:

    $jnews = "array('category__and' => array(" . '$category_id' . ')))';

    in preperation for query_posts($jnews)



    I think wp_title() has a habit of adding a space between the title and the separator to make it more readable. So even though you’re not using a separator it’s still dumping the space in there which means get_cat_ID() is reading the variable as " Title" instead of just "Title".

    Try changing the first line of your code to:
    $cat_title = ltrim(wp_title('', false));

    Which should get rid of any annoying whitespace and make it all work properly.

    now here is another wierd thing.

    i get it to echo $jnews and it looks exactly like query_posts would be expecting.

    array(‘category__in’ => array(10,19)) 10,19 are two category IDs I’m using to test the theory.

    However the query_posts($jnews) does nothing.

    Even weirder if I put the output of $jnews and paste it into query_posts it spews out what it should do when i just pass $jnews to it.

    HELP!??? please



    Post the code you’re using to make the $jnews variable.


    Thanks for your help; I was starting to loose hope.

    The actualy code is at work and I’ve come home, however it looked a bit like this,

    $jnews = array('category__in' => array( . $category_id . ',19' '))';

    Like I say if I echo $jnews then the output looks perfect. It didn’t work though when I set the IDs statically so I presume its a syntax issue.

    Many Thanks!



    Forum Moderator

    in your code, the array has just one element, a string ‘10,19’ – and not two integer numbers, as the query would need.


    $jnews = array('category__in' => array($category_id, 19));

    instead of echo $jnews try and use var_dump($jnews) which will give you more info; and would have shown that your $jnews was not an array with two elements.

    Many Thanks alchymyth. I’m sure that tip will prove very helpful.

    If your feeling brave feel free to tackle my other problem: here

    Again, many thanks

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘get_cat_ID and a variable’ is closed to new replies.