Forums

Display Subcategories Before Post Title (like "Category Subcategory Post Name") (4 posts)

  1. Walker
    Member
    Posted 2 years ago #

    I have a post that is nested in a subcategory like:
    Cars->BMW->Latest BMW Models

    I want to display the parent categories of a post in their corresponding order as the title of the post.

    E.g: instead of showing just "Latest BMW Models" I want the post title to say "Cars BMW Latest BMW Models". I know this makes no sense to you but I am giving the car blog just as an example, my blog is about completely different thing.

    Also, it should work no matter how deep the post is located, e.g Cars->BMW->Sedan->Diesel->2010 models->Latest BMW Models.

    So is there a WordPress function to just list the Parent categories of a post in the correct order or at lest pass them to an array? I am pretty knowledgeable at PHP so you can skip the basic stuff like "go to this line", "replace this line with this". Just point me a function that may help.

    Thanks in advance for the help and regards!

  2. Web Hosting India
    Member
    Posted 2 years ago #

  3. vtxyzzy
    Member
    Posted 2 years ago #

    Assuming that you only have one category assigned to a post, this may do what you want if in the loop:

    <?php
    $cats=get_the_category();
    echo get_category_parents($cats[0]->cat_ID,TRUE,'->');
    ?>

    Outside the loop, use this:

    <?php
    global $post;
    $cats=get_the_category($post->ID);
    echo get_category_parents($cats[0]->cat_ID,TRUE,'->');
    ?>
  4. aldax
    Member
    Posted 1 year ago #

    Thanks for the link to the breadcrumbs plugin. It is exactly what I needed for my wordpress ecommerce site.

    Joseph

Topic Closed

This topic has been closed to new replies.

About this Topic