WordPress.org

Ready to get started?Download WordPress

Forums

Need to edit code that shows first image to include first video if no image (1 post)

  1. dubbinz
    Member
    Posted 1 year ago #

    Hi I have been using this code snippet I found on the web to display posts on the frontpage of my website and it works very well. However I need to edit it to do: look for first image, if no image, check for video, if no video post default image. So if posts have a youtube video it embeds youtube videos as well. The url for youtube videos are like this "http://www.youtube.com/watch?v=bapImCQ2vXY". here is the code I am currently using.

    function catch_that_image() {
      		global $post, $posts;
      		$first_img = '';
      		ob_start();
      		ob_end_clean();
      		$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
      		$first_img = $matches [1] [0];
    
      		if(empty($first_img)){ //Defines a default image
      			$first_img = bloginfo('template_directory');
        		$first_img .= "/images/default.png";
      		}
      		return $first_img;
    	}

    I have no idea how to go about adding lines to include a search for a youtube video as my php knowledge is slim to none so I have come here in the hope that someone may help me with this.

Topic Closed

This topic has been closed to new replies.

About this Topic