-1

So far I've got the code below which achieves the effect I'm after, but it appends the title of only the current page to every page title on a page (including Next / Previous links which obviously have different titles), so I obviously need to retrieve the custom field value from these posts, but I'm struggling to achieve this.

    add_filter( 'the_title', 'post_title_append_alt_lang' );
       function post_title_append_alt_lang( $title ) {

         if( get_field( 'post_title_text_alt_language' ) && in_the_loop() ){ 
              return $title.' <span class="chinese_trad" lang="' 
              . get_field(post_title_language) . '">' 
              . get_field(post_title_text_alt_language)
              . '</span>';
           }
           return $title;
       }

I know I need to tie the custom field retrieved to the post ID somehow, but I'm not sure of the best way to do this that will work for any posts that might appear on the page.

Any help would be greatly appreciated thanks!

0

If you need to append something to EVERY post_title you have, you should:

Make your loop with WP Query, check Codex

Then, use get_the_ID, to get the specific post ID, check Codex

Then, use the ID to get the ACF field from that specific post like get_field('name-of-the-field), check the documentation

Don't forget to use wp_reset_query(), check the Codex

  • Thanks so much! I did it slightly differently in the end, but it seems to work ok so far. – andersonslookout Nov 28 '17 at 16:07
  • You did a function that kinda does what I said and you kinda 'froze' that function using the '10' and '2'. It's not the best approach, but if its working for you, that's what matters =) – Marcelo Henriques Cortez Nov 28 '17 at 16:12
-1

OK, I found a solution in the end. Here's the function I went with:

function add_custom_field_after_title($title, $id) {

  if ( in_the_loop() ) {

      $custom_field = get_field( 'customfield', $id );

      if ( !empty($custom_field) ){
        $title .= $custom_field;
      }
   }

  return $title;
  }

add_filter('the_title','add_custom_field_after_title',10,2);

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.