0

So I've set up my site to use static pages for the front page and the posts page. Now I'm creating my posts page template. I wanted to grab some content from the actual page I made for the posts page (even though it's no longer really considered a page) and I figured out how to do it...sort of.

$posts_page = get_option( 'page_for_posts' );
$title   = get_post( $posts_page )->post_title;
echo $title;

So this will display the title of the "page" in the header area instead of the title of the post. What I need and can't figure out now is how to grab the featured image URL, with a custom thumbnail size with this method.

Any ideas?

1

In your example, $posts_page contains the ID of the page for posts, you can use that with any API function that accepts a post ID.

For the featured image:

$posts_page = get_option( 'page_for_posts' );
$post_thumbnail_id = get_post_thumbnail_id( $posts_page );
$url = wp_get_attachment_image_src( $post_thumbnail_id, 'your-custom-size' );
echo $url;

And outputting post title with filters applied:

$posts_page = get_option( 'page_for_posts' );
$title = get_post_field( 'post_title', $posts_page );
if ( ! is_wp_error( $title ) ) {
    echo $title;
}
  • This worked great thank you. I just had to edit it slightly as $url was returning an array, so i changed it to 'echo $url[0]' and it works perfect. – NickD Jan 21 '16 at 13:20
1

You can make use of the queried object. On the blog page, the queried object will hold the page object of the page set as blog page.

The following will work (This is if you just need to show this on the blog page)

/**
 * Use $post as local variable in order use setup_postdata 
 * in order to make the use of template tags available
 */
if ( is_post_page() ) { // Only target the blog page
    $post = get_queried_object();
    setup_postdata( $post ); 
        // Display the post thumbnail
        the_title();

        // Display the post thumbnail
        if ( has_post_thumbnail() ) {
            // featured image URL
            $size = 'my_custom_size';
            $thumbnail_id =  get_post_meta( get_the_ID(), '_thumbnail_id', true );
            $image_src = wp_get_attachment_image_src( $thumbnail_id, $size );
            //var_dump( $image_src ); // For debugging purposes
            $url = $image_scr[0]; 

            Display post thumbnail
            the_post_thumbnail( $size );
        }
    wp_reset_postdata(); // Restore the $post global
}

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.