php 自定义帖子类型为插件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 自定义帖子类型为插件相关的知识,希望对你有一定的参考价值。
save this as [post-type-name]-cpt.php in wp-content/plugins/ folder
<?php
/*
Plugin Name: Portfolio Custom Post Type
Plugin URI: https://roseandpixel.com
Description: Custom Post Types for Portfolio
Author: Rachael Portier
Version:1.0.0
Author URI:https://roseandpixel.com
*/
/*
This code is a plugin to create a Custom Post Type in WordPress, it can be used with any WordPress theme.
The action initialises the function below it.
This example uses the term 'Designs' as its name, a search and replace will allow any name to be used, making sure plural and singular versions of the name are replaced.
Also replace the name in 'rewrite' and in the 'register_post_type' function.
To activate this as a plugin just add to wp-contents/plugins and activate in Dashboard
This doesn't use all the labels and arguments possible but includes the main ones, you can see more here - https://codex.wordpress.org/Function_Reference/register_post_type
*/
add_action( 'init', 'create_portfolio_cpt' );
function create_portfolio_cpt() {
$labels = array(
'name' => __( 'Portfolio' ),
'singular_name' => __( 'Design' ),
'all_items' => __( 'All Designs' ),
'add_new' => _x( 'Add new Design', 'Designs' ),
'add_new_item' => __( 'Add new Design' ),
'edit_item' => __( 'Edit Design' ),
'new_item' => __( 'New Design' ),
'view_item' => __( 'View Design' ),
'search_items' => __( 'Search in Designs' ),
'not_found' => __( 'No Designs found' ),
'not_found_in_trash' => __( 'No Designs found in trash' ),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => false, // Set to false hides Archive Pages
'menu_icon' => 'dashicons-images-alt2', //pick one here ~> https://developer.wordpress.org/resource/dashicons/
'query_var' => true,
'menu_position' => 5,
'hierarchical' => true,
'publicly_queryable' => true, // Set to false hides Single Pages
'supports' => array( 'thumbnail' , 'custom-fields', 'excerpt', 'comments', 'title', 'editor', 'genesis-cpt-archives-settings')
);
register_post_type( 'portfolio', $args );
}
add_action('init' , 'register_cpt_category', 0 );
function register_cpt_category()
{
$labels = array(
'name' => _x( 'Portfolio Categories', 'portfolio' ),
'singular_name' => _x( 'Portfolio Category', 'portfolio' ),
'search_items' => __( 'Search Portfolio Categories' ),
'all_items' => __( 'All Portfolio Categories' ),
'parent_item' => __( 'Parent Portfolio Categories' ),
'parent_item_colon' => __( 'Parent Portfolio Category:' ),
'edit_item' => __( 'Edit Portfolio Category' ),
'update_item' => __( 'Update Portfolio Category' ),
'add_new_item' => __( 'Add New Portfolio Category' ),
'new_item_name' => __( 'New Portfolio Category' ),
);
register_taxonomy('portfolio-category','portfolio', array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'show_in_nav_menus' => true,
'rewrite' => array('slug' => 'portfolio-category', 'with_front' => false),
));
}
function my_flush_rewrite_rules() {
flush_rewrite_rules();
}
?>
以上是关于php 自定义帖子类型为插件的主要内容,如果未能解决你的问题,请参考以下文章
php 如果电子商务插件未激活,则在Upfront Builder导出的布局中显示“产品”自定义帖子类型的布局
PHP 检查当前帖子类型是否为自定义帖子类型 - WordPress
php 在“前端布局”列表中显示“自定义帖子类型”。 Upfront隐藏'产品'CPT,因为它依赖于电子商务插件,此过滤器将显示CPT i