• Resolved ezs

    (@ezs)


    I’ve already searched through the Forum and the Codex so many time, but nothing I’ve found works.

    I have a categories structure like this:

    Category A
    -Sub Category A1
    -Sub Category A2

    Category B
    -Sub Category B1
    -Sub Category B2

    I only want to show inside each post this structure:

    Entry title
    Topic: Topic 1
    Location: Location 1

    I tried the_category but this call show a list without parent relation (Categories: Topic 1, Location 1). Also I tried get_category_parents but this add the separator at the end, as we can see in the codex example:

    Internet ? Blogging ? WordPress ?

    Finally I ruled out use this:

    <?php if ( in_category(‘4’) ): ?>
    Category A: Subcategory A1
    <?php endif; ?>

    <?php if ( in_category(’11’) ): ?>
    Category B: Subcategory B2
    <?php endif; ?>

    Because this is a ugly solution for a 60 categories list. And of course, the changes on the category structure are not automaticly reflected and would be necessary edit the theme.

    Any suggestions?

    Thanks in advance.

Viewing 5 replies - 1 through 5 (of 5 total)
  • It’s not clear if you know about:

    <?php the_parent('', 'single'); ?>
    <?php the_parent('', 'multiple'); ?>

    Reference: https://codex.www.ads-software.com/Template_Tags/the_category

    Use of ‘single’ displays a single category link to the ‘subcategory’ like so:

    Category B/Subcategory B1

    Whereas with ‘multiple’, separate links to both parent and child categories occur:

    Category B/Subcategory B1

    Thread Starter ezs

    (@ezs)

    Thanks Kafkaesqui,

    I don’t like the slash separator between Category B/Subcategory B1 but I was able to hack it editing wp-include/category-template.php at line 47:

    function get_category_parents($id, $link = FALSE, $separator = ‘: ‘, $nicename = FALSE){

    That return:

    Category B: Subcategory B1

    Would be great be able to do that by other way, for eg. a function hold at theme directory. I am not sure if it is possible. This would be better in order to make clean WP updates.

    Thanks!

    I don’t like the slash separator between Category B/Subcategory B1

    You can modify the separator from within your theme by utilizing the underlying function of the_category():

    <?php echo str_replace('</a>/', '</a>: ', get_the_category_list('', 'multiple')); ?>

    Thread Starter ezs

    (@ezs)

    Wow! Nice function! I ignored it. Thanks!

    Hello. I’m using this in my single.php
    <?php the_category(‘ / ‘, ‘single’) ?>
    and it outputs this link structure:

    CategoryA CategoryA/SubcategoryA CategoryB CategoryB/SubcategoryB.

    Isn’t it suppose to output only

    CategoryA/SubcategoryA CategoryB/SubcategoryB?

    I’m using version 2.6.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to show “Parent Category: Subcategory” structure?’ is closed to new replies.