• Ok, so this is a question I don’t know is a possible to answer. I’ve tried googling the solution and not having much luck.

    What I do now
    For each location on our website, we make a category with the name of the town, in our example below, it’s in “Hot Srings.”

    Right now we have set up a hierarchy of categories as follows City>Western Arkansas> Hot Springs. So when ever we check the category, this is what appears: City/Town: ? Hot Springs
    Because of the code

    <?php if ( in_category( 'Hot Springs' )) { ?>&bull; <a href="https://abandonedar.com/city/western-arkansas/hot-springs/">Hot Springs</a>
    <?php } ?>

    The goal
    We have to go into the back end to add every new city and it gets a little annoying. Is there a way with php to say:

    <?php if ( in_child_category( 'Western Arkansas' )) { ?>&bull; <a href="https://abandonedar.com/city/western-arkansas/"><?php echo get_cat_name(); ?></a>
    <?php } ?>

    And it would print the category name?
    The idea is that this code would streamline the process where we just add the category on the post and it adds prints the name to the frontend, no coding.

    Is this possible or am I reaching for the stars here?

    Thanks in advance!

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Sure it’s possible, nothing astronomical about it.

    I assume this is for single post pages? You can get post categories with wp_get_post_categories(). If there are categories outside the “City” hierarchy it might be tricky getting the actual town category out of the list, but if the assigned cats are all under “City” I believe the deepest descendant (the actual town) is always listed first. You’ll need to verify this. Either way, the data you need is there. It’s a matter of how hard it is to get to it.

    Assuming you can get the right term because it’s always first in the list, the category term name would be the first objects name property. The link to that term’s category archive can be had with get_category_link(), given the correct term object or ID from the list of cat terms assigned to the post.

Viewing 1 replies (of 1 total)
  • The topic ‘Printing Child Category Names’ is closed to new replies.