2

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
  • Do you need the - also in between? And will there be just 2 words? If there are 3 or more words, how would you like to display? – Akshat Nov 22 '18 at 22:03
2

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.
3
  • Thanks for response! Actually there is no dash between. $separator =' '; solved the problem! Great! – Vedran Sadić Nov 22 '18 at 23:02
  • That's why I made that separator easy to customize ;) – Krzysiek Dróżdż Nov 22 '18 at 23:03
  • @VedranSadić remember to mark answer as correct to keep the site clean and tidy. – Krzysiek Dróżdż Nov 22 '18 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.