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 - 16 through 30 (of 34 total)
  • Author
    Posts
  • #110353
    chanman
    Member

    I figured that out after i saved it.

    #110355
    chrisburton
    Participant

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

    #110356
    chanman
    Member

    It’s giving me an error but heres the code

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

    “>

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

    Post Divider

    ‘;
    }
    ?>

    #110357
    chanman
    Member

    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

    #110358
    chanman
    Member

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

    #110359
    chrisburton
    Participant

    @chanman Try this just for kicks.

    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 ‘

    #110360
    chanman
    Member

    it’s not working for me and it’s not giving an error you go check it out.
    http://www.yardsale.cdcwebdesign.com

    #110361
    chanman
    Member

    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.

    #110362
    chrisburton
    Participant

    @chanman I might know a fix for your previous code. Repaste your old code into functions.php and loop.php but instead of ``

    do this: `

    #110364
    chanman
    Member

    didn’t work

    here is the loop

    >

    “>

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

    Post Divider

    ‘;
    }
    ?>

    here is the fuctions

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

    #110365
    chanman
    Member

    it’s not giving an error either.

    #110366
    chrisburton
    Participant

    @chanman Remove the img tag and just try `` to see if it outputs anything.

    #110367
    chanman
    Member

    nothing yet.

    #110368
    chrisburton
    Participant

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

    #110370
    chanman
    Member

    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)
  • The forum ‘Back End’ is closed to new topics and replies.