WordPress.org

Ready to get started?Download WordPress

Forums

get_cat_ID and a variable (8 posts)

  1. mtwelve
    Member
    Posted 3 years ago #

    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:

    <?php
    $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?

  2. mtwelve
    Member
    Posted 3 years ago #

    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)

  3. DigitalSquid
    Member
    Posted 3 years ago #

    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.

  4. mtwelve
    Member
    Posted 3 years ago #

    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

  5. DigitalSquid
    Member
    Posted 3 years ago #

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

  6. mtwelve
    Member
    Posted 3 years ago #

    Hi,

    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' '))';
    
    query_posts($jnews);

    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!

  7. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

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

    try:

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

    btw:
    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.

  8. mtwelve
    Member
    Posted 3 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic