Grow your CSS skills. Land your dream job.

need help with “How to: Get the first image from the post and display it”

  • # September 18, 2012 at 4:06 pm

    http://www.wprecipes.com/how-to-get-the-first-image-from-the-post-and-display-it
    This is a wordpress problem. I put the code where it said to in my functions and loop files. but all it outputs is the url of the image. here is my site http://www.yardsale.cdcwebdesign.com.
    And no I am not trying to steal the picture a the top of the page i will pay for it when the site goes live. thanks in advance

    # September 18, 2012 at 5:41 pm

    @chanman Did you remove anything? Paste the exact code you put inside your loop.

    Or you can try this:

    function echo_first_image ($postID)
    {
    $args = array(
    ‘numberposts’ => 1,
    ‘order’=> ‘ASC’,
    ‘post_mime_type’ => ‘image’,
    ‘post_parent’ => $postID,
    ‘post_status’ => null,
    ‘post_type’ => ‘attachment’
    );

    $attachments = get_children( $args );

    //print_r($attachments);

    if ($attachments) {
    foreach($attachments as $attachment) {
    $image_attributes = wp_get_attachment_image_src( $attachment->ID, ‘thumbnail’ ) ? wp_get_attachment_image_src( $attachment->ID, ‘thumbnail’ ) : wp_get_attachment_image_src( $attachment->ID, ‘full’ );

    echo ‘

    # September 18, 2012 at 6:39 pm

    This is what is in my entire loop.php file

    < ?php if(!is_single()) : global $more; $more = 0; endif; //enable more link ?>

    < ?php echo catch_that_image() ?>

    >

    “>< ?php the_title(); ?>

    < ?php the_content(); ?>

    < ?php edit_post_link(__('Edit', 'themify'), ''); ?>

    < ?php
    if (($wp_query->current_post + 1) < ($wp_query->post_count)) {
    echo ‘

    Post Divider

    ‘;
    }
    ?>

    # September 18, 2012 at 10:33 pm

    Sorry I posted the loop instead of functions I’ll post it in the morning.

    # September 19, 2012 at 9:12 am

    This is just the top of the functions.php file

    < ?php

    ///////////////////////////////////////
    // You may add your custom functions here
    ///////////////////////////////////////

    function catch_that_image() {
    global $post, $posts;
    $first_img = ”;
    ob_start();
    ob_end_clean();
    $output = preg_match_all(‘//i’, $post->post_content, $matches);
    $first_img = $matches [1] [0];

    if(empty($first_img)){ //Defines a default image
    $first_img = “/images/default.jpg”;
    }
    return $first_img;
    }

    ///////////////////////////////////////
    // Load theme languages
    ///////////////////////////////////////
    load_theme_textdomain( ‘themify’, TEMPLATEPATH.’/languages’ );

    ///////////////////////////////////////
    // Enable WordPress feature image
    ///////////////////////////////////////
    add_theme_support( ‘post-thumbnails’);

    ///////////////////////////////////////
    // Register Custom Menu Function
    ///////////////////////////////////////
    if (function_exists(‘register_nav_menus’)) {
    register_nav_menus( array(
    ‘main-nav’ => __( ‘Main Navigation’, ‘themify’ ),
    ‘footer-nav’ => __( ‘Footer Navigation’, ‘themify’ ),
    ) );
    }

    # September 19, 2012 at 9:23 am

    @chanman Remove what you previously used and try the code I provided for you and put it in your theme file.

    You also seem to have an error in your functions.php file. I cannot access your site at all.

    # September 19, 2012 at 9:25 am

    I’m trying to do that right now but every time i try to save the file it gives me this

    Parse error: syntax error, unexpected $end in /nfs/c10/h02/mnt/142167/domains/yardsale.cdcwebdesign.com/html/wp-content/themes/base/functions.php on line 199

    # September 19, 2012 at 9:30 am

    @chanman Line 199 seems to be fixed now. You have an error on line 3 in loop.php. Perhaps that is `< ?php catch_that_image(); ?>`

    If so, remove that.

    # September 19, 2012 at 9:31 am

    I figured out the problem i left out the last } in your code.

    # September 19, 2012 at 9:33 am

    Ok, I got rid of my stuff and put yours and now it’s saying this:

    Fatal error: Call to undefined function catch_that_image() in /nfs/c10/h02/mnt/142167/domains/yardsale.cdcwebdesign.com/html/wp-content/themes/base/includes/loop.php on line 3

    that probably means I’m still calling my code and not yours right?

    # September 19, 2012 at 9:34 am

    This is whats on line 3

    < ?php echo catch_that_image() ?>

    # September 19, 2012 at 9:35 am

    @chanman The code I provide you with goes into the theme file (loop.php) not functions.php and you still have an error on line 3 in loop.php.

    # September 19, 2012 at 9:35 am

    @chanman Yes. Above I asked you to remove that.

    # September 19, 2012 at 9:38 am

    I thought it went in the functions file let me change it real quick and see what i get.

    # September 19, 2012 at 9:40 am

    @chanman Wrap it in php tags `< ?php THE CODE I PROVIDED GOES HERE ?>`

    < ?php
    function echo_first_image ($postID)
    {
    $args = array(
    ‘numberposts’ => 1,
    ‘order’=> ‘ASC’,
    ‘post_mime_type’ => ‘image’,
    ‘post_parent’ => $postID,
    ‘post_status’ => null,
    ‘post_type’ => ‘attachment’
    );

    $attachments = get_children( $args );

    //print_r($attachments);

    if ($attachments) {
    foreach($attachments as $attachment) {
    $image_attributes = wp_get_attachment_image_src( $attachment->ID, ‘thumbnail’ ) ? wp_get_attachment_image_src( $attachment->ID, ‘thumbnail’ ) : wp_get_attachment_image_src( $attachment->ID, ‘full’ );

    echo ‘

    Viewing 15 posts - 1 through 15 (of 34 total)

    You must be logged in to reply to this topic.

    Projects

    Colophon

    CSS-Tricks* is created, written by, and maintained by Chris Coyier. It is built on WordPress, hosted by MediaTemple, and the assets are served by MaxCDN. The fonts are Source Sans and Source Code Pro. It is made possible by viewers like you who subscribe to The Lodge and through advertising for products and services I like.

    *May or may not contain any actual "CSS" or "Tricks".