The MU forums have moved to WordPress.org

Get a first image of a post with preg_match_all (24 posts)

  1. Romik84
    Member
    Posted 12 years ago #

    Hi there,

    I would need a help with preg_match_all function. I try to get first image of a post for related posts plugin. Currently I use following function:

    function get_first_image() {
     global $post, $posts;
     $first_img = '';
     $output = preg_match_all('/< *img[^>]*src *= *["\']?([^"\']*)/', $post->post_content, $matches);
     $first_img = $matches [1][0];
     return $first_img;
    }

    This function is working without a problem. The problem is that some posts are not in the wp_posts table so it doesn't show any picture for some items. Some posts contents are in wp_post_settings table created by a plugin where new posts contents are saved. How should the function looks now to work for both (for classic post content in the wp_posts as well as for wp_post_settings.

    Thanks.

  2. SteveAtty
    Member
    Posted 12 years ago #

    Why is the plugin not storing the complete post in the _posts table? That strikes me as just asking for trouble.

  3. Romik84
    Member
    Posted 12 years ago #

    I don't know maybe because it's plugin which generates shop items and not classic posts.

  4. SteveAtty
    Member
    Posted 12 years ago #

    So you'll need to query that table and put the results of that query through the preg_match as well.

  5. Romik84
    Member
    Posted 12 years ago #

    yes I know, but I don't know how as I don't know too much about PHP. :S

  6. Romik84
    Member
    Posted 12 years ago #

    Will anyone help me with this issue?

  7. SteveAtty
    Member
    Posted 12 years ago #

    If you only want the first image then use preg_match rather than preg_match_all.

    You'll need to write an SQL query to pull back the relevant column from the additional table into a variable and then put that through a preg_match.

    so:

    function get_first_image() {
     	global $post, $wpdb;
    	preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/', $post->post_content, $matches);
     	$first_img[] = $matches [1];
    	# something like $sql="Select column_name from table_name where post_id=".$post->ID;
    	$sql="Your query in here";
    	$result = $wpdb->get_row($sql);
    	preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/', $result->column_name, $matches);
    	$first_img[] = $matches[1];
     return $first_img[0];
    }

    But if you're not really sure about php coding you're going to have problems troubleshooting code.

  8. Romik84
    Member
    Posted 12 years ago #

    Thanks for your help. I have little problem with the code. I tried this:

    function get_first_image() {
     	global $post, $wpdb;
    	preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/', $post->post_content, $matches);
     	$first_img[] = $matches [1];
    $sql="SELECT Product_Image_Small FROM wp_4_post_settings WHERE post_id=".$post->ID;
    	$result = $wpdb->get_row($sql);
    	preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/', $result->Product_Image_Small, $matches);
    	$first_img[] = $matches [1];
     return $first_img[0];
    }

    but have no luck. Am I doing something wrong? Or missing something? By the way, should be this code

    $result = $wpdb->get_row($sql);

    better

    $result = $wpdb->get_col($sql);

    when I call a column? Confused :/

  9. Romik84
    Member
    Posted 12 years ago #

    anyone for help?

  10. andrea_r
    Moderator
    Posted 12 years ago #

    Why not use the internal post_thumbnail function?

  11. Romik84
    Member
    Posted 12 years ago #

    Because the images (thumbnails) are not located on my server and I need to read the image URL.

  12. andrea_r
    Moderator
    Posted 12 years ago #

    Justin Tadlock's get the Image plugin.

  13. Romik84
    Member
    Posted 12 years ago #

    thanks Andrea for your idea and I have tried this plugin but it doesn't work for images saved in the wp_post_settings table as it scan only post_content. I have used this function <?php get_the_image( array( 'image_scan' => true ) ); ?> and works only with the post_content. Any ideas?

  14. SteveAtty
    Member
    Posted 12 years ago #

    Why not take it up with the developer of the plugin that is putting "key" post data in a non standard WP table.

  15. Romik84
    Member
    Posted 12 years ago #

    he has a poor support as it's a free plugin. No reply a long time :/

  16. andrea_r
    Moderator
    Posted 12 years ago #

    edit the plugin itself?

  17. Romik84
    Member
    Posted 12 years ago #

    As I have seen the plugin uses same function preg_match_all but I am not sure how to modify the functions as my PHP experiences are limited :).

  18. Romik84
    Member
    Posted 12 years ago #

    any new ideas? I really need a help with this.

  19. Romik84
    Member
    Posted 12 years ago #

    anyone help me?

  20. SteveAtty
    Member
    Posted 12 years ago #


  21. Romik84
    Member
    Posted 12 years ago #

    yes i did but had no results. did i use correct code? please see above.

  22. SteveAtty
    Member
    Posted 12 years ago #

    Have you tried a var_dump on the matches array?

    I think people expect people to be able to write/debug php code if they're messing round with WPMU

  23. Romik84
    Member
    Posted 12 years ago #

    I really don't know how to use that :/

  24. Romik84
    Member
    Posted 12 years ago #

    so I think I have test the var_dump. For the first $matches I got

    array(2) { [0]=>

    for the second $matches I got

    } array(0) { }

    so, it means it didn't find any picture. Right?

About this Topic