如何动态更改定制邮购ASC/DESC MENU_ORDER WISE?

时间:2017-08-19 作者:Ravi

我已经用sort menu\\u order post在后端创建了自定义post类型,如屏幕截图所示。http://prntscr.com/gacw95

在后端排序中,升序/降序工作正常。

但我也需要在前面对ASC/DESC进行排序。

当我点击后端的order选项卡时,它将在后端对帖子进行排序,而不是在前端。

当后端发生变化时,如何对前端的自定义帖子进行排序?

Backend code

<?php
// ptype_gallery Custom Post Type
add_action( \'init\', \'ptype_gallery_post_type\' );
function ptype_gallery_post_type() {
    register_post_type( \'ptype_gallery\',
        array(
            \'labels\' => array(
                \'name\' => __( \'Gallery\', \'theme\' ),
                \'singular_name\' => __( \'Gallery\', \'theme\' ),
                \'add_new\' =>  __( \'Add New Gallery\', \'theme\' ),
                \'add_new_item\' =>  __( \'Add New Gallery\', \'theme\' ),
                \'edit_item\' =>  __( \'Edit Gallery\', \'theme\' ),
                \'new_item\' =>  __( \'New Gallery\', \'theme\' ),
                \'all_items\' =>  __( \'All Gallery\', \'theme\' ),
                \'view_item\' =>  __( \'View Gallery\', \'theme\' ),
                \'search_items\' =>  __( \'Search Gallery\', \'theme\' ),
                \'not_found\' =>   __( \'No Gallery found\', \'theme\' ),
                \'not_found_in_trash\' =>  __( \'No Gallery found in Trash\', \'theme\' ), 
                \'parent_item_colon\' => \'\',
                \'menu_name\' =>  __( \'Gallery\', \'theme\')
            ),
        \'public\' => true,
        \'has_archive\' => true,
        \'hierarchical\' => false,
        \'menu_position\' => 26,      
        \'supports\' => array( \'title\', \'page-attributes\', \'thumbnail\', \'editor\' ), 
        \'rewrite\'  => array( \'slug\' => \'gallery\', \'with_front\' => true ),
        \'menu_icon\' => \'dashicons-format-gallery\',  // Icon Path
        )
    );
}

// MetaBox
add_action( \'admin_init\', \'ptype_gallery_register_meta_box\' );
function ptype_gallery_register_meta_box()
{
    // Check if plugin is activated or included in theme
    if ( !class_exists( \'RW_Meta_Box\' ) )
    return;
    $prefix = \'ptype_gallery_\';
    $meta_box = array(
            \'id\' => \'gallery-settings\',
            \'title\' => \'Photo Gallery\',
            \'pages\' => array( \'ptype_gallery\' ),
            \'context\' => \'normal\',
            \'priority\' => \'core\',
            \'fields\' => array(          
                    /*array(
                        \'name\' => \'Specifications\',
                        \'desc\' => \'\',
                        \'id\' => $prefix . \'specs\',
                        \'type\' => \'textarea\',
                        \'std\' => \'\',
                        \'rows\' => \'10\'
                    ),*/

                    array(
                        \'name\' => \'Gallery Images\',
                        \'desc\' => \'\',
                        \'id\' => $prefix . \'images\',
                        \'type\' => \'image_advanced\'
                    ),

                 )
            );
    new RW_Meta_Box( $meta_box );
}

// Add a new column for the order
function add_new_ptype_gallery_column($ptype_gallery_columns) {
  $ptype_gallery_columns[\'menu_order\'] = "Order";
  return $ptype_gallery_columns;
}
add_action(\'manage_edit-ptype_gallery_columns\', \'add_new_ptype_gallery_column\');

// Render the column values
function show_order_column_gallery($name){
  global $post;

  switch ($name) {
    case \'menu_order\':
      $order = $post->menu_order;
      echo $order;
      break;
   default:
      break;
   }
}
add_action(\'manage_ptype_gallery_posts_custom_column\',\'show_order_column_gallery\');

// Set the column to be sortable
function order_column_register_sortable_gallery($columns){
  $columns[\'menu_order\'] = \'menu_order\';
  return $columns;
}
add_filter(\'manage_edit-ptype_gallery_sortable_columns\',\'order_column_register_sortable_gallery\');
?>
Front End code
  Template Name: Photo Gallery Page Template

 */

get_header();
?>
<div id="main">
    <div class="wrapper">
        <div id="container" class="fullwidth photo-gallery-section">
            <h2 class="pageTitle">
                <?php the_title(); ?>
            </h2>
            <?php
            $args = array(
                \'post_type\' => \'ptype_gallery\', \'posts_per_page\' => -1, \'post_status\' => \'publish\'
            );
            // the query
            $the_query = new WP_Query($args);               
            ?>

            <?php if ($the_query->have_posts()) : ?>

                <!-- pagination here -->

                <!-- the loop -->
                <?php
                while ($the_query->have_posts()) : $the_query->the_post();
                    $images = get_post_meta(get_the_ID(), \'ptype_gallery_images\');

                    if ($images) {
                        ?>
                        <h4 class="pageTitle">
                            <?php the_title(); ?>
                        </h4>
                        <div class="photogallery-section">
                            <?php
                            echo \'<div class=""></div><div class="productImages row">\';
                            foreach ($images as $image) {
                                $thumb = wp_get_attachment_image_src($image, \'product_thumb\');
                                $img = wp_get_attachment_image_src($image, \'full\');
                                $attachment = get_post($image);
                                ?>
                                <div class="grid2">
                                    <div class="productImgBx">
                                        <div class="productImg"><a data-fancybox="gallery" href="<?php echo $img[0]; ?>"><img src="<?php echo $thumb[0]; ?>"/></a></div>
                                        <div class="productImgTitle"><?php echo $attachment->post_content; ?></div>
                                    </div>
                                </div>
                                <?php
                            }
                            echo \'</div>\';
                            ?>
                        </div>

                    <?php } endwhile; ?>
                <!-- end of the loop -->

                <!-- pagination here -->

                <?php wp_reset_postdata(); ?>

            <?php else : ?>
                <p><?php _e(\'Sorry, no products found.\'); ?></p>
            <?php endif; ?>
        </div>
    </div>
</div>
<?php get_footer(); ?>
这是我的页面模板,在前端显示自定义帖子类型,但不能按照后端排序。

请帮帮我。

谢谢你的到来。

1 个回复
SO网友:hwl

你可以钩住pre_get_posts 并使用order_by 的参数WP_Query.来自插件或函数。活动主题的php,类似于以下(未测试的示例):

add_action( \'pre_get_posts\', \'my_post_type_sort\', 10, 1);

function my_post_type_sort( $query ) {
     if ( is_admin || ! $query->is_main_query() ) {
        return;
     }
     if ( $query->get(\'post_type\') !== \'name_of_post_type\' ) {
        return;
     }
     $query->set(\'orderby\', array( \'menu_order\' => \'ASC\') );

} 

结束

相关推荐

Sort posts by activity date

我有一个不同日期发布的帖子列表我可以按日期订购邮件$loop_args = array( \'post_type\' => \'post\', \'order_by\' => \'date\', \'order\' => \'ASC\' ) 如果帖子有评论,我如何按评论日期订购帖子。因此,请按活动对帖子进行排序