在Wordpress中更改自定义附件的顺序

I have to change the order of the image attachments that I add to my custom post types. They use the plugin called "WP Better Attachments" but there is no option to change the order in the backend (dragging, changing the title or time of upload does not change the order).

I searched for the code and this is what I found:

<?php 
                        $id = icl_object_id(get_the_ID(), 'momenten', true, 'nl');
                        $attachments = get_posts( array(

                            'post_type' => 'attachment',

                            'posts_per_page' => -1,

                            'post_parent' => $id ,

                            'exclude'     => get_post_thumbnail_id()

                        ) );



                        if ( $attachments ) {

                            foreach ( $attachments as $attachment ) {

                                $thumbimg = wp_get_attachment_image_src( $attachment->ID, 'bs-tumb', true );

                                $thumbimgbig = wp_get_attachment_image_src( $attachment->ID, 'large', true );

                                echo '<div class="small-12 medium-4 large-4 columns vxf"  itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">

                                <a href="'.$thumbimgbig[0].'" class="gallery-item" title="'.get_field('titel_afbeelding_'.ICL_LANGUAGE_CODE, $attachment->ID).'"  itemprop="contentUrl" data-size="'.$thumbimgbig[1].'x'.$thumbimgbig[2].'"><img src="' . $thumbimg[0] . '" class="thumbnail"  alt="'.get_post($attachment->ID)->post_title.'" temprop="thumbnail" ></a><div class="descrip" itemprop="caption description">'.get_field('titel_afbeelding_'.ICL_LANGUAGE_CODE, $attachment->ID).'</div></div>';

                            }



                        }



                    ?>

Sadly my knowledge of php is limited so any kind of help will be greatly appreciated.

The Wordpress get_posts function can take arguments in an array that help with sorting. Some arguments are already being passed - eg the post_parent argument.

You don't mention how you want the posts to be ordered, but there are quite a few options. For example, to sort by attachment name, in descending order, you can add:

$attachments = get_posts( array(
    'post_type' => 'attachment',
    'posts_per_page' => -1,
    'post_parent' => $id ,
    'exclude'     => get_post_thumbnail_id()
    'orderby'     => 'date',
    'order'       => 'DESC',
 ) );

You can see the full list of available options in the WP docs.

Be aware that if you add custom code to the plugin, it might get overwritten when you upgrade the plugin. For some plugins, there is a special file that is preserved between upgrades, but using it would require you integrate your custom sorting code with the plugin, which is a little more complex than the solution here.