我已经用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(); ?>
这是我的页面模板,在前端显示自定义帖子类型,但不能按照后端排序。
请帮帮我。
谢谢你的到来。