Support » Fixing WordPress » Assigning Category Hierarchy to Posts by ID not alphabetically

Viewing 15 replies - 1 through 15 (of 20 total)
  • could it be altered using: wp_set_post_categories?

    Surely someone else had experienced this?! It comes up time after time!

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Can we have a link to the page where this is happening?

    Can’t link to page but here’s the code

    This outputs correctly for this category hierarchy:

    UK > Luxury Training > Restaurant (outputs: UK > Luxury > Restaurant)

    But not for:

    UK > Luxury Training > Universities (outputs: UK > Luxury > UK)

    the order that the categories are assigned changes depending on alphabetical order??

    <?php  // Output post parent category name                                  
    
    								  $category = get_the_category();
                                      $parent = get_cat_name($category[0]->category_parent);
                                      if (!empty($parent)) {
                                      echo $parent;
                                      } else {
                                      echo $category[0]->cat_name;
                                      }
                                     ?> News :
                                    <?php 
    
    								  $category = get_the_category();
    								  $firstCategory = $category[0]->cat_name; echo $firstCategory;?> <br />
    								<?php if (has_term('luxury-training', 'category')) {
    								  $secondCategory = $category [1]->cat_name; echo  $secondCategory;
    								} ?>
                               </h3>
    
                             </div>
                           <?php if (has_term('luxury-training', 'category')) {?>
                             <h2> Visit the <a href="/<?php echo $category [1]->slug; ?>">   <?php echo $category [1]->cat_name; ?> </a> training page</h2>                       <?php } ?>
                       	</div>
    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    From the code I can’t really tell what it is you want to do. Show all parent category names for the ‘luxury-training’ category and the ‘luxury-training’ category itself?

    Sorry – it’s not very clear!

    OK – this is in single.php

    I want to output the top level, then second level, then if the current post is in the category ‘Luxury-training’ output the third level

    Then, again, if post is in the ‘Luxury-training’ cat – output also the third level again into link tag that links to a page of same slug as the category (bit in the h2 tag).

    Hope that makes sense!

    Like this:

    UK News : Luxury Training
    Universities
    Visit the Universities training page

    Thanks for taking the time!

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Just to be clear do all posts only have only one top level category? Or can they have multiple top level cats. What should happen if they do?

    they only have one Top level… i.e. UK or Europe etc…

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Try it with this for posts with the ‘luxury-training’ category.

    <?php
    $html = '';
    $terms = array();
    $cat_id = 4; // 'luxury-training' category ID
    $seperator = ' ';
    
    if ( has_term( $cat_id, 'category' ) ) {
    
    	$ancestors = get_ancestors( $cat_id, 'category' );
    
    	// does it have a top level category
    	if ( !empty( $ancestors ) ) {
    
    		$top_level_cat_id = end( $ancestors );
    
    		// top level category and 'luxury-training' category
    		$html .= get_cat_name( $top_level_cat_id ) . ' ' . 'News: ';
    		$html .= $seperator . get_cat_name( $cat_id );
    
    		$child_cats = get_categories( 'child_of='.$cat_id );
    
    		// child categories of 'luxury-training' category
    		if ( $child_cats ) {
    			foreach ( $child_cats as $cat ) {
    				$html .= $seperator . '<a href="' . get_category_link( $cat->term_id ) . '">' . $cat->name .'</a>';
    			}
    		}
    
    	}
    } else {
    	// do stuff for posts that don't have the 'luxury-training' category
    }
    echo $html;
    ?>

    Change the category ID in $cat_id = 4; // 'luxury-training' category ID

    Wow! Thanks very much for your time on this!

    Good guess on the ID – it’s actually 40!

    This works to a point but outputs all sub categories of Luxury Training rather than just the sub categories that the current post is in.

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Ah, try it with this [untested]:

    <?php
    $html = '';
    $terms = array();
    $cat_id = 40; // 'luxury-training' category ID
    $seperator = ' ';
    $post_terms = wp_get_post_categories( $post->ID, array( 'fields' => 'ids' ) );
    
    if ( $post_terms && has_term( $cat_id, 'category' ) ) {
    
    	$ancestors = get_ancestors( $cat_id, 'category' );
    
    	// does it have a top level category
    	if ( !empty( $ancestors ) ) {
    
    		$top_level_cat_id = end( $ancestors );
    
    		// top level category and 'luxury-training' category
    		$html .= get_cat_name( $top_level_cat_id ) . ' ' . 'News: ';
    		$html .= $seperator . get_cat_name( $cat_id );
    
    		$child_cats = get_categories( 'child_of='.$cat_id );
    
    		// child categories of 'luxury-training' category
    		if ( $child_cats ) {
    			foreach ( $child_cats as $cat ) {
    				if ( in_array( $cat->term_id, $post_terms ) )
    					$html .= $seperator . '<a href="' . get_category_link( $cat->term_id ) . '">' . $cat->name .'</a>';
    			}
    		}
    
    	}
    } else {
    	// do stuff for posts that don't have the 'luxury-training' category
    }
    echo $html;
    ?>

    Beautiful – you nailed it… many thanks! Now to work out how you did it…

    Is it the fields => ‘ids’ that dicates the category output order?

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    I’m glad it works.

    The fields => 'ids' argument only gets the term IDs and not the full term objects (smaller query on the database).

    Don’t you need to show terms if a post doesn’t have the term ‘luxury-training’?

    // do stuff for posts that don't have the 'luxury-training' category

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘Assigning Category Hierarchy to Posts by ID not alphabetically’ is closed to new replies.