WordPress

front end – Jak dynamicznie zmieniać niestandardowe zamówienie pocztowe ASC/DESC menu_order?

  • 19 sierpnia, 2017
  • 4 min read
front end – Jak dynamicznie zmieniać niestandardowe zamówienie pocztowe ASC/DESC menu_order?


Stworzyłem niestandardowy typ postu w zapleczu z sortowaniem postów menu_order, jak widać na zrzucie ekranu.

W zapleczu sortowanie ASCENDING/MDESENDING działa dobrze.

Ale muszę również posortować ASC/DESC z przodu.

Kiedy kliknę zakładkę zamówienia na zapleczu, post zostanie posortowany na zapleczu, ale nie na froncie.

Jak mogę posortować niestandardowy post z przodu po zmianie na zapleczu?

Kod zaplecza

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

Kod Front Endu

  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=" 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(); ?>

To jest mój szablon strony z wyświetlaniem niestandardowego typu postu na interfejsie, ale nie można go sortować zgodnie z zapleczem.

Warto przeczytać!  przepisywanie adresu URL - przepisywanie dla podstrony wydaje się tracić parametr

Proszę pomóż mi.

Dziękuję.


Źródło