Grow your CSS skills. Land your dream job.

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

  • # September 19, 2012 at 9:44 am

    I figured that out after i saved it.

    # September 19, 2012 at 9:50 am

    @chanman Repaste your loop.php file without changing anything.

    # September 19, 2012 at 9:55 am

    It’s giving me an error but heres the code

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

    < ?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 ‘” < ?php post_class("post clearfix $class"); ?>>

    # September 19, 2012 at 10:00 am

    Here is the error it’s giving me:

    Fatal error: Cannot redeclare echo_first_image() (previously declared in /nfs/c10/h02/mnt/142167/domains/yardsale.cdcwebdesign.com/html/wp-content/themes/base/includes/loop.php:4) in /nfs/c10/h02/mnt/142167/domains/yardsale.cdcwebdesign.com/html/wp-content/themes/base/includes/loop.php on line 27

    # September 19, 2012 at 10:01 am

    Sorry for all the problems I’m new to wordpress and can’t figure it out yet.

    # September 19, 2012 at 10:03 am

    @chanman Try this just for kicks.

    < ?php
    if(!function_exists(‘echo_first_image’))
    {
    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 19, 2012 at 10:11 am

    it’s not working for me and it’s not giving an error you go check it out.

    http://www.yardsale.cdcwebdesign.com

    # September 19, 2012 at 10:13 am

    I don’t know if this matters but on the first post the image is in my media library and the second and third post they are called from the mapbox api.

    # September 19, 2012 at 10:16 am

    @chanman I might know a fix for your previous code. Repaste your old code into functions.php and loop.php but instead of `< ?php catch_that_image(); ?>`

    do this: `

    # September 19, 2012 at 10:24 am

    didn’t work

    here is the loop

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

    ” < ?php post_class("post clearfix $class"); ?>>

    here is the fuctions

    < ?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’ );

    # September 19, 2012 at 10:25 am

    it’s not giving an error either.

    # September 19, 2012 at 10:27 am

    @chanman Remove the img tag and just try `< ?php catch_that_image(); ?>` to see if it outputs anything.

    # September 19, 2012 at 10:30 am

    nothing yet.

    # September 19, 2012 at 10:33 am

    Then you must have changed something. Are you trying to get the first image of every post?

    # September 19, 2012 at 10:39 am

    yep, i’m not very good at php yet. Do you know of a way you access my code to help me? Is there a way to ad an admin to wordpress or a temp admin?

Viewing 15 posts - 16 through 30 (of 34 total)

You must be logged in to reply to this topic.

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