SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Grabbing video/image from post without text

    Sorry if i have posted this in the wrong area, im new here.

    Im new to wordpress and have been trying to get to grips with how to call different elements in the loop. Im having troubble grabbling the first video/image in a post without grabbing the other content. Im only posting either a video or and image and some text but i want to pull the video/image seperatly in the loop.

  2. #2
    SitePoint Addict deadmix's Avatar
    Join Date
    Jun 2009
    Location
    france
    Posts
    205
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    to grabb images, open your functions.php and add this :

    Code:
    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 = "/images/default.jpg";
      }
      return $first_img;
    }
    Once done, you can call the function within the loop to display the first image from the post:

    Code:
    <?php echo catch_that_image() ?>
    Goodluck


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •