• I’m having trouble retrieving category ID’s using the get_cat_ID() function. I have a number of categories such as “Monday Specials”, “Tuesday Specials” etc (one for each weekday) with content in each category. When I pass in a literal string to the get_cat_ID() function (e.g. Tuesday Specials) I get the category ID returned properly but when I pass in a variable I get 0 returned which I understand to mean that the function failed. When I print the variable it displays fine so I don’t understand why this function is not working. (In case it matters I’m using the Shortcode PHP Exec plugin to execute the code via a shortcode in a page and the Easy Timer plugin to retrieve the current weekday and date (in the local users’s timezone).

    Here is a code snippet:

    [please use the ‘code’ button to mark your code]

    ~ ~ ~

    extract (shortcode_atts(array('region' => 'All Neighborhoods'), $atts));
    $curr_weekday = '[weekday]';   // get the weekday from the other plugin since it's working properly
    echo 'Today is ' . $curr_weekday . '.';  //this works properly
    
    $weekdayspecials = $curr_weekday . ' Specials';  // e.g. Tuesday Specials
    echo 'weekdayspecials=' . $weekdayspecials . '';  // displays the correct string
    
    $category_id = get_cat_ID( $weekdayspecials );      //This returns 0 (doesn't work)
    $category_id = get_cat_ID( 'Tuesday Specials' );  //This works (returns proper category id)

    ~ ~ ~

    I cannot figure out why it is only working properly when I am passing in the literal string (which I don’t want to do). This is driving me crazy. Please help! Thanks! ?? (I am a programmer but new to PHP so I’m sure there are things I’m just not getting yet…)

    Corinne

Viewing 2 replies - 1 through 2 (of 2 total)
  • extra spaces within the $curr_weekday string can break the result;

    try:

    $weekdayspecials = trim($curr_weekday) . ' Specials';  // e.g. Tuesday Specials

    https://php.net/manual/en/function.trim.php

    Thread Starter cbattle

    (@cbattle)

    Thanks for the suggestion but I tried it and it didn’t change anything. I have a feeling it’s due to the way that I’m retrieving the weekday, via a shortcode (Easy Timer plugin). I can’t imagine why this is a problem but I have a feeling it has some thing to do with that. I may need to pull in the code that does the weekday calculation rather than using the shortcode. Again, I have no idea why it would be this way but when I set the $weekdayspecials variable manually and then call get_cat_ID it works fine, like this:

    $weekdayspecials = ‘Wednesday Specials’;
    $category_id = get_cat_ID( $weekdayspecials ); // this works

    It’s just when I use the shortcode that it doesnt’ work, eventhough when I print out the $weekdayspecials string it looks fine.

    $weedayspecials = ‘[weekday]’;
    $category_id = get_cat_ID( $weekdayspecials ); // this doesn’t work

    I’m totally stumped.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Having trouble retrieving category ID with get_cat_ID()’ is closed to new replies.