WordPress adds custom article type / classification function and custom sorting

When we choose WordPress program, we consider that there are many functions that can be extended. The themes and plugins we can choose can basically achieve the required functions. For example, when we use WordPress program to build enterprise website, we may need to customize the article type and classification function. Here, we sorted out WordPress to realize custom article type, classification and sorting.

1. Add custom article type

/ * add custom article type wpfans.org *
/add_ actiON( ‘init’, ‘create_ products_ post_ type’ );
// add portfolio
function create_ products_ post_ type() {
$labels = array(
‘name’ => __ (‘product ‘,’wpgp’), < br > 0’singular_ name’ => __ (‘product ‘,’wpgp’), < br > 0’add_ new’ => __ (‘add ‘,’ wpgp ‘), < br > 0’add_ new_ item’ => __ (‘New products’,’wpgp ‘), < br > 0’edit_ item’ => __ (‘edit product ‘,’ wpgp ‘), < br > 0’new-item’ => __ (‘New products’,’wpgp ‘), < br > 0’view_ item’ => __ (‘view product ‘,’ wpgp ‘), < br > 0’search_ items’ => __ (‘search products’,’wpgp ‘), < br > and’not_ found’ => __ (‘product not found ‘,’wpgp’), < br > 0’not_ found_ in_ trash’ => __ (‘product not found in dustbin ‘,’wpgp’), < br > 0’parent_ item_ colon’ => ”,
);

$args = array(
‘labels’ => $labels,
‘show_ ui’ => true, // Whether to generate a default UI for managing this post type in the admin
‘query_ var’ => true,
‘show_ in_ nav_ menus’ => false,
‘public’ => true, // Controls how the type is visible to authors and readers
‘capability_ type’ => ‘post’,
‘hierarchical’ => false,
‘menu_ icon’ => ‘dashicons-format-gallery’, // use a font icon, e.g. ‘dashicons-chart-pie’
‘has_ archive’ => true, // Enables post type archives
‘rewrite’ => array( ‘slug’ => ‘products’ ),
‘supports’ => array( ‘title’, ‘editor’, ‘thumbnail’, ‘excerpt’, ‘comments’, ‘custom-fields’, ‘page-attributes’ ),
‘can_ export’ => true,
);

register_ post_ type( ‘products’, $args );
}

2. Add classification function

add_ action( ‘init’, ‘register_ products_ taxonomy’);
// create two taxonomies, genres and writers for the post type “book”
function register_ products_ taxonomy() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
‘name’ => __ (‘product classification ‘,’wpgp’), < br > and’singular_ name’ => __ (‘product classification ‘,’wpgp’), < br > and’menu_ name’ => __ (‘product classification ‘,’wpgp’), < br > and’search_ items’ => __ (‘search ‘,’ wpgp ‘), < br > 0’all_ items’ => __ (‘All product categories’,’wpgp ‘), < br > the’parent_ item’ => __ (‘superior classification of the product classification ‘), < br > and’parent_ item_ colon’ => __ (‘superior classification of the product classification: ‘, < br > 0’edit_ item’ => __ (‘edit product classification ‘,’wpgp’), < br > 0’update_ item’ => __ (‘update product classification ‘,’wpgp’), < br > 0’add_ new_ item’ => __ (‘add new product category ‘,’wpgp’), < br > 0’new_ item_ name’ => __ (‘New product classification ‘,’wpgp’), < br > and);

$args = array(
‘hierarchical’ => true,
‘labels’ => $labels,
‘show_ ui’ => true,
‘show_ in_ menu’ => true,
‘show_ in_ nav_ menus’ => true,
‘query_ var’ => true,
‘has_ archive’ => false,
‘show_ admin_ column’ => true,
‘rewrite’ => array( ‘slug’ => ‘product’ ),
);

register_ taxonomy( ‘product’, ‘products’, $args );
}

3. Custom article sorting

/ / sort articles wpfans.org
add_ filter( ‘parse_ query’, ‘sort_ products_ by_ date’ );
function sort_ products_ by_ date() {
global $pagenow;

if ( is_ admin() && $pagenow ==’ edit.php ‘ && !empty($_ GET[‘post_ type’] == ‘products’) && !isset($_ GET[‘post_ status’]) && !isset($_ GET[‘orderby’]) ) {
wp_ redirect( admin_ url(‘ edit.php?post_ type=products&orderby=date&order=desc’) );
exit;
}
}

We can adjust it according to the actual needs.

Related Article
WordPress displays the registration time of the login user in the frontend

When using WordPress to build a website, some users want to display the user registration time in the frontend, but Read more

WordPress adds the number of SQL queries at the bottom

An excellent woodpress theme is definitely that the less SQL consulting is used, the better. How can we see how Read more

Some WordPress articles can be seen after comments / replies

If we want to hide part of the content of the article and need the user's comments and replies to Read more

WordPress common template function reference document

Basic template filefile namedescribe style.css Theme style file index.php Home page template file header.php Header template file single.php Single article Read more

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image