Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

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

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 34 total)
  • Author
    Posts
  • #39909
    chanman
    Member

    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

    #110303
    chrisburton
    Participant

    @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 ‘

    #110305
    chanman
    Member

    This is what is in my entire loop.php file

    >

    “>

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

    Post Divider

    ‘;
    }
    ?>

    #110315
    chanman
    Member

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

    #110341
    chanman
    Member

    This is just the top of the functions.php file

    // 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’ ),
    ) );
    }

    #110342
    chrisburton
    Participant

    @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.

    #110343
    chanman
    Member

    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

    #110345
    chrisburton
    Participant

    @chanman Line 199 seems to be fixed now. You have an error on line 3 in loop.php. Perhaps that is ``

    If so, remove that.

    #110346
    chanman
    Member

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

    #110347
    chanman
    Member

    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?

    #110348
    chanman
    Member

    This is whats on line 3

    #110349
    chrisburton
    Participant

    @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.

    #110350
    chrisburton
    Participant

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

    #110351
    chanman
    Member

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

    #110352
    chrisburton
    Participant

    @chanman Wrap it in php tags ``

    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 ‘

    Author
    Posts
Viewing 15 posts - 1 through 15 (of 34 total)
  • The forum ‘Back End’ is closed to new topics and replies.