Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter icyville

    (@icyville)

    anyone?

    I’m also looking to do this, does anyone have any ideas?

    At the moment I’m using <?php the_category(' ', 'single' ); ?> but that displays Parent / Child but I only want to show Child.

    any ideas? Sorry to bring this topic back to the top but I really need to figure this out.

    Thanks!

    Heh, I just wrote a function for this last night. But it might bee too much – I’m not clear on what you’re trying to do – plus my code is very hacky (I *know* it can be cleaned up and written leaner…I just wrote the first draft last night.)

    but to answer the initial poster’s question – no, you cannot do it just by using the_category – not that I’ve found anyway.

    What *I* needed it for was a custom navigation setup. So the main navigation had the main parent link, but when you clicked on that link (be it a Page or a category link) the children of that Page/category would show up in the sidebar – without the parent. (I also had to write some custom code to make the main link in the header nav stay a certain color so you’d know where you were – but that’s something else)

    Anyway, that was the *purpose* of me writing the function. it would list *all* of the children – and even grandchildren (although you could probably edit that by changing the depth – I didn’t bother testing that part) of the main nav link in the header IF there were children to be had.

    If that’s what you’re trying to do, let me know, and I’ll post my ugly-code-that-seriously-needs-to-be-cleaned-up (but I’m working on it today).

    The reason I need it is so that people can see what child category a post is from, take a look at this as an example:

    https://www.daleanthony.com/grab/5cc16ceb35bfd98c52dcf4ae91c5d388.png

    I’m using <?php the_category(‘ ‘, ‘single’ ); ?> which displays the text ‘Guide / Restaurants’ which is the parent and the child category the post applies to.

    However, I only want it to show the child category so underneath the name of the post (in this case ‘Flavourz’) the name of the child category would be shown ‘Restaurants’

    Someone just emailed me and reminded me of this thread. Apologies for waiting so long in getting back to it – I usually watch to see if htere are any replies, but this one must have gotten away from me. SO I’ll post *my* solution to this question here – I’ve used it on a couple of clients sites and it works pretty well. I’m sure my code could be made a little better, but it works, anyway.

    Open up your functions.php file and add the following code:

    <?php function toplevel() {
    	global $post;
    	$cat = get_query_var('cat');
    	$parents = get_category_parents($cat);
            $get_parent = explode('/',$parents);
    
        if(is_home()) {
          $toplevel = 'home';
        } else {
          $toplevel = $get_parent[0];
        }
        return strtolower($toplevel);
    } ?>

    the above code will give you the names of the topmost level of Pages (no matter how many children they have), as well as the topmost categories (no matter what subcategory you are in). If you’re on the home page, it’ll just return “home” – otherwise, it’ll return the slug of the parent Page or category you are in. You can use the value that’s put out as an ID or class value. For example, in your header.php file, change <body> to <body id="<?php echo toplevel(); ?>"> and now our <body> tag will have the name of top topmost parent of whatever area you’re in. You can then use that to style things as needed.

    The case I used this in needed to also have Pages to insert a “description” of sorts before displaying the archive of posts for that section. So I needed the Page name to match the category name so it would display the proper “Description”. So I named a Page the same thing as a category I wanted associated with it. Then I did the following (also in the functions.php file):

    <?php function cat_description() {
            global $post;
            $top = toplevel(); // pulls in the function above and retrieves the current value
            $catid = get_query_var('cat'); // query the current category
            $cat = &get_category($catid); // get the current category ID
            $catname = $cat->slug; // retrieve the current category slug
            $cat = $cat->cat_name; // retrieve the current category nice name
    
    // now let's compare
    $pagematch = new WP_Query("pagename=$catname"); // if the Page slug is the same as the category slug...
            if($pagematch->have_posts()) : while($pagematch->have_posts()) : $pagematch->the_post(); ?>
            <div id="category" class="<?php echo $top; ?> ">
              <h1><?php echo $cat; ?></h1>
              <?php the_content(); ?>
            </div>
      <?php endwhile; endif;
    } ?>

    Now, on the “category.php” page, you just insert <?php cat_description(); ?> before your regular Loop starts, and it’ll pull in the matching Page content above your category listing.

    Add this to your functions.php if you want the children of the parent category *only* to show. This also applies a class to the list items in the default wp_list_categories so you can indicate styles for hover status and such.

    <?php function sidenav() {
    	$top = toplevel(); // get toplevel name
            $pc_id =  get_cat_id($top); // parent category ID
    
               $categories = wp_list_categories('title_li=&child_of='.$pc_id.'&hide-empty=0&echo=0');
    	   $category_array = preg_split('/\n/', $categories);
    	   $count = count($category_array);
    	   $i = 0;
    	   while ( $i < $count ) {
            if ( preg_match('/<ul class=(\'|")children(\'|")/i', $category_array[$i+1]) ) {
              echo preg_replace('/<li class=(\'|")(.+)(\'|")>/i', '<li class=$1parent $2$3>', $category_array[$i]) . "\n";
            } else {
              echo $category_array[$i] . "\n";
            }
            $i++;
    	   }
    } ?>

    And again, use <?php sidenav(); ?> where you want this to show. For Pages, you can just use <?php &get_page_children( $page_id, $pages ) ?> as referenced here in the codex.

    Hope that helps ??

    Hi, All

    This retrieves childs only within Loop. ( works fine on WP 2.6.3 comment.lv )

    <?php $parentscategory ="";
    foreach((get_the_category()) as $category) {
    if ($category->category_parent != 0) {
    $parentscategory .= ' cat_ID) . '" title="' . $category->name . '" class="part">' . $category->name . ', ';
    }
    }?>
    <?php echo substr($parentscategory,0,-2); ?>
    //* <<— this is instead of <?php the_category(‘,’); ?>
    and class=”part” must be defined within your CSS to apply styling to your category ??

    Regards
    Eugene

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Only display child category using <?php the_category(‘ ‘); ?>’ is closed to new replies.