Ronny K
Ronny K

Reputation: 3751

how to display custom post type post's title on page

I am trying something out on wp. I want to display custom post types title on page post type. here is my code.

function get_post_type_title(){

        if('movie_ronny' == get_post_type()){ // if post type is movie_ronny, get post title

            $movie = get_the_title(); //hold the post title in $movie.

    }

    if(is_page() ){ // if viewing page, display movie_ronny title

    echo $movie;

    }       

}
add_filter('wp_head','get_post_type_title');

Above code does not display the title of movie post-type when viewing page. Any help is highly appeciated.

Upvotes: 0

Views: 6702

Answers (3)

Deco Grafics
Deco Grafics

Reputation: 11

Try this one:

<?php 
    $post_type = get_post_type_object( get_post_type($post) ); 
    echo $post_type->label ; 
?>

Upvotes: 1

Ben Racicot
Ben Racicot

Reputation: 5980

Your post type will most likely be shown on its own single-movie_ronny template. In which case of course you wouldn't need a conditional -

if('movie_ronny' == get_post_type()){...}

The other reason why this question is a bit strange is that your loop query must define within it's arguments what type of post, page or custom post type it is querying. So you would also then know which post type you are messing with and not need the first conditional.

And no matter what your situation is:

if(is_page() ){ // will only return true for pages. not posts or CPT's

Also,

get_the_title($id); // needs a post ID outside of the loop.

Upvotes: 2

Sverri M. Olsen
Sverri M. Olsen

Reputation: 13283

Well, there are two ways this function will fail (i.e. do nothing):

  1. If the first if block is ignored then the second if block will not do anything (I believe it will actually show a notice, because $movie would not be defined).

  2. If is_page() does not evaluate to true then nothing will be done in any case.

If you know anything about programming then this should be obvious. So, either you have no idea what you are doing, or I am misunderstanding something here.

Upvotes: 1

Related Questions