Wordpress: excluding images inserted in the message 'from get_children

I have a page with a slide show at the top and images pasted into a row in the content area.

I need to exclude images that were inserted into the message from the slide show.

I currently exclude "Best Image", but this limits me to one image that can be inserted into the message.

Here is my existing code:

$thumbnail = get_post_thumbnail_id(); $images = get_children( 'post_type=attachment&post_mime_type=image&order=asc&orderby=menu_order&post_parent='.$post->ID .'&exclude='.$thumbnail); 

I used to use the image metadata description field to exclude images by typing "exclude". This is not as pleasant for the end user as we would like.

Any suggestions, plugins or code based!

Update: I updated the code, so now I get the URLs of images from post_content and check them for slideshow images.

  $content = $post->post_content; $inlineImages = array(); preg_match( '/src="([^"]*)"/i', $content, $inlineImages ) ; $thumbnail = get_post_thumbnail_id($post->ID); $images = get_children( 'post_type=attachment&post_mime_type=image&order=asc&orderby=menu_order&post_parent='.$post->ID .'&exclude='.$thumbnail); if ($images) { echo '<div id="slideshow">'; foreach ( $images as $attachment_id => $attachment ) { $image = wp_get_attachment_image_src( $attachment_id,array(900,265)); if (!in_array($image[0],$inlineImages)) { echo '<img src="'.$image[0].'" width="'. $image[1] .'" height="'. $image[2].'">'; } } echo '</div>'; } 

This solution is OK, although regex can be improved.

A more pleasant step is to add an array of images to a custom field field, which is updated after publication / publication or publication.

Any suggestions on how to do this?

+6
source share
7 answers

I updated the code, so now I get the URLs of the images from post_content and check them for slideshow images.

 $content = $post->post_content; $inlineImages = array(); preg_match( '/src="([^"]*)"/i', $content, $inlineImages ) ; $thumbnail = get_post_thumbnail_id($post->ID); $images = get_children( 'post_type=attachment&post_mime_type=image&order=asc&orderby=menu_order&post_parent='.$post->ID .'&exclude='.$thumbnail); if ($images) { echo '<div id="slideshow">'; foreach ( $images as $attachment_id => $attachment ) { $image = wp_get_attachment_image_src( $attachment_id,array(900,265)); if (!in_array($image[0],$inlineImages)) { echo '<img src="'.$image[0].'" width="'. $image[1] .'" height="'. $image[2].'">'; } } echo '</div>'; } 
+4
source

It was just necessary to do the same. Your original approach is how I wanted to do this - just exclude any images that were inserted into the message appearing in the slider. But I did not want the client to do something special for this to happen. Here is my code.

 $args = array( 'post_type' => 'attachment', 'post_mime_type'=>'image','numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID ); $attachments = get_posts($args); preg_match_all("/<img[^']*?src=\"([^']*?)\"[^']*?>/", $post->post_content, $matches, PREG_PATTERN_ORDER); /* $matches[1] holds the urls as an array */ foreach ( $attachments as $attachment ) { if(in_array($attachment->guid, $matches[1])){ continue;} wp_get_attachment_image( $attachment->ID , 'slider_size'); } 

The first bit receives all the images associated with the message. $ Preg_match_all gets all the images in the message body. Then, when we look at the images to display them in the slider, in_array checks the URLs of the images that were inserted with the URL of the image to be added to the slider, and moves on to the next one if there is a match.

Thanks for the message, made me think in the right direction.

+6
source

I think that the easiest way would be to use the Meteor Slideshow plugin to create a slide show for each page, and then insert a short code for the correct slide show in the content area of ​​the corresponding page. Yes, this means that you have to edit every page outside the page editor, but it also gives you simple and complete control over which photos to take and do not appear in each slide show, and the short code is very easy to insert using the page editor .

0
source

The Media Custom Fields plugin allows you to add user data to media elements in the same way as to custom message fields, providing a user-friendly way to mark only slide shows. From the FAQ plugin:

In addition to giving you the ability to add custom fields, your custom fields will appear in all relevant places. If you go to the multimedia section to manage your media, as usual, they will appear in the form of multimedia editing. Custom fields are also displayed when you upload an image to a message and in all other expected places.

One approach is to install this plugin and add a custom field, such as Slide with a value of TRUE , to your slide images through the Dashboard β†’ Media menu. Then your filter code could be:

 $thumbnail = get_post_thumbnail_id(); $images = get_children( /* removed for brevity */ ); if ($images) { // .. foreach ( $images as $attachment_id => $attachment ) { // skip images not marked for slideshow if ( !get_post_meta($attachment_id, 'Slide') ) continue; // otherwise do slideshow things } // .. } 

Note that with this approach, the value for Slide can be set to everything except the empty string. You can define Slide as a class constant somewhere to avoid hard coding.

<strong> Benefits

  • Requires no work on part of the poster
  • Future: the plugin seems to be based on the good old mail features, and attachments are just one kind of message, so there is nothing strange.
0
source

Here is another approach to this.

I would not use urls , because the image inserted inside the content area could have different sizes, for example, medium, reduced or full. So the url will not match.

Using the code above,

 function printMeImages() { $content = $post->post_content; $inlineImages = array(); // populate ids of images from wp-image-id preg_match_all( '/wp-image-([^"]*)"/i', $content, $inlineImages ) ; $thumbnail = get_post_thumbnail_id($post->ID); $images = get_children( 'post_type=attachment&post_mime_type=image&order=asc&orderby=menu_order&post_parent='.$post->ID .'&exclude='.$thumbnail); $out = ""; if ($images) { $out .= '<ul id="slideshow">'; foreach ( $images as $attachment_id => $attachment ) { $image = wp_get_attachment_image_src( $attachment_id,'desiredImageSize'); // $inlineImages[1] has ids to be discarded if (!in_array($attachment_id,$inlineImages[1])) { $out .= '<li><img src="'.$image[0].'" width="'. $image[1] .'" height="'. $image[2].'"></li>'; } } $out .= '</ul>'; } return $out; } 

Images that are not set as recognized and not used inside the message are received.

0
source

For performance reasons, I will not execute the code while the page is being rendered, but rather I will attach the code to the save_post click. When mail content is edited and saved, a hook will be called, and all attached images (or their identifiers) that are not used in the content are saved in the postmeta table. When a slider is displayed, image identifiers can be accessed via get_post_meta ($ post_id, 'image_ids_for_slider'). Like this, I can avoid the preg_match_all operation when rendering the page. This may not be the reason for performance when the content of the message is small and when only one slider is loaded, but usually I find this approach a little cleaner to scale the reasons.

 //hook this function to save post action function save_ids_of_image_attachments_not_used_in_the_content( $post_id, $post ) { $args = array( 'post_type' => 'attachment', 'post_mime_type'=>'image', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post_id ); $attachments = get_posts($args); preg_match_all("/<img[^']*?src=\"([^']*?)\"[^']*?>/", $post->post_content, $matches, PREG_PATTERN_ORDER); $ids_of_attached_images_not_in_content = array(); /* $matches[1] holds the urls as an array */ foreach ( $attachments as $attachment ) { if(in_array($attachment->guid, $matches[1])){ continue; } $ids_of_attached_images_not_in_content[] = $attachment->ID; } // save the image_ids as postmeta update_post_meta($post_id, 'image_ids_for_slider', $ids_of_attached_images_not_in_content); } add_action( 'save_post', 'save_ids_of_image_attachments_not_used_in_the_content', 10, 2 ); 
0
source

I did not fully understand your problem, but what about the exclusion of the div identifier in which the slideshow is present?

 $thumbnail = get_post_thumbnail_id(); $images = get_children('post_type=attachment&post_mime_type=image&order=asc&orderby=menu_order&post_parent='.$post->ID .'&exclude='.$thumbnail. '&NAME'); 

Replace "NAME" after the thumbnail. ' in brackets. Hope this helps.

-1
source

Source: https://habr.com/ru/post/904858/


All Articles