• Resolved Steelclover

    (@steelclover)


    <div class ="category-<?php echo the_category_ID( $echo ); ?>" id="page-<?php echo get_query_var('name'); ?>">

    this i run outside the loop but it gives no value.

    it shows:
    <div class ="category-" id="page-">

    Why is that?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hi, @steelclover!

    The official WordPress API documentation states that the_category_ID is deprecated. You should use the get_the_category/ function, with an optional parameter $post_id.

    Thread Starter Steelclover

    (@steelclover)

    Thank you.

    That gives me Array as result.

    True. Does your post have several categories, or only one?
    It you only have one category, you can use get_the_category( $post_id <optional>)[0]->name to get the category name:
    class ="category-<?php echo get_the_category($post_id)[0]->name; ?>"
    replace $post_id with the correct id, or remove it.

    As for the get_query_var('name');: are you sure there is a ‘name’ variable in your query?

    Thread Starter Steelclover

    (@steelclover)

    Thank you again : )

    the post-part wasnt broken. i tried all but its still not working.

    Here is what ive got:

    <div class ="category-<?php echo the_category_ID( $echo ); ?>">
    do something
    </div>

    the above prints over years:

    <div class ="category-biersorten">
    do something
    </div>

    now it shows:

    <div class ="category-">
    do something
    </div>

    ive tried:

    <div class ="category-<?php echo get_the_category()[0]->name; ?>"
    do something
    </div>

    and it shows:

    <div class ="category-">
    do something
    </div>

    It works until i did the update. I have another sites running that old code and it still works. But all updated Sites are now broken.

    May you can have a look at it.
    forget about the id=”post” – this still works.

    • This reply was modified 3 years, 8 months ago by Steelclover.

    I didn’t give you the full information, sorry.
    You can omit post_id only if you are using get_the_category inside the loop. If you are using it outside the loop, you need to pass the post_id parameter.

    So, the code would look something like this:

    <div class ="category-<?php echo get_the_category($post->ID)[0]->name ?>">
    <?php echo get_the_category($post->ID)[0]->name ?>
    </div>
    
    • This reply was modified 3 years, 8 months ago by Olga Bulat. Reason: Added the sample code
    Thread Starter Steelclover

    (@steelclover)

    oops i think something went wrong ??

    Thread Starter Steelclover

    (@steelclover)

    As i can see by now it depends on if its a clean installed wordpress site or just updated.

    however
    the_category_ID( $echo ); still works on new clean version
    the_category_ID( $echo ); still works on older version
    the_category_ID( $echo ); stops workin on updated version
    get_the_category($post->ID)[0]->name works on new clean version
    get_the_category($post->ID)[0]->name does not work on updated version

    i think there must be some issues within the progress of updating.

    but thank you. now i know how to get it work.

    • This reply was modified 3 years, 8 months ago by Steelclover. Reason: spelling

    Interesting! I’m glad you’ve figured it out.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘easy modifier code doesnt work’ is closed to new replies.