Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to add a slider on my front blog page for every gallery post, but I have some trouble extracting the images from the post gallery, using the following code:

$post_content = get_the_content();
preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids);
$array_id = explode(",", $ids[1]);

The problem is that each gallery post consists of paragraph with text, "read more" break and a gallery after the break and thus get_the_content() skips the images because they are after the break. How can I make it get the whole content, regardless of breaks?

I use "read more" because I want short description about the gallery post before clicking on it to read it.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

get_the_content is a template tag and would only work reliably inside a Loop. That means that you should also be able to use the $post global instead.

global $post; // may not be necessary unless you have scope issues
              // for example, this is inside a function
$post_content = $post->post_content;
preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids);
$array_id = explode(",", $ids[1]);

You can then use wp_get_attachment_image to actually get the images.

foreach ($array_ids as $id) {
  echo  wp_get_attachment_image($id);
}
share|improve this answer
    
Oh, that solved it. Thank you very much! –  Ziik Jul 5 '13 at 15:15
    
Hi again, after using this code, can you please help me find a way to get all the images by their ID using the values in $array_id ? –  Ziik Jul 8 '13 at 11:51
    
    
Thanks! I figured it out. –  Ziik Jul 8 '13 at 16:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.