在unctions.php中添加多个自定义帖子类型,但仪表板中只显示一个自定义帖子类型

时间:2018-04-13 作者:Cheung

我是wordpress的新手,我正在尝试在函数中添加多个自定义帖子类型。php。

添加一个cpt即可。但如果我使用相同的函数添加第二个cpt(只更改函数名),则第二个cpt不会显示在仪表板菜单中。

以下是代码。我将非常感谢您在这方面的帮助。非常感谢。

// register a new post type with Divi builder on

 function create_new_cpt()
{
$labels = array(
    \'name\' => _x(\'Article\', \'Article\', \'divi\'), // CPT name is Article. Replace every instance of this CPT name with your own
    \'singular_name\' => _x(\'Article\', \'article\', \'divi\'),
    \'menu_name\' => __(\'Article\', \'divi\'),
    \'edit_item\' => __(\'Edit Article\', \'divi\'),
    \'add_new_item\'          => __( \'Add New Article\', \'divi\' ),
    \'update_item\'           => __( \'Update Article\', \'text_domain\' ),
    \'view_item\'             => __( \'View Article\', \'text_domain\' ),
    \'not_found_in_trash\' => __(\'Not found in Trash\', \'divi\')
);
$args   = array(
    \'labels\' => $labels,
    \'description\'       => __(\'Articles\', \'divi\'),
    \'supports\'          => array(\'title\', \'author\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\', \'revisions\', \'custom-fields\'),
    \'menu_icon\'         => \'dashicons-welcome-write-blog\',
    \'menu-position\'     => null,
    \'public\'            => true,
    \'publicly_queryable\'=> true,
    \'show_ui\'           => true,
    \'show_in_menu\'      => true,
    \'show_in_admin_bar\' => true,
    \'rewrite\'           => array( \'slug\' => \'article\' ),
    \'capability_type\'   => \'post\',
    \'can_export\'        => true,
    \'has_archive\'       => true,
    \'hierarchical\'      => false,
    \'publicly_queryable\'=> true
);
register_post_type(\'article\', $args); // registering the CPT. 
    $labels = array(
    \'name\'              => esc_html__( \'Article Categories\', \'divi\' ),
    \'singular_name\'     => esc_html__( \'Article Category\', \'divi\' ),
    \'search_items\'      => esc_html__( \'Search Categories\', \'divi\' ),
    \'all_items\'         => esc_html__( \'All Categories\', \'divi\' ),
    \'parent_item\'       => esc_html__( \'Parent Category\', \'divi\' ),
    \'parent_item_colon\' => esc_html__( \'Parent Category:\', \'divi\' ),
    \'edit_item\'         => esc_html__( \'Edit Category\', \'divi\' ),
    \'update_item\'       => esc_html__( \'Update Category\', \'divi\' ),
    \'add_new_item\'      => esc_html__( \'Add New Category\', \'divi\' ),
    \'new_item_name\'     => esc_html__( \'New Category Name\', \'divi\' ),
    \'menu_name\'         => esc_html__( \'Categories\', \'divi\' ),
);
// registering the custom taxomoy for this CPT. 
register_taxonomy( \'article_category\', array( \'article\' ), array(
    \'hierarchical\'      => true,
    \'labels\'            => $labels,
    \'show_ui\'           => true,
    \'show_admin_column\' => true,
    \'query_var\'         => true,
) );
$labels = array(
    \'name\'              => esc_html__( \'Article Tags\', \'divi\' ),
    \'singular_name\'     => esc_html__( \'Article Tag\', \'divi\' ),
    \'search_items\'      => esc_html__( \'Search Tags\', \'divi\' ),
    \'all_items\'         => esc_html__( \'All Tags\', \'divi\' ),
    \'parent_item\'       => esc_html__( \'Parent Tag\', \'divi\' ),
    \'parent_item_colon\' => esc_html__( \'Parent Tag:\', \'divi\' ),
    \'edit_item\'         => esc_html__( \'Edit Tag\', \'divi\' ),
    \'update_item\'       => esc_html__( \'Update Tag\', \'divi\' ),
    \'add_new_item\'      => esc_html__( \'Add New Tag\', \'divi\' ),
    \'new_item_name\'     => esc_html__( \'New Tag Name\', \'divi\' ),
    \'menu_name\'         => esc_html__( \'Tags\', \'divi\' ),
);
register_taxonomy( \'article_tag\', array( \'article\' ), array(
    \'hierarchical\'      => false,
    \'labels\'            => $labels,
    \'show_ui\'           => true,
    \'show_admin_column\' => true,
    \'query_var\'         => true,
) );

}    

     // adding divi page builder to this CPT
function add_db_to_article ($post_types) {
$custom_post_types = array (\'article\');
$output = array_merge($post_types, $custom_post_types);
return $output;
}   

add_action(\'init\', \'create_new_cpt\');
add_filter( \'et_builder_post_types\', \'add_db_to_article\' );


 //  register a new post type with Divi builder on

function create_new_cpt2()
{
$labels = array(
    \'name\' => _x(\'News\', \'News\', \'divi\'), // CPT name is News. Replace every instance of this CPT name with your own
    \'singular_name\' => _x(\'News\', \'news\', \'divi\'),
    \'menu_name\' => __(\'News\', \'divi\'),
    \'edit_item\' => __(\'Edit News\', \'divi\'),
    \'add_new_item\'          => __( \'Add New News\', \'divi\' ),
    \'update_item\'           => __( \'Update News\', \'text_domain\' ),
    \'view_item\'             => __( \'View News\', \'text_domain\' ),
    \'not_found_in_trash\' => __(\'Not found in Trash\', \'divi\')
);
$args   = array(
    \'labels\' => $labels,
    \'description\'       => __(\'News\', \'divi\'),
    \'supports\'          => array(\'title\', \'author\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\', \'revisions\', \'custom-fields\'),
    \'menu_icon\'         => \'dashicons-welcome-write-blog\',
    \'menu-position\'     => null,
    \'public\'            => true,
    \'publicly_queryable\'=> true,
    \'show_ui\'           => true,
    \'show_in_menu\'      => true,
    \'show_in_admin_bar\' => true,
    \'rewrite\'           => array( \'slug\' => \'news\' ),
    \'capability_type\'   => \'post\',
    \'can_export\'        => true,
    \'has_archive\'       => true,
    \'hierarchical\'      => false,
    \'publicly_queryable\'=> true
);

register_post_type(\'news\', $args); // registering the CPT. 
    $labels = array(
    \'name\'              => esc_html__( \'News Categories\', \'divi\' ),
    \'singular_name\'     => esc_html__( \'News Category\', \'divi\' ),
    \'search_items\'      => esc_html__( \'Search Categories\', \'divi\' ),
    \'all_items\'         => esc_html__( \'All Categories\', \'divi\' ),
    \'parent_item\'       => esc_html__( \'Parent Category\', \'divi\' ),
    \'parent_item_colon\' => esc_html__( \'Parent Category:\', \'divi\' ),
    \'edit_item\'         => esc_html__( \'Edit Category\', \'divi\' ),
    \'update_item\'       => esc_html__( \'Update Category\', \'divi\' ),
    \'add_new_item\'      => esc_html__( \'Add New Category\', \'divi\' ),
    \'new_item_name\'     => esc_html__( \'New Category Name\', \'divi\' ),
    \'menu_name\'         => esc_html__( \'Categories\', \'divi\' ),
);



// registering the custom taxomoy for this CPT. 
register_taxonomy( \'news_category\', array( \'news\' ), array(
    \'hierarchical\'      => true,
    \'labels\'            => $labels,
    \'show_ui\'           => true,
    \'show_admin_column\' => true,
    \'query_var\'         => true,
) );
$labels = array(
    \'name\'              => esc_html__( \'News Tags\', \'divi\' ),
    \'singular_name\'     => esc_html__( \'News Tag\', \'divi\' ),
    \'search_items\'      => esc_html__( \'Search Tags\', \'divi\' ),
    \'all_items\'         => esc_html__( \'All Tags\', \'divi\' ),
    \'parent_item\'       => esc_html__( \'Parent Tag\', \'divi\' ),
    \'parent_item_colon\' => esc_html__( \'Parent Tag:\', \'divi\' ),
    \'edit_item\'         => esc_html__( \'Edit Tag\', \'divi\' ),
    \'update_item\'       => esc_html__( \'Update Tag\', \'divi\' ),
    \'add_new_item\'      => esc_html__( \'Add New Tag\', \'divi\' ),
    \'new_item_name\'     => esc_html__( \'New Tag Name\', \'divi\' ),
    \'menu_name\'         => esc_html__( \'Tags\', \'divi\' ),
);
register_taxonomy( \'news_tag\', array( \'news\' ), array(
    \'hierarchical\'      => false,
    \'labels\'            => $labels,
    \'show_ui\'           => true,
    \'show_admin_column\' => true,
    \'query_var\'         => true,
) );

}

2 个回复
SO网友:VishwasDhamale

添加add_action(\'init\', \'create_new_cpt2\'); 在代码段中并修改add_db_to_article 功能如下:,

function add_db_to_article ($post_types) {
    $custom_post_types = array (\'article\', \'news\');
    $output = array_merge($post_types, $custom_post_types);
    return $output;
}

Note: \'news\' is added in $custom_post_types array.

SO网友:David Sword

缺少您的代码段

add_action(\'init\', \'create_new_cpt2\');

结束

相关推荐

从Functions.php访问插件数据

我正在尝试将wordpress帖子中的数据插入到新的表数据库表中,而不是WP_postmeta. 我的函数正在运行,它将插入$postid 没问题。我遇到的问题是,我需要从Marty Spellerbergs的“地址地理编码器”插件中插入纬度和经度坐标。在插件页面上,Marty说您可以使用以下内容访问循环中的Cooridate:<?php echo get_geocode_lng( $post->ID ); ?> <?php echo get_geocode_lat( $p