视觉排序为最终用户订购“自定义帖子类型”
-
16-10-2019 - |
题
现在,使用这些自定义帖子类型和其他内容,不一定需要按日期按时间顺序进行组织,例如:客户有100个T恤,作为自定义帖子类型的“衬衫”,他想设置其外观订单。
您建议采用哪种方法让网站的编辑/管理员对其订单进行排序?
其他提示
您可以过滤分类法
// to filter by category
function restrict_manage_posts() {
global $typenow;
if ( FB_CPT_POST_TYPE_1 == $typenow ) {
$args = array('orderby' => 'count', 'hide_empty' => true);
echo $this->get_taxonomy_html_select(FB_CPT_TAXONOMY_TYPE_1, $args);
}
}
function get_taxonomy_html_select($taxonomy_name, $args) {
$taxonomy = get_taxonomy($taxonomy_name);
$terms = get_terms($taxonomy_name, $args);
$label = __( 'Show all ' . $taxonomy->label, FB_CPT_TEXTDOMAIN );
$html = array();
$html[] = '<select style="min-width:155px;" id="' . $taxonomy_name . '" name="' . $taxonomy_name . '" class="postform">';
$html[] = '<option value="0">' . $label . '</option>';
if ( isset($_GET[$taxonomy_name]) )
$this_term = $_GET[$taxonomy_name];
else
$this_term = '';
foreach($terms as $term) {
$default = ( $this_term == $term->term_id ? ' selected="selected"' : '' );
$value = esc_attr($term->name);
$value = $value . ' (' . (int)$term->count . ')';
$html[] = "\t" . '<option value="' . $term->term_id . '"' . $default . '>' . $value . '</option>';
}
$html[] = '</select>' . "\n";
return implode( "\n", $html );
}
function request($request) {
global $pagenow;
if ( is_admin() && 'edit.php' == $pagenow && isset( $request[FB_CPT_TAXONOMY_TYPE_1] ) && FB_CPT_POST_TYPE_1 == $request[FB_CPT_TAXONOMY_TYPE_1] ) {
$request['taxonomy'] = FB_CPT_TAXONOMY_TYPE_1;
$request['term'] = get_term($request[FB_CPT_TAXONOMY_TYPE_1], FB_CPT_TAXONOMY_TYPE_1)->name;
unset($request['name']);
}
return $request;
}
使用此钩子
// to filter custom post type per custom taxonomy
add_action( 'restrict_manage_posts', array( &$this, 'restrict_manage_posts') );
add_action( 'request', array( &$this, 'request' ) );
我会查看菜单生成器管理屏幕。由于菜单只是一种自定义帖子类型,因此我敢肯定,其中一些东西可以用作灵感。
为了有一个真正的任意排序顺序(与任何后字段完全无关),您需要创建一个“排序值”(或“权重”)字段,并在该字段中为每个项目输入一个数字值。然后,您可以在该字段上排序。
如果您要做的就是为每件“衬衫”设置一个位置,那么为什么不只是用户菜单订单,那么您可以在WP_QUERY中按Menu_order进行排序。您可能必须制作一个设置帖子的元盒 menu_order
财产。然后在你 WP_Query
:
$shirts = new WP_Query( 'post_type=shirt&orderby=menu_order&order=ASC' );
这将类似于您在 Media Upload
画廊选项卡,用于设置页面的菜单订单。
还是我误会了什么?
如果要在插件或代码中创建帖子类型,我更喜欢做的是添加此代码,这使您可以为前端和后端设置默认订单,并在管理员中设置get变量WordPress管理员识别更改,如果您按照标题,日期或其他任何您在管理表中显示的内容进行排序,则将箭头放置。
add_filter( 'pre_get_posts' , 'my_cpt_order' ); // Runs before the posts are fetched
function my_cpt_order( $query ) {
// Check query and alter only the query needed
//echo '<PRE>'; print_r($query); echo '</PRE>';
if ($query->query['post_type'] == 'cpt' && !isset($query->query['orderby'])) {
$query->set( 'orderby' , 'title' );
$query->set( 'order' , 'asc' );
//get the arrow to show up over title in admin
if (is_admin()) {
$_GET['orderby'] = 'title';
$_GET['order'] = 'asc';
}
} //if
} //my_cpt_order
由于菜单_erd是默认值,如果您是按菜单_erder对自定义帖子类型进行排序,但是如果它是帖子类型,您不希望用户担心自己对自己进行排序,并且您想使用诸如标题之类的内容,非常方便。