Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author bjornjohansen

    (@bjornjohansen)

    Thread Starter tvermand

    (@tvermand)

    Thanks, you rock, works great!

    tvermand – i am struggling with the filter, could you paste what you did please?

    Thread Starter tvermand

    (@tvermand)

    <?php
    $args = array(
    'post_type' => 'attachment',
    'post_status' => null,
    'post_parent' => $fr_ID,
    'posts_per_page' => -1,
    'orderby' => 'menu_order',
    'order' => 'ASC'
    );
    
    $attachments = get_posts( $args );
    
    if( $attachments ) {
    
    $thumb_images = array();
    
    foreach( $attachments as $key => $attachment ) { 
    
    	$thumb_image = wp_get_attachment_image_src( $attachment -> ID, 'thumbnail' );
    
    	$thumb_images[] = $thumb_image[0];
    }
    natsort( $thumb_images );
    
    ?>
    
    <?php
    // Apply lazy load filter to images
    $img_lazy_load = '<img src="'.$thumb_images[0].'" alt="'.esc_attr( get_the_title() ).'" />';
    $img_lazy_load = apply_filters( 'bj_lazy_load_html', $img_lazy_load );
    echo $img_lazy_load;
    ?>

    I use a foreach because I have more than one image per post and need them after in my template.
    Hope it helps…

    Hi tvermand
    Thanks for replying with your code.
    I pasted it in but I get an unexpected $end message.
    It looks like after $attachments you have 2 opening {‘s in the code but only one closing }, where should the other closing } go?
    I presume placing the other } would sort my error.

    Thanks in advance.

    Thread Starter tvermand

    (@tvermand)

    Seem’s that it’s missing closing } related to if. Should be placed at the end.

    But my template has a lot of custom stuffs. See that shorter code that I used in content.php of twentyfourteen theme to test the plugin:

    <?php
    			$attachments = get_children(array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order'));
    			if ( ! is_array($attachments) ) continue;
    			$count = count($attachments);
    			$first_attachment = array_shift($attachments);
    			?>
    			<div class="left">
    				<?php
    				$img_html = wp_get_attachment_image($first_attachment->ID , 'thumbnail');
    				$img_html = apply_filters( 'bj_lazy_load_html', $img_html );
    				echo $img_html;
    				?>
    			</div>

    Is it working for you?

    I’m afraid putting the } at the end gave me cannot modify headers messages.

    I have tried pasting your second example in to the theme files but still to no avail, this has me puzzled…

    Hi,

    Does anyone know how to get this to work with ACF (Advanced custom fields).

    I want to apply a filter to :

    <img id="staffprofile" src="<?php the_sub_field('image'); ?>">

    I tried `<?php
    $img_html = ‘<img>’;
    $img_html = apply_filters( ‘bj_lazy_load_html’, $img_html );
    echo $img_html;
    ?>`

    Thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Not working with attachments’ is closed to new replies.