duan4739 2017-03-30 07:43
浏览 19
已采纳

在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.

  • 写回答

1条回答 默认 最新

  • douju6752 2017-03-30 08:29
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料