WordPress.org

Ready to get started?Download WordPress

Forums

display featured image on index.php outside the loop (3 posts)

  1. chiara77
    Member
    Posted 3 years ago #

    Hey there,

    hope somebody can help me on this one.

    I am using this code to show content of a certain page in the home page:

    <?php
    $page_id = 293;
    $page_data = get_page( $page_id );
    $content = $page_data->post_content;
    $title = $page_data->post_title;
    echo $page_data->post_title;
    echo $page_data->post_excerpt;
    ?>

    I want to show the title of the page, the excerpt and the featured image. Like this I get easily the title and the excerpt, but I am looking for a way to show the featured image too.

    Thank you,

    Chiara

  2. Mark / t31os
    Moderator
    Posted 3 years ago #

    Where is this featured image stored? More clarification on how you're setting this featured image would help..

    That said, you'd be better off calling get_posts() so you can form a regular loop which will give you access to pre-sanitized template tags, which would be preferable to the method you have above, which accesses raw(unsanitized data)..

    // Example
    $my_page = get_posts("p=$page_id");
    if( $my_page )
    foreach( $my_page as $my_post ) : setup_postdata( $my_post );
    
      the_title();
      the_excerpt();
      // the_post_thumbnail(); // You can use this if you're using the built-in feature image facility
    
    endforeach;
    wp_reset_query();

    Related docs:
    http://codex.wordpress.org/Template_Tags/get_posts
    http://codex.wordpress.org/Template_Tags/
    http://codex.wordpress.org/Function_Reference/the_title
    http://codex.wordpress.org/Function_Reference/the_excerpt
    http://codex.wordpress.org/Function_Reference/the_post_thumbnail

  3. pete@aodny.com
    Member
    Posted 2 years ago #

    FAN_FRIGGAN_TASTIC!

Topic Closed

This topic has been closed to new replies.

About this Topic