WordPress.org

Ready to get started?Download WordPress

Forums

Attachments
[resolved] Supersized gallery - problem with comma (7 posts)

  1. karl19
    Member
    Posted 1 year ago #

    I'm using Attachments to show images through the jQuery Supersized image gallery, which is working fine.

    There is one issue though, which causes problems in IE. The image output for Supersized shows all images on one line each, ending with a comma - apart from the last line, which doesn't have a comma. So I was wondering if it's possible to output all attachments and when getting to the last one, do a slightly modified echo?

    This is what I'm using:

    $attachments = new Attachments( 'my_attachments', 1301 );
    if( $attachments->exist() ) :
      while( $attachment = $attachments->get() ) :
        echo "{image : '" . $attachments->src( 'full' ) . "'}," . "\n";
      endwhile;
    endif;

    Which gives this:

    {image : 'website/image.jpg'},
    {image : 'website/image.jpg'},
    {image : 'website/image.jpg'},

    I would like to somehow get rid of that last comma..

    http://wordpress.org/extend/plugins/attachments/

  2. Jonathan Christopher
    Member
    Plugin Author

    Posted 1 year ago #

    This isn't an issue with Attachments, have a look at http://php.net/substr

  3. karl19
    Member
    Posted 1 year ago #

    Thanks a lot for the reply Jonathan! Sorry, didn't mean to say that it was an issue with Attachments.

    That said, I'm not sure substr will help me - the way I understand it, it would work on ALL of the images I output? Essentially, I'm outputting some 20 photos, each line look like above (with a comma at the end) and I need to remove the comma ONLY from the last line (ie the last photo in the loop).

    I can do that with a normal while/if WP loop, but I don't understand how I can achieve that with the below, as I don't quite understand the syntax..

    if( $attachments->exist() ) :
      while( $attachment = $attachments->get() ) :
  4. JR Tashjian
    Member
    Posted 1 year ago #

    It looks like you are echoing out a comma right after the final curly brace (before the "\n" part)

    // Change this:
    echo "{image : '" . $attachments->src( 'full' ) . "'}," . "\n";
    
    // To this:
    echo "{image : '" . $attachments->src( 'full' ) . "'}" . "\n";
  5. karl19
    Member
    Posted 1 year ago #

    Hi JR, thanks for your input!

    You're right, I'm echoing a comma at the end of the line, but this is needed - apart from the very last line. So if the loop spits out three images, the list should look like this:

    {image : 'website/image.jpg'},
    {image : 'website/image.jpg'},
    {image : 'website/image.jpg'}

    Essentially, I need an if/else, where it checks if it's the last item in the attachments loop: if so, echo without comma, otherwise echo with comma.

    Somehow I don't seem to understand the attachments query loop, so I don't know how to write the if/else into the loop from the first post above.

  6. JR Tashjian
    Member
    Posted 1 year ago #

    Ah, ok I see now. You'll want to do something like this then:

    $attachments = new Attachments( 'my_attachments', 1301 );
    
    if( $attachments->exist() ) :
    
    	for ( $i=0; $i < $attachments->total(); $i++ ) :
    
    		$attachment = $attachments->get_single( $i );
    
    		if( $i == $attachments->total() - 1 ) :
    			echo "{image : '" . $attachments->src( 'full' ) . "'}" . "\n";
    		else :
    			echo "{image : '" . $attachments->src( 'full' ) . "'}," . "\n";
    		endif;
    
    	endfor;
    
    endif;

    This code is untested but should be pretty spot on or at least get you in the right direction. :)

  7. karl19
    Member
    Posted 1 year ago #

    Excellent, thanks a lot JR! At first I got the right number of images, but it just output something from wp-includes. But when I changed:

    $attachment = $attachments->get_single( $i );

    to:

    $attachment = $attachments->get( $i );

    things started to work properly. Thanks again for your input, very much appreciated!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.