• wutzelmutz

    (@wutzelmutz)


    Hi everybody,

    on our book review blog, every category is the name of a a books author, e. g. “The old man and the sea” is filed under “Ernest Hemmingway”.

    Is there any way to alter the output on the archives pages since I would like the categories to be sorted by the surname (e. g. “Hemmingway, Ernest”). Since the author shall be shown correctly with first and surname on the single pages, I somehow need to versions of every category.

    This is the code I’m using for the category index:

    <?php
    	$taxonomy = 'category';
    	$param_type = 'category__in';
    	$term_args=array(
    	'orderby' => 'name',
    	'order' => 'ASC'
    	);
    	$terms = get_terms($taxonomy,$term_args);
    	if ($terms) {
    	foreach( $terms as $term ) {
    	$args=array(
    	"$param_type" => array($term->term_id),
    	'post_type' => 'post',
    	'post_status' => 'publish',
    	'posts_per_page' => -1,
    	'caller_get_posts'=> 1
    	);
    	$my_query = null;
    	$my_query = new WP_Query($args);
    	if( $my_query->have_posts() ) {  ?>
    	<h2 style="margin-bottom: 0px;"><?php echo $term->name;?></h2>
    	<?php
    	while ($my_query->have_posts()) : $my_query->the_post(); ?>
    	<a>" rel="bookmark"><?php the_title(); ?></a>
    	<?php
    	endwhile;
    	?>
    	<?php
    	}
    	}
    	}
    	wp_reset_query();  // Restore global post data stomped by the_post().
    	?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • One possible solution could be to rename category slugs and then order terms by slug.
    The only difference you’ll notice will be the url, so instead of http://www.example.com/category/ernest-hemmingway you’ll have http://www.example.com/category/hemmingway-ernest

    $taxonomy = 'category';
    	$param_type = 'category__in';
    	$term_args=array(
    	'orderby' => 'slug',
    	'order' => 'ASC'
    	);

    More about get_terms function here

    Thread Starter wutzelmutz

    (@wutzelmutz)

    Thanks a lot, that worked out great. I always wondered what those slugs are used for 🙂

    Thread Starter wutzelmutz

    (@wutzelmutz)

    Okay, the solution with the slugs was fine, but I’m still struggling with the displayed order of the categories.

    Is it possible to change the displayed names from “Henry Miller” to “Miller, Henry” in any way? I could rename the categories, but as I need both “Henry Miller” (on the posts itself) and “Miller, Henry” (on the archives ordered alphabetically), that’s not the best idea.

    Any tipps on that?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Display order of category names’ is closed to new replies.