php - Wordpress - targeting images in content -
i have following code in wordpress theme, single-post page (the page single posts displayed).
<article id="post-<?php the_id(); ?>"<?php post_class('col-md-12'); ?>> <div class="container"> <header class="entry-header"> <h1 class="entry-title"><?php the_title(); ?></h1> </header> <?php $thumb = get_post_thumbnail_id(); $img_url = wp_get_attachment_url( $thumb,'full' ); //get full url image (use "large" or "medium" if images big) $image = aq_resize( $img_url, 1200, 720, true ); //resize & crop image ?> <?php if($image) : ?> <img class="img-responsive singlepic" src="<?php echo $image ?>"/> <?php endif; ?> <?php if ( $post->post_type == 'data-design' && $post->post_status == 'publish' ) { if ( $attachments ) { foreach ( $attachments $attachment ) { $class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type ); $thumbimg = wp_get_attachment_link( $attachment->id, 'thumbnail-size', true ); echo '<li class="' . $class . ' data-design-thumbnail">' . $thumbimg . '</li>'; } } } ?> <div class="entry-content"> <?php the_content(); ?> <?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'pages:', 'web2feel' ), 'after' => '</div>', ) ); ?> </div><!-- .entry-content --> </div> </article><!-- #post-## -->
it puts post-title featured-image large image posts content, , rest.
i want target images in content, , display them same way featured-image displayed.
*note - changes 'if ( $attachments ) { ...' function doesn't change (nor removing it) not part does. code affects content when called ( '')
sometimes ago used following code strip off images in content area , display them separately apart contents. might you.
in functions.php
file -
function stripe_images_from_content($content){ $images = array(); $subject = $content; $subject = preg_replace('~<img [^\>]*\ />~', '', $subject); $subject = preg_replace('~\[caption[^\]]*\][^\]]*\]~', '', $subject); $image_urls = array(); $match_count = preg_match_all( '/<img.*src=["\']([^"\']*)["\'].*\/>/', $content, $image_urls ); if (is_numeric($match_count) && $match_count > 0) { if( is_array($image_urls[1]) , count($image_urls[1]) > 0 ){ foreach( $image_urls[1] $i ){ $featured_image_url = $i; $image_id = get_attachment_id_from_src( $featured_image_url ); if (! is_numeric($image_id)) { $match_count = preg_match( '/(.*)-\d+x\d+(\.(jpg|png|gif))/', $featured_image_url, $matches ); if (is_numeric($match_count) && $match_count > 0) { $base_image_url = $matches[1] . $matches[2]; $images[] = get_attachment_id_from_src( $base_image_url ); } } else{ $images[] = $image_id; } } } } $data = new stdclass(); $data->images = $images; $data->content = $subject; return $data;
}
what function images inserted in content area of post , strip them off. return image(s) , content without image.
in single.php
file-
$content = stripe_images_from_content(get_the_content());
now need 2 things. $content->images
contains images inserted in post content area. , $content->content
holds raw content images stripped off. first need use $content->images
wisely want display images , second replace the_content()
function echo wpautop($content->content);
hope might you.
Comments
Post a Comment