• First of all, thanks for this plugin.

    It’s almost perfect for me except I want to add the first image in the content when there’s no featured image.
    For this I added following code in the functions.php

    //function to call first uploaded image in functions file
    function main_image() {
    $files = get_children('post_parent='.get_the_ID().'&post_type=attachment
    &post_mime_type=image&order=desc');
      if($files) :
        $keys = array_reverse(array_keys($files));
        $j=0;
        $num = $keys[$j];
        $image=wp_get_attachment_image($num, 'large', true);
        $imagepieces = explode('"', $image);
        $imagepath = $imagepieces[1];
        $main=wp_get_attachment_url($num);
    		$template=get_template_directory();
    		$the_title=get_the_title();
        print "<img src='$main' alt='$the_title' class='frame' />";
      endif;
    }

    and then I can retrieve the first image in the content with this

    <?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) {
      echo get_the_post_thumbnail($post->ID);
    } else {
       echo main_image();
    } ?>

    I tried to add this to your plugin and I messed a lot between lines 744 -> 749 but couldn’t integrate this piece of code in there.

    I’ll be happy if you can help me out with this.

    Thanks

    https://www.ads-software.com/plugins/latest-post-shortcode/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Iulia Cazan

    (@iulia-cazan)

    Hi!

    You should adjust your function to receive the post ID as a parameter

    function main_image( $post_ID = 0 ) {
    	/** If the post ID is not specified, let's get the files for is the current object */
    	if ( empty( $post_ID ) ) {
    		$files = get_children( 'post_parent=' . get_theID() . '&post_type=attachment&post_mime_type=image&order=desc' );
    	} else {
    		$files = get_children( 'post_parent=' . (int) $post_ID . '&post_type=attachment&post_mime_type=image&order=desc' );
    	}
    	if ( $files ) :
    		$keys = array_reverse( array_keys( $files ) );
    		$j=0;
    		$num = $keys[$j];
    		$image=wp_get_attachment_image( $num, 'large', true );
    		$imagepieces = explode( '"', $image );
    		$imagepath = $imagepieces[1];
    		$main = wp_get_attachment_url( $num );
    		$template = get_template_directory();
    		$the_title = get_the_title();
    		print '<img src="' . $main . '" alt="' . $the_title .'" class="frame" />';
    	endif;
    }

    Then, send the parameter also in the lines you changed in my plugin

    <?php if ( ( function_exists( 'has_post_thumbnail' ) ) && ( has_post_thumbnail() ) ) {
      echo get_the_post_thumbnail( $post->ID );
    } else {
       echo main_image( $post->ID );
    } ?>

    Let me know if this helps

    Thread Starter talha8877

    (@talha8877)

    Honestly couldnt figure out how to display it in the $tile

    if ( ( function_exists( 'has_post_thumbnail' ) ) && ( has_post_thumbnail() ) ) {
    				if ( ! empty( $args['mainimage'] ) ) {
    					$mainimage = wp_get_attachment_image_src( main_image( intval( $post->ID ) ), $args['mainimage'] );
    					if ( ! empty( $mainimage[0] ) ) {
    						$tile = str_replace( '[mainimage]', '<img src="' . esc_url( $mainimage[0] ) . '" />', $tile );
    					} else {
    						$tile = str_replace( '[mainimage]', '', $tile );
    					}
    				} else {
    					$tile = str_replace( '[mainimage]', '', $tile );
    				}
    			}
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Getting first image instead of featured image’ is closed to new replies.