WordPress.org

Ready to get started?Download WordPress

Forums

Attachments
[resolved] Random sort order (10 posts)

  1. karl19
    Member
    Posted 1 year ago #

    Perhaps this isn't a very common request, but I'm looking for a way to randomise images.

    We're using the plugin for a frontpage slideshow, to be able to create a list of images from many different pages - with Attachments, it's easy to pick from the media library and make a custom slideshow. But we're trying to randomise it..

    Ideally, I'd like to find a way to randomise the images, then keep the randomised list in a string, so that I can output the same list in a couple of places.

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

  2. Jonathan Christopher
    Member
    Plugin Author

    Posted 1 year ago #

    That's a feature you'll need to build by hand and won't be part of Attachments' code base, but given the utility functions available you are able to implement what you're looking for, it's a matter of randomizing an array of numbers ranging from 0 to your total attachments, and then grabbing each one by one, using the utility functions offered. I'd love to help more but perhaps you have a friend who knows PHP and can help out?

  3. Jonathan Christopher
    Member
    Plugin Author

    Posted 1 year ago #

    Actually, you know what. This might in fact be within the scope of what could be expected. I'm going to think about what it would take and see if I can add it. In the meantime you can surely try something like I mentioned above and give it a shot by hand as well.

  4. karl19
    Member
    Posted 1 year ago #

    Many thanks for your input Jonathan! Perhaps it would be a sensible option to be able to randomise the results - but you know best what features you think are sensible for the plugin.

    I didn't manage to solve the randomising with Attachments and ended up using a setup where I added categories to image attachments, then running a normal WP query and randomising the results. Was the simplest way to finish this project. But I'm sure I'll end up using Attachments again for another project in the future!

  5. Jonathan Christopher
    Member
    Plugin Author

    Posted 1 year ago #

    This has been implemented as of version 3.4, and a usage example is provided in the docs. Enjoy!

  6. karl19
    Member
    Posted 1 year ago #

    That's great news! I'm sure it'll come in very handy for a future project.

    Thanks a lot for considering and implementing this feature! I've sent you a donation.

  7. Jonathan Christopher
    Member
    Plugin Author

    Posted 1 year ago #

    Wow! That's super generous of you thank you so much, I hope you enjoy Attachments!

  8. mttd
    Member
    Posted 1 year ago #

    hi,

    I can't seem to get the randomise feature to work, even if i add in a shuffle( $attachments ) in to the code in the loop...

    I've added the randomise function in to my functions.php file as directed but this breaks the script.

    I'm just using the default instance name of 'attachments'. any clues as to what is going wrong or what im doing wrong please?

    any help much appreciated! i'm loving attachments as it's just what i need, would be perfect if i could get this feature to work!

    cheers,
    Tom

  9. Jonathan Christopher
    Member
    Plugin Author

    Posted 1 year ago #

    You'll need to paste code samples and indicate which files they're in. If your instance name is attachments the code you'd add to functions.php is

    function my_attachments_randomize( $attachments )
    {
        return shuffle( $attachments );
    }
    
    add_filter( 'attachments_get_attachments', 'my_attachments_randomize' );
  10. mttd
    Member
    Posted 1 year ago #

    below is what I have in my functions.php file (overriding the default instance as my pages were already setup using the default instance.

    function my_attachments_randomise( $attachments )
    {
        return shuffle( $attachments );
    }
    
    add_filter( 'attachments_get_attachments', 'my_attachments_randomise' );
    
    function attachments( $attachments )
    {
      $fields         = array(
        array(
          'name'      => 'title',                         // unique field name
          'type'      => 'text',                          // registered field type
          'label'     => __( 'Title', 'attachments' ),    // label to display
          'default'   => 'title',                         // default value upon selection
        ),
        array(
          'name'      => 'caption',                       // unique field name
          'type'      => 'textarea',                      // registered field type
          'label'     => __( 'Caption', 'attachments' ),  // label to display
          'default'   => 'caption',                       // default value upon selection
        ),
      );
    
      $args = array(
    
        // title of the meta box (string)
        'label'         => 'Page/Post Attachments',
    
        // all post types to utilize (string|array)
        'post_type'     => array( 'post', 'page' ),
    
        // meta box position (string) (normal, side or advanced)
        'position'      => 'normal',
    
        // meta box priority (string) (high, default, low, core)
        'priority'      => 'high',
    
        // allowed file type(s) (array) (image|video|text|audio|application)
        'filetype'      => array( 'image' ),  // no filetype limit
    
        // include a note within the meta box (string)
        'note'          => 'Attach files here!',
    
        // by default new Attachments will be appended to the list
        // but you can have then prepend if you set this to false
        'append'        => true,
    
        // text for 'Attach' button in meta box (string)
        'button_text'   => __( 'Attach Files', 'attachments' ),
    
        // text for modal 'Attach' button (string)
        'modal_text'    => __( 'Attach', 'attachments' ),
    
        // which tab should be the default in the modal (string) (browse|upload)
        'router'        => 'browse',
    
        // fields array
        'fields'        => $fields,
    
      );
    
      $attachments->register( 'attachments', $args ); // unique instance name
    }
    
    add_action( 'attachments_register', 'attachments' );

    and here is what is in the template file:

    <ul class="grid full-page">
                            <?php 
    
                                $attachments = new Attachments( 'attachments' ); /* pass the instance name */
                                $count = 1;
    
                            ?>
                        <?php if( $attachments->exist() ) { 
    
                            ?>
    
                            <li <?php post_class('grid-item-copy'); ?> id="post_<?php the_ID(); ?>">
                                <?php echo wpautop(get_the_content(), true); ?>
                                <div class="social">
                                    <div class="fb-like" data-href="<?php the_permalink() ?>" data-send="true" data-layout="button_count" data-width="330" data-show-faces="false"></div>
                                </div>
                            </li>
    
                            <?php while( $attachments->get() ) : ?>
    
                            <li<?php echo (++$count%3 ? '' : ' class="end-of-row"'); ?>>
                                <h2>
                                    <a href="<?php echo $attachments->src( 'full' ); ?>" class="colorbox">
                                        <span class="item-image" style="background: url(<?php echo get_template_directory_uri() .'/scripts/timthumb.php?w=330&h=330&src='. $attachments->src(' full '); ?>) 0 0 no-repeat;">
                                            <img src="<?php echo get_template_directory_uri() .'/scripts/timthumb.php?w=330&h=330&f=2&src='. $attachments->src( 'full' ); ?>" alt="<?php echo trim(strip_tags( $attachments->field( 'caption' ) )); ?>" />
                                        </span><?php
                                            $profile_title = trim(strip_tags( $attachments->field( 'caption' )));
    
                                            if($profile_title != "") {
                                                echo '<span class="item-content">'. $profile_title .'</span>';
                                            }
                                    ?></a>
                                </h2>
                            </li>
                            <?php endwhile; ?>
                        <?php } else { ?>
    
                            <li <?php post_class('grid-item-copy'); ?> id="post_<?php the_ID(); ?>">
                                <?php the_content(); ?>
                            </li>
    
                        <?php } ?>
                        </ul>

    if I remove the shuffle() from the return statement then the attachments are shown, if I leave it nothing is shown...

    Many thanks,
    Tom

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags