I'm trying to get the post title of a post_type (post/page/portfolio) in the admin panel outside of the loop.
I nearly tried all possible solutions, but without success:
//method 1
$content_post = get_post(3208);
$_menu_item_title = $content_post->post_title; //(Trying to get property of non-object error)
//method 2
$obj = get_post_type_object($_menu_item_type);
$content_post = get_post(3208); //(Trying to get property of non-object error)
$_menu_item_title = $obj->$content_post->post_title;
//method 3
$_menu_item_title = the_title(3208); //(no error but nothing is store)
//method 4
$_menu_item_title = get_the_title(3208); //(no error but nothing is store)
I'm trying to get the post title of a post_type (post/page/portfolio) in the admin panel outside of the loop.
I nearly tried all possible solutions, but without success:
//method 1
$content_post = get_post(3208);
$_menu_item_title = $content_post->post_title; //(Trying to get property of non-object error)
//method 2
$obj = get_post_type_object($_menu_item_type);
$content_post = get_post(3208); //(Trying to get property of non-object error)
$_menu_item_title = $obj->$content_post->post_title;
//method 3
$_menu_item_title = the_title(3208); //(no error but nothing is store)
//method 4
$_menu_item_title = get_the_title(3208); //(no error but nothing is store)
Share
Improve this question
edited Jul 18, 2014 at 9:15
Pieter Goosen
55.4k23 gold badges116 silver badges210 bronze badges
asked Jul 17, 2014 at 20:13
lolololo
1112 silver badges10 bronze badges
3
|
4 Answers
Reset to default 4If you have only ID of a post and you want only the title of that post, then using get_post_field
will be best way to do this, I guess.
The syntax of this function:
get_post_field( $field, $post_id, $context );
So code, that will solve your problem looks like that:
$title = get_post_field( 'post_title', $POST_ID );
// most probably you want to display the title, so you can ignore last param
And addressing your code snippets... First method should work, if the post exists and it's published or current user can see it.
Second method can't work. It makes no sense.
Third and fourth methods can't work also. These functions don't take post_ID as param, so such use of them doesn't make much sense...
As already been pointed out, the "non-object error" means get_post() returned nothing, and that the post does not exist.
If you are certain, though, that this post does exist, here is an alternative method taken from the codex
<?php
$post_7 = get_post(7, ARRAY_A);
$title = $post_7['post_title'];
?>
If, again, nothing is returned, are you certain this post has been Published, and is not in a Draft status?
I did get the relevant title of the post using following process. first: I get information of post Id from dashboard [hover over the post and watch at the left bottom of the dashboard (you will get id information there)]
Second: inside wordpress loop I used:
<?php if(have_posts()): while(have_posts()) : $var_name= get_the_ID(); ?>
<?php if($var_name == post-id-value): get_the_title(); ?>
<?php endwhile; endif; ?>
here post-id-value is the integer value which is your post's id. You can store the get_the_title() value in a variable and echo function to echo out the value in your desired location. Note* you need to have at least one post in your wordpress dashboard. Otherwise you may get unexpected results ( browser even computer may crash)
using this default WP methods you can get the title of current page and current post.
<?php
echo get_the_title();
3208
then method 1, 4 should work. – obiPlabon Commented Feb 13, 2018 at 9:31