我已经完成了我命名为手风琴的自定义帖子类型,并且我确实设置了一个简单的带有短代码的手风琴,它可以工作。但是现在我想知道如何允许用户为手风琴制作自己的 ID(类别),然后如何使用简码显示该类别(ID)。因为现在正如我所拥有的,他会展示你所做的每一个新的手风琴帖子,而你不能在不同的页面上制作不同的手风琴。
add_action('init', function(){
$labels = array(
'name' => _x('accordion', 'post type general name'),
'singular_name' => _x('Accordion', 'post type singular name'),
'add_new' => _x('Add New Accordion', 'Accordion'),
'add_new_item' => __('Add New Accordion'),
'edit_item' => __('Edit Accordion'),
'new_item' => __('New Accordion'),
'all_items' => __('All Accordions'),
'view_item' => __('View Accordions'),
'search_items' => __('Search Accordions'),
'not_found' => __('No Accordion found'),
'not_found_in_trash' => __('No Accordion found in Trash'),
'parent_item_colon' => '',
'menu_name' => 'Accordion'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'thumbnail', 'page-attributes')
);
register_post_type('Accordion', $args);
});
add_shortcode('accordion', function(){
$posts = get_posts(array(
'numberposts' => 10,
'orderby' => 'menu_order',
'order' => 'ASC',
'post_type' => 'accordion',
));
$accordion = '<div id="accordion" class="accordion">'; ///Open the container
foreach ( $posts as $post ){
$accordion .= sprintf(('<h2>%1$s</a></h2><div>%2$s</div>'), // Generate the markup for each Question
$post->post_title,
wpautop($post->post_content)
);
}
$accordion .= '</div>'; //Close the Container
return $accordion; //Return the HTML
});