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.

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

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么