简体   繁体   English

对于 WordPress,如何使用 PHP 将自定义帖子显示为可排序表?

[英]How can I display custom posts as a sortable table with PHP for WordPress?

Ideally, I'm trying to do this with a plugin and then assign the table to a shortcode which I can use anywhere on a website.理想情况下,我正在尝试使用插件来执行此操作,然后将表格分配给我可以在网站上的任何地方使用的简码。

Here's my generic code to add a custom post type:这是我添加自定义帖子类型的通用代码:

    <?PHP

function create_post_type_customers(){
    // Set UI labels for CPT
    $labels = array(
        'name'                  =>  __('Customers'),
        'singular_name'         =>  __('Customer'),
        'menu_name'             =>  __('Customers'),
        'parent_item_colon'     =>  __('All Customers'),
        'all_items'             =>  __('All Customers'),
        'view_item'             =>  __('View Customer'),
        'add_new_item'          =>  __('Add New Customer'),
        'add_new'               =>  __('Add New'),
        'edit_item'             =>  __('Edit Customer'),
        'update_item'           =>  __('Update Customer'),
        'search_items'          =>  __('Search Customers'),
        'not_found'             =>  __('Not Found'),
        'not_found_in_trash'    =>  __('Not Found in Trash'),
    );
    
    // CPT Options
    $args = array(
        'label'                 =>  __('customers'),
        'description'           =>  __('Freedom Search Customers'),
        'labels'                =>  $labels,
        'supports'              =>  array('title', 'editor', 'revisions', 'custom-fields'), 
        'taxonomies'            =>  array('test'),  
        'hierarchical'          =>  false,
        'public'                =>  true,
        'show_ui'               =>  true,
        'show_in_menu'          =>  true,
        'show_in_nav_menu'      =>  true,
        'show_in_admin_bar'     =>  true,
        'menu_position'         =>  5,
        'has_archive'           =>  true,
        'rewrite'               =>  array('slug'    =>  'customers'),
        'exclude_from_search'   =>  false,
        'publicly_queryable'    =>  true,
        'capability_type'       =>  'post',
        'show_in_rest'          =>  true,
    );
    
    register_post_type('Customers', $args);
}

    add_action('init', 'create_post_type_customers', 0);

?>

I am fairly new to PHP and WordPress development, so some general direction would be brilliant, if not a full solution.我对 PHP 和 WordPress 开发相当陌生,所以如果不是一个完整的解决方案,一些大方向会很棒。

Note - register_post_type('Customers', $args);注意- register_post_type('Customers', $args); make Customers lowercase使客户小写

Here is a simple example how to start这是一个如何开始的简单示例

// Call shortcode by echo do_shortcode('[customers]') or [customers]

add_shortcode( 'customers', 'customers_shortcode' );
function customers_shortcode() {
    $args = array(
        'post_type' => 'customers',
        'posts_per_page' => -1,
        'post_status' => 'publish',
        'orderby' => 'DATE',
        'order' => 'DESC',
    );

    $customers = new WP_Query($args);

    if($customers->have_posts()):
        while($customers->have_posts()): $customers->the_post();
            //Do stuff here 
            the_title();
        endwhile;
    endif;
    wp_reset_query();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM