2

I am using following codes to show title of a post in another post.But it shows only post id.How to solve this?

<?php $home_team_name = rwmb_meta( 'pb_select_home_team', 'type=select_advanced', get_the_ID() ); ?>
      <?php  echo esc_html( $home_team_name  ); ?>

Thanks

1
  • 1
    This is clearly Wordpress related. How do 3 mods close this for being off topic? May 1, 2019 at 6:04

2 Answers 2

9

Because you're outside the loop, you'll need to either know the post id of the title you want and specify it in the function parameter, or call the global $post variable if you're on the page (just not in the loop yet).

global $post

echo get_the_title($post->ID);

or

echo get_the_title(2);
2
  • When I am using echo get_the_title('3592'); then it shows the title.But my problem is not solved yet.Let me explain my problem once again.I have a custom post name 'football_team'.I have shown all post related to that CP as metabox using 'select_advanced' type in another CPT football_fixture. Now I want to show the content I select from the metabox.Thanks
    – bdtheme
    Sep 9, 2016 at 8:30
  • If it's returning the post id, then it should be as simple as, echo get_the_title($home_team_name) Sep 9, 2016 at 19:42
0

Generally we use the_title() or echo get_the_title() to print title

try

echo get_the_title();

or

echo get_the_title( get_the_ID() );
5
  • get_the_ID() still only works within the loop. developer.wordpress.org/reference/functions/get_the_id Sep 9, 2016 at 7:29
  • @JeremyRoss you are absolutely correct, but when we access a post/page it is a loop itself. so when we call get_the_ID() inside of page.php or single.php get_the_ID() gives the id of current post/page Sep 9, 2016 at 8:03
  • 1
    as @Arif Hossin can access get_the_ID() he can also access get_the_title( get_the_ID() ); No need to to define global $post Sep 9, 2016 at 8:09
  • The title of the question was how to get access from outside the loop, but I see now that in the description, that code is actually running in the loop. Sep 9, 2016 at 19:41
  • It sure does is. Sep 10, 2016 at 21:11

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