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

时间:2017-03-30 07:44:00

标签: php wordpress plugins

我必须更改添加到自定义帖子类型的图片附件的顺序。他们使用名为" WP Better Attachments"但是没有选项可以改变后端的顺序(拖动,更改标题或上传时间不会改变顺序)。

我搜索了代码,这就是我找到的:

<?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>';

                            }



                        }



                    ?>

可悲的是,我对php的了解有限,因此我们将非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

Wordpress get_posts函数可以在数组中获取有助于排序的参数。一些参数已经被传递 - 例如post_parent参数。

您没有提及如何订购帖子,但有很多选择。例如,要按附件名称排序,请按降序排列:

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

您可以在WP docs中查看可用选项的完整列表。

请注意,如果您向插件添加自定义代码,则在升级插件时可能会覆盖该代码。对于某些插件,有一个特殊文件在升级之间保留,但使用它会要求您将自定义排序代码与插件集成,这比解决方案稍微复杂一点。