WordPress.org

Ready to get started?Download WordPress

Forums

category issue (for coding) child-parent (2 posts)

  1. selflearner
    Member
    Posted 6 years ago #

    Hi,
    I'm really stucked, as I am not very good at php..
    my problem is, I can not reach a parent category.
    here is the situtation,

    MainCategory
    -subCategory

    I write a post, and assign it to the -subCategory. well now, when I'm viewing the post, I can not echo the category MainCategory.

    I have searched all over the net, and read all the articles about breadcrumb in this site, but non of them works for me, I simply need to reach the category one up.

    when I view the post,

    foreach((get_the_category()) as $cat) {
    	$parent_category = $cat-> category_parent;
    }

    so it gives me the parent category id, for instance, its 6
    it means that this is the subcategory of the category which ID is 6, how can I echo that category name ??

    I really dont get it, I searched and tried all the codes about category, but I can not find to way to reach the main category.

    one thing that I realise, if I write,
    <?php the_category('','multiple'); ?>
    shows, mainCategory/subCategory but I can not seperate them, it doesnt put space between words and also I want to put > between them.

    I'm going mad, trying to solve this problem for 5 days.

    please don't say breadcrump plugins, cause none of them works for me.
    thanks in advance.

  2. Chris Lee
    Member
    Posted 6 years ago #

    try...
    <?php
    $string = the_category('','multiple');
    $string = split('/', $string);
    echo $string[1]; // should echo subCategory
    echo $string[0]; // should echo mainCategory

    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic