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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化