Register WordPress custom post type with custom category, custom tag, default post category and tags:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | <?php if ( ! function_exists( 'quote_create_post_type' ) ) : function quote_create_post_type() { $labels = array ( 'name' => 'Quote' , 'singular_name' => 'Quote' , 'add_new' => 'Add quote' , 'all_items' => 'All quotes' , 'add_new_item' => 'Add quote' , 'edit_item' => 'Edit quote' , 'new_item' => 'New quote' , 'view_item' => 'View quote' , 'search_items' => 'Search quotes' , 'not_found' => 'No quotes found' , 'not_found_in_trash' => 'No quotes found in trash' , 'parent_item_colon' => 'Parent quote' //'menu_name' => default to 'name' ); $args = array ( 'labels' => $labels , 'public' => true, 'has_archive' => true, 'publicly_queryable' => true, 'query_var' => true, 'rewrite' => true, 'capability_type' => 'post' , 'hierarchical' => false, 'supports' => array ( 'title' , 'editor' , 'excerpt' , 'thumbnail' , //'author', //'trackbacks', //'custom-fields', //'comments', 'revisions' , //'page-attributes', // (menu order, hierarchical must be true to show Parent option) //'post-formats', ), 'taxonomies' => array ( 'category' , 'post_tag' ), // add default post categories and tags 'menu_position' => 5, 'exclude_from_search' => false, 'register_meta_box_cb' => 'quote_add_post_type_metabox' ); register_post_type( 'quote' , $args ); //flush_rewrite_rules(); register_taxonomy( 'quote_category' , // register custom taxonomy - category 'quote' , array ( 'hierarchical' => true, 'labels' => array ( 'name' => 'Quote category' , 'singular_name' => 'Quote category' , ) ) ); register_taxonomy( 'quote_tag' , // register custom taxonomy - tag 'quote' , array ( 'hierarchical' => false, 'labels' => array ( 'name' => 'Quote tag' , 'singular_name' => 'Quote tag' , ) ) ); } add_action( 'init' , 'quote_create_post_type' ); function quote_add_post_type_metabox() { // add the meta box add_meta_box( 'quote_metabox' , 'Meta' , 'quote_metabox' , 'quote' , 'normal' ); } function quote_metabox() { global $post ; // Noncename needed to verify where the data originated echo '<input type="hidden" name="quote_post_noncename" value="' . wp_create_nonce( plugin_basename( __FILE__ ) ) . '" />' ; // Get the data if its already been entered $quote_post_name = get_post_meta( $post ->ID, '_quote_post_name' , true); $quote_post_desc = get_post_meta( $post ->ID, '_quote_post_desc' , true); // Echo out the field ?> <table class = "form-table" > <tr> <th> <label>Name</label> </th> <td> <input type= "text" name= "quote_post_name" class = "regular-text" value= "<?php echo $quote_post_name; ?>" > <!-- classes: .small-text .regular-text .large-text --> </td> </tr> <tr> <th> <label>Description</label> </th> <td> <textarea name= "quote_post_desc" class = "large-text" ><?php echo $quote_post_desc ; ?></textarea> </td> </tr> </table> <?php } function quote_post_save_meta( $post_id , $post ) { // save the data /* * We need to verify this came from our screen and with proper authorization, * because the save_post action can be triggered at other times. */ if ( ! isset( $_POST [ 'quote_post_noncename' ] ) ) { // Check if our nonce is set. return ; } // verify this came from the our screen and with proper authorization, // because save_post can be triggered at other times if ( !wp_verify_nonce( $_POST [ 'quote_post_noncename' ], plugin_basename( __FILE__ ) ) ) { return $post ->ID; } // is the user allowed to edit the post or page? if ( ! current_user_can( 'edit_post' , $post ->ID )){ return $post ->ID; } // ok, we're authenticated: we need to find and save the data // we'll put it into an array to make it easier to loop though $quote_post_meta [ '_quote_post_name' ] = $_POST [ 'quote_post_name' ]; $quote_post_meta [ '_quote_post_desc' ] = $_POST [ 'quote_post_desc' ]; // add values as custom fields foreach ( $quote_post_meta as $key => $value ) { // cycle through the $quote_post_meta array // if( $post->post_type == 'revision' ) return; // don't store custom data twice $value = implode( ',' , ( array ) $value ); // if $value is an array, make it a CSV (unlikely) if ( get_post_meta( $post ->ID, $key , FALSE ) ) { // if the custom field already has a value update_post_meta( $post ->ID, $key , $value ); } else { // if the custom field doesn't have a value add_post_meta( $post ->ID, $key , $value ); } if ( ! $value ) { // delete if blank delete_post_meta( $post ->ID, $key ); } } } add_action( 'save_post' , 'quote_post_save_meta' , 1, 2 ); // save the custom fields endif ; // end of function_exists() if ( ! function_exists( 'view_quotes_posts' ) ) : // output function view_quotes_posts( $do_shortcode = 1, $strip_shortcodes = 0 ) { $args = array ( 'posts_per_page' => 100, 'offset' => 0, //'category' => , 'orderby' => 'menu_order, post_title' , // post_date, rand 'order' => 'DESC' , //'include' => , //'exclude' => , //'meta_key' => , //'meta_value' => , 'post_type' => 'quote' , //'post_mime_type' => , //'post_parent' => , 'post_status' => 'publish' , 'suppress_filters' => true ); $posts = get_posts( $args ); $html = '' ; foreach ( $posts as $post ) { $meta_name = get_post_meta( $post ->ID, '_quote_post_name' , true ); $meta_desc = get_post_meta( $post ->ID, '_quote_post_desc' , true ); $img = get_the_post_thumbnail( $post ->ID, 'medium' ); if ( empty ( $img ) ) { $img = '<img src="' .plugins_url( '/img/default.png' , __FILE__ ). '">' ; } if ( has_post_thumbnail( $post ->ID ) ) { $img = wp_get_attachment_image_src( get_post_thumbnail_id( $post ->ID ), 'thumbnail' ); $img_url = $img [0]; //the_post_thumbnail( 'thumbnail' ); /* thumbnail, medium, large, full, thumb-100, thumb-200, thumb-400, array(100,100) */ } $content = $post ->post_content; if ( $do_shortcode == 1 ) { $content = do_shortcode( $content ); } if ( $strip_shortcodes == 1 ) { $content = strip_shortcodes( $content ); } $content = wp_trim_words( $content , 30, '...' ); $content = wpautop( $content ); $html .= ' <div> <h3> '.$post->post_title.' </h3> <div> <p>Name: '.$meta_name.' </p> <p>Description: '.$meta_desc.' </p> </div> <div> '.$img.' </div> <div> '.$content.' </div> </div> '; } $html = '<div class="wrapper">' . $html . '</div>' ; return $html ; } endif ; // end of function_exists() ?> |
Usage in templates:
1 2 3 4 5 6 7 8 9 | <?php if ( function_exists( 'view_quotes_posts' ) ) { echo view_quotes_posts(); } ?> |
Show list of all not empty custom taxonomies (custom post type categories):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | <?php $tax_name = 'quote_category' ; $args = array ( 'hide_empty' => false, 'hierarchical' => false, 'parent' => 0 ); $taxonomies = get_terms( $tax_name , $args ); echo '<ul class="taxonomy">' ; foreach ( $taxonomies as $taxonomy ){ $link = get_term_link( $taxonomy , $tax_name ); echo '<li>' ; echo '<a href="' . $link . '">' ; echo $taxonomy ->name; echo '</a>' ; $sub_args = array ( 'hide_empty' => false, 'hierarchical' => false, 'parent' => 0 ); $sub_taxonomies = get_terms( $tax_name , $sub_args ); $children = get_term_children( $taxonomy ->term_id, $tax_name ); echo ' c= ' . count ( $children ); echo '</li>' ; } echo '</ul>' ; ?> |
List of custom post types (archive) can be viewed by such link: http://site.com/quote/
WordPress by default uses the archive template of your theme to display the custom post type archive page - archive.php.
If you want to create a custom archive page for your custom post type, then you would need to create a new file called archive-quote.php.
Example of archive-quote.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php get_header(); if (have_posts()) : while (have_posts()) : the_post(); the_title(); echo '<div class="entry-content">' ; the_content(); echo '</div>' ; endwhile ; endif ; get_footer(); ?> |
Great code and extremely useful to me! I'm trying to extend it a bit, by having a frontend quote entry form. My first thought was to replicate and then adapt the view_quotes_posts function at the very of the main file, so it can be inserted on template files. How would you do it?
How would you output this to an xml file?
You may check out the code of the Sitemap.xml plugin generator.
I think you may find solution there.
You forget to close php tag in the very end of first code section
Thank you for the feedback. I fixed it.
It is not necessary to close the final PHP tag. In fact, I would recommend not closing it unless you have to.
That way, a stray linebreak at the end of your source code won't cause the white screen of death.
Hi,
I am wondering how I can enable people to read my posts in their native language (Hindi). Is creating custom post type good idea? Will it enable me to post and get comments in the native language?
Do you know a better alternative?
At present I am using google translate box and I tell they can send comments to my email using gmail in their language.
Do you have better ideas to take this forward?
Dr. Ashok Koparday
Custom post types will not help you to force people to read posts in Hindi. Custom post types are needed to create completely new type of posts, like products or services with another structure than in default posts.
If you want for people to read posts on Hindi than just write your posts on Hindi and when Google will index that posts than Google will send users to your site who will search on Hindi.
If users will like the articles than they will return to you more frequently.
So the best advice to promote your native Hindi language for users is to write useful articles for users on that language. Nothing else will not help you with it.
Hi,
Thank you. I appreciate your taking time to opine.
I have begun writing posts in hindi. This pulls many native people.
Best,
Dr. Ashok Koparday
Where do we place the php code for register_post_type to create the custom post type? Is it its own file? If so, where does this file go? Do we add it to an existing file?
you should insert this code into functions.php of active theme or you may create a new plugin and insert code there
Just be weary that if you update the theme, the functions.php file will also be replaced by its original updated version.
For that you need to work on child theme
Just create a theme-child folder and place a style.css and functions.php and write all the relevant code inside it