I created alphabetical list of members(custom post) using first letter of title. Therefore title was in format ‘Surname – Name’. Now on single member page I need somehow to reverse order to ‘Name – Surname’. Is it possible?

<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>

1 Answer
1

There will be a lot of ways to solve this. One of them is to modify template of single CPT post and replace:

<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>

with:

<h1 class="entry-title"><?php
    $separator=" - ";
    echo implode( $separator, array_reverse( explode( $separator, get_the_title() ) ) );
?></h1>

What it will do is:

  1. Take the title.
  2. Explode it into pieces using ‘ – ‘ as separator.
  3. Reverse the pieces.
  4. Glue them together using ‘ – ‘ as separator again.
  5. Echo the result.

Leave a Reply

Your email address will not be published. Required fields are marked *