【问题标题】:Wordpress - Dynamic Custom Post Type in a wp_queryWordpress - wp_query 中的动态自定义帖子类型
【发布时间】:2019-10-03 11:22:02
【问题描述】:

我们都知道,在创建新的自定义帖子类型时,如果我们想查询该特定自定义帖子类型的数据库,我们必须在其中指定名称:

一种自定义帖子类型:

'post_type' => 'NAMECPT_ONE',

两个或多个自定义帖子类型

'post_type' => array('NAMECPT_ONE, NAMECPT_TWO'),

现在我想知道是否存在一种方法来创建新的自定义帖子类型,而不是通过添加 @987654323 来修改所有查询@可以拉出自定义帖子类型的整个列表并将其动态插入 @987654324 @

有可能吗?

方法创建新cpt:

/*
 * CREZIONE CUSTOM POST TYPE - SUITE
 */
add_action('init', 'crea_eventi_suite');
function crea_eventi_suite() {
    $labels = array(
        'name'               => __('Suite' , 'suite-plugin'),
        'singular_name'      => __('Suite' , 'suite-plugin'),
        'add_new'            => __('Aggiungi Evento', 'suite-plugin'),
        'add_new_item'       => __('Aggiungi Nuovo Evento' , 'suite-plugin'),
        'edit_item'          => __('Modifica Evento', 'suite-plugin'),
        'new_item'           => __('Nuovo Evento', 'suite-plugin'),
        'all_items'          => __('Tutti gli eventi Suite', 'suite-plugin'),
        'view_item'          => __('Vedi Eventi Suite' , 'suite-plugin'),
        'search_items'       => __('Cerca Evento Suite' , 'suite-plugin'),
        'not_found'          => __('Evento Non Trovato', 'suite-plugin'),
        'not_found_in_trash' => __('Evento Non Trovato nel cestino', 'suite-plugin'),
    );
    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'rewrite'            => array('slug' => 'eventi-suite'),
        'has_archive'        => true,
        'hierarchical'       => true,
        'menu_position'      => 22,
        'menu_icon'          => 'dashicons-welcome-write-blog',
        'supports'           => array(
            'title',
            'editor',
            'thumbnail',
            'excerpt',
            'page-attributes'
        ),
    );
    register_post_type('eventi-suite', $args);
}

【问题讨论】:

  • 你能告诉我使用哪种方法创建帖子类型吗?
  • @Jinesh 我创建了一个新插件,看我的问题我更新了方法

标签: wordpress custom-post-type


【解决方案1】:

你可以这样使用

/*
 * CREZIONE CUSTOM POST TYPE - SUITE
 */
add_action('init', 'crea_eventi_suite');
function crea_eventi_suite() {
    $labels = array(
        'name'               => __('Suite' , 'suite-plugin'),
        'singular_name'      => __('Suite' , 'suite-plugin'),
        'add_new'            => __('Aggiungi Evento', 'suite-plugin'),
        'add_new_item'       => __('Aggiungi Nuovo Evento' , 'suite-plugin'),
        'edit_item'          => __('Modifica Evento', 'suite-plugin'),
        'new_item'           => __('Nuovo Evento', 'suite-plugin'),
        'all_items'          => __('Tutti gli eventi Suite', 'suite-plugin'),
        'view_item'          => __('Vedi Eventi Suite' , 'suite-plugin'),
        'search_items'       => __('Cerca Evento Suite' , 'suite-plugin'),
        'not_found'          => __('Evento Non Trovato', 'suite-plugin'),
        'not_found_in_trash' => __('Evento Non Trovato nel cestino', 'suite-plugin'),
    );
    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'rewrite'            => array('slug' => 'eventi-suite'),
        'has_archive'        => true,
        'hierarchical'       => true,
        'menu_position'      => 22,
        'menu_icon'          => 'dashicons-welcome-write-blog',
        'supports'           => array(
            'title',
            'editor',
            'thumbnail',
            'excerpt',
            'page-attributes'
        ),
    );
   update_option('customer_postquery',get_option('customer_postquery')+',new-post_type');

    register_post_type('eventi-suite', $args);
}

现在当你想显示帖子时,你可以这样使用

 $args = array(  
       'post_type' => get_option('customer_postquery'),
       'post_status' => 'publish',
       'posts_per_page' => 8,
       ‘orderby’ => ‘title’,
       ‘order’ => ‘ASC’,
   );

   $loop = new WP_Query( $args );

【讨论】:

  • 每次我创建一个新的cpt时,我都会添加:update_option('customer_postquery',get_option('customer_postquery')+'new-post_type');并且工作完美?
  • 或者我在哪里看到'new-post_type',我用我的cpt的名字编辑?
  • 当您创建自定义帖子类型时,它会保存它 wp_option 并在您获取它时从中读取
  • 所以我唯一需要的是:update_option('customer_postquery',get_option('customer_postquery')+',new-post_type');而且我什么都不编辑,对吧?
  • 能否请您阅读我的另一个问题:stackoverflow.com/questions/58224214/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多