creare Custom Post Types image

Use and implementation of custom posts

Website postings are among the most important elements when it comes to personalizing your site. Through them we can adapt the site for any niche, bringing personalized features. In WordPress it is not recommended to edit or modify the classic posts, the ones existing by default in WordPress, because they can be rewritten once a new update is available and installed. Instead, we can create similar, custom posts, which we can create in the function.php file (preferably in a Child theme).

To better understand what personalized posts mean and what we can do with them, here are some examples:

  • Woocommerce uses custom product postings;
  • Ad sites use such personalized postings, whether it’s a real estate site or an ad site for various market niches (car, job recruitment, etc.);
  • movie sites use custom posts, and examples can go on.

If you create Custom Post Types, you will see in the WordPress main dashboard menu a new section of articles (posts), but renamed by you depending on the purpose for which you created them. For example, if you make a site about cars, you’ll have a new posting option in the menu called “cars”, which will have similar default WordPress subfile posts as “all cars”, “add a new car”, “car categories” and “car labels”.

How can you create custom posts?

To create custom posts, you can use and customize the following PHP code, which you need to enter in the child function’s function.php file.

if ( ! function_exists('custom_post_type') ) {

// Register Custom Post Type
function custom_post_type() {

	$labels = array(
		'name'                  => _x( 'automobile', 'Post Type General Name', 'text_domain' ),
		'singular_name'         => _x( 'automobil', 'Post Type Singular Name', 'text_domain' ),
		'menu_name'             => __( 'automobile', 'text_domain' ),
		'name_admin_bar'        => __( 'automobile', 'text_domain' ),
		'archives'              => __( 'toate automobilele', 'text_domain' ),
		'attributes'            => __( 'Item Attributes', 'text_domain' ),
		'parent_item_colon'     => __( 'Parent Item:', 'text_domain' ),
		'all_items'             => __( 'Toate automobilele', 'text_domain' ),
		'add_new_item'          => __( 'Adauga un automobil nou', 'text_domain' ),
		'add_new'               => __( 'Adauga', 'text_domain' ),
		'new_item'              => __( 'Adauga automobil', 'text_domain' ),
		'edit_item'             => __( 'Editeaza automobil', 'text_domain' ),
		'update_item'           => __( 'Update automobil', 'text_domain' ),
		'view_item'             => __( 'Vezi automobil', 'text_domain' ),
		'view_items'            => __( 'Vezi automobilele', 'text_domain' ),
		'search_items'          => __( 'Cauta automobile', 'text_domain' ),
		'not_found'             => __( 'Not found', 'text_domain' ),
		'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),
		'featured_image'        => __( 'Featured Image', 'text_domain' ),
		'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
		'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
		'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
		'insert_into_item'      => __( 'Insert into item', 'text_domain' ),
		'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
		'items_list'            => __( 'Items list', 'text_domain' ),
		'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
		'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
	);
	$rewrite = array(
		'slug'                  => 'automobile',
		'with_front'            => true,
		'pages'                 => true,
		'feeds'                 => true,
	);
	$args = array(
		'label'                 => __( 'automobil', 'text_domain' ),
		'description'           => __( 'Aceasta este descrierea pentru articolele cu denumirea \"automobile\"', 'text_domain' ),
		'labels'                => $labels,
		'supports'              => array( 'title', 'editor', 'thumbnail', 'comments' ),
		'taxonomies'            => array( 'category', 'post_tag' ),
		'hierarchical'          => false,
		'public'                => true,
		'show_ui'               => true,
		'show_in_menu'          => true,
		'menu_position'         => 5,
		'menu_icon'             => 'dashicons-car',
		'show_in_admin_bar'     => true,
		'show_in_nav_menus'     => true,
		'can_export'            => true,
		'has_archive'           => true,
		'exclude_from_search'   => false,
		'publicly_queryable'    => true,
		'rewrite'               => $rewrite,
		'capability_type'       => 'page',
	);
	register_post_type( 'post_type', $args );

}
add_action( 'init', 'custom_post_type', 0 );

}

Brief explanation of the code:

  1. At the beginning of the code, it is specified that if there is no function with the name custom_post_type () (it can be any other name of your choice) that generates custom posts, it will be created in the following lines.
  2. The $ labels variable generates an array that names the labels that will be used in the WordPress menu. For example, for the “Add Post”, “Post Categories”, “All Posts” sub-tabs, etc.
  3. The $ rewrite variable generates an array that specifies certain information related to the display mode, for example: what slug will appear in the URLs of posts, whether to create individual pages for posts, etc.
  4. In the $ args variable is structured information such as: menu image (next to the post option), menu position, post content (title, description, thumbnail, comments), post archiving, whether they are public or not, etc.
  5. An action hook is used at the bottom of the code, through which we tell the program to add this information from the custom_post_type () function written above.

The code example above can be customized by replacing the word “car / automobile” with the name you want your posts to use.

Share: