Uvod
WordPress custom post type je svaki tip koji nije neki od 5 defaultnih post tipova:
- Post (Post Type: ‘post’)
- Page (Post Type: ‘page’)
- Attachment (Post Type: ‘attachment’)
- Revision (Post Type: ‘revision’)
- Navigation menu (Post Type: ‘nav_menu_item’)
Da bi napravili novi wordpress tip potrebno je da napravimo custom funkciju koja će da aktivira opciju u admin panelu sa našim specifičnim zahtevima. Celokupna custom funkcija se u suštini zasniva na register_post_type() funkciji.
Funkcija register_post_type()
1 |
register_post_type( $post_type, $args ) |
$post_type
je ime koje koristi wordpress tzv. machine name, ne sme sadržati velika slova ili spaces
$args je niz argumenata koji definišu novi post type.
Neki argumenti koji se često koriste su:
- label je naziv (u množini) koji se pojavljuje na admin stranici spremno za prevodjenje
- labels je niz su nazivi koji se koriste u toj vrsti custom post-a
- ‘name’ – general name for the post type, usually plural. The same as, and overridden by $post_type_object->label
- ‘singular_name’ – name for one object of this post type. Defaults to value of ‘name’.
- ‘menu_name’ – the menu name text. This string is the name to give menu items. Defaults to a value of ‘name’.
- ‘name_admin_bar’ – name given for the “Add New” dropdown on admin bar. Defaults to ‘singular_name’ if it exists, ‘name’ otherwise.
- ‘all_items’ – the all items text used in the menu. Default is the value of ‘name’.
- ‘add_new’ – the add new text. The default is “Add New” for both hierarchical and non-hierarchical post types. When internationalizing this string, please use a gettext context matching your post type. Example: _x('Add New', 'product');
- ‘add_new_item’ – the add new item text. Default is Add New Post/Add New Page
- ‘edit_item’ – the edit item text. In the UI, this label is used as the main header on the post’s editing panel. The default is “Edit Post” for non-hierarchical and “Edit Page” for hierarchical post types.
- ‘new_item’ – the new item text. Default is “New Post” for non-hierarchical and “New Page” for hierarchical post types.
- ‘view_item’ – the view item text. Default is View Post/View Page
- ‘search_items’ – the search items text. Default is Search Posts/Search Pages
- ‘not_found’ – the not found text. Default is No posts found/No pages found
- ‘not_found_in_trash’ – the not found in trash text. Default is No posts found in Trash/No pages found in Trash.
- ‘parent_item_colon’ – the parent text. This string is used only in hierarchical post types. Default is “Parent Page”.
- description opis
- public je opcija koja kontroliše vidljivost autorima i čitaocima sajta, i dirkektno utiče na druge parametre exclude_from_search, publicly_queryable, show_in_nav_menus, i show_ui jer daje njihovu default vrednost.
To je boolen-ova vrednost pa može biti:- ‘true’ The built-in types attachment, page, and post are similar to this.
- ‘false’ The built-in types nav_menu_item and revision are similar to this. Best used if you’ll provide your own editing and viewing interfaces (or none at all).
- menu_position
- 5 – below Posts
- 10 – below Media
- 15 – below Links
- 20 – below Pages
- 25 – below comments
- 60 – below first separator
- 65 – below Plugins
- 70 – below Users
- 75 – below Tools
- 80 – below Settings
- 100 – below second separator
- menu_icon može biti:
- url do ikone npr. ‘get_template_directory_uri().”/images/cutom-posttype-icon.png”‘
- ime ikone iz iconfont-a npr. ‘dashicons-video-alt’
- supports je boolen-ov niz
- ‘title’
- ‘editor’ (content)
- ‘author’
- ‘thumbnail’ (featured image, current theme must also support post-thumbnails)
- ‘excerpt’
- ‘trackbacks’
- ‘custom-fields’
- ‘comments’ (also will see comment count balloon on edit screen)
- ‘revisions’ (will store revisions)
- ‘page-attributes’ (menu order, hierarchical must be true to show Parent option)
- ‘post-formats’ add post formats
Proveriti da li su prethodno obezbedjene podrške za osobinu na nivo theme pre nego što uradimo podršku na nivou custom post tipa!
- has_archive je boolen-ova vrednost koja aktivira post type archives, pa će se koristiti $post_type kao archive slug po defasult-u.
Custom post type kroz primere
Osnovno jednostavno korišćenje:
1 2 3 4 5 6 7 8 |
function codex_custom_init() { $args = array( 'public' => true, 'label' => 'Books' ); register_post_type( 'book', $args ); } add_action( 'init', 'codex_custom_init' ); |
ili kompletan primer:
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 |
function codex_book_init() { $labels = array( 'name' => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ), 'singular_name' => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ), 'menu_name' => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ), 'name_admin_bar' => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ), 'add_new' => _x( 'Add New', 'book', 'your-plugin-textdomain' ), 'add_new_item' => __( 'Add New Book', 'your-plugin-textdomain' ), 'new_item' => __( 'New Book', 'your-plugin-textdomain' ), 'edit_item' => __( 'Edit Book', 'your-plugin-textdomain' ), 'view_item' => __( 'View Book', 'your-plugin-textdomain' ), 'all_items' => __( 'All Books', 'your-plugin-textdomain' ), 'search_items' => __( 'Search Books', 'your-plugin-textdomain' ), 'parent_item_colon' => __( 'Parent Books:', 'your-plugin-textdomain' ), 'not_found' => __( 'No books found.', 'your-plugin-textdomain' ), 'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' ) ); $args = array( 'labels' => $labels, 'description' => __( 'Description.', 'your-plugin-textdomain' ), 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'book' ), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => null, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ) ); register_post_type( 'book', $args ); } add_action( 'init', 'codex_book_init' ); |
NAPOMENA:
Ukoliko se nakon registrovanja post tipa pojavi greška tipa 404, potrebno je u Settings/Permalinks uraditi Save Changes.
Adaptiranje teksta u admin interfejsu
Promena default poruka koje wordpress aktivira nakon nekih radnji korisnika se lako može promeniti koristeći post_updated_message udice kao u sledećem primeru.
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 |
function rw_post_updated_messages( $messages ) { $post = get_post(); $post_type = get_post_type( $post ); $post_type_object = get_post_type_object( $post_type ); $messages['my-post-type'] = array( 0 => '', // Unused. Messages start at index 1. 1 => __( 'My Post Type updated.' ), 2 => __( 'Custom field updated.' ), 3 => __( 'Custom field deleted.'), 4 => __( 'My Post Type updated.' ), /* translators: %s: date and time of the revision */ 5 => isset( $_GET['revision'] ) ? sprintf( __( 'My Post Type restored to revision from %s' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, 6 => __( 'My Post Type published.' ), 7 => __( 'My Post Type saved.' ), 8 => __( 'My Post Type submitted.' ), 9 => sprintf( __( 'My Post Type scheduled for: <strong>%1$s</strong>.' ), // translators: Publish box date format, see http://php.net/date date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ) ), 10 => __( 'My Post Type draft updated.' ) ); //return the new messaging return $messages; } add_filter( 'post_updated_messages', 'rw_post_updated_messages' ); |
Ili slično:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
function my_updated_messages( $messages ) { global $post, $post_ID; $messages['product'] = array( 0 => '', 1 => sprintf( __('Product updated. View product'), esc_url( get_permalink($post_ID) ) ), 2 => __('Custom field updated.'), 3 => __('Custom field deleted.'), 4 => __('Product updated.'), 5 => isset($_GET['revision']) ? sprintf( __('Product restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, 6 => sprintf( __('Product published. View product'), esc_url( get_permalink($post_ID) ) ), 7 => __('Product saved.'), 8 => sprintf( __('Product submitted. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), 9 => sprintf( __('Product scheduled for: <strong>%1$s</strong>. Preview product'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ), 10 => sprintf( __('Product draft updated. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), ); return $messages; } add_filter( 'post_updated_messages', 'my_updated_messages' ); |
Pomoćni tekst za objašnjenja
Da bi se olakšalo korišćenje custom post tipova može da se napravi help tab koji bi se pojavljivao u admin stranici na dva “mesta”
- kada pregledamo listu sa svim custom post tipovima
- kada editujemo jedan custom post tip
Ovakav help meni se dobija ugradjivanjem custom funkcije u functions.php a koja se aktivira sa akcionom udicom contextual_help. Na sledećem primeru je kod za help meni na admin strani za custom post type movie:
1 2 3 4 5 6 7 8 9 10 11 12 |
function my_contextual_help( $contextual_help, $screen_id, $screen ) { if ( 'movie' == $screen->id ) { $contextual_help = '<h2>Podaci o filmu</h2> <p>Ubacite naslov filma i sadrzaj.</p>'; } elseif ( 'edit-'movie'' == $screen->id ) { $contextual_help = '<h2>Pregledajte spisak svih vasih filmova</h2> <p>Na ovom delu mozete da izvrsite editovanje svi filmova odjednom!</p>'; } return $contextual_help; } add_action( 'contextual_help', 'my_contextual_help', 10, 3 ); |
U prethodnom primeru help tab sa svojim tekstom za pomoć će se pojaviti u dve različite prilike i sadržaće različite prateće tekstove. Dok smo u Admin stranici i kada:
- pregledamo svie postove ovoga tipa
(deo u kodu kada je korišćeno ‘movie’ == $screen->id) - editujemo pojedinačni post
(deo u kodu kada je korišćeno ‘edit-‘movie” == $screen->id )
Ukoliko želimo da imamo više tabova i želimo da bude pripemljeno za prevodjenje koristićemo kod kao u sledećem primeru:
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 |
function my_contextual_help2( $contextual_help, $screen_id, $screen ) { if ( 'movie' == $screen->id ) { $screen->add_help_tab( array( 'id' => 'tab1', 'title' => __('Prvi help tab'), 'content' => '<div>' . __('<h2>Kako popuniti sadrzaj?</h2> <p>Ubacite naslov filma i sadrzaj.</p> ') . '</div>', )); $screen->add_help_tab( array( 'id' => 'tab2', 'title' => __('Drugi help tab'), 'content' => '<div>' . __( '<p>Pellentesque habitant morbi tristique senectus.</p>' ) . '</div>', )); $screen->add_help_tab( array( 'id' => 'tab3', 'title' => __('Treci help tab'), 'content' => '<div>' . __( '<p>Pellentesque habitant morbi tristique senectus.</p>' ) . '</div>', )); } elseif ( 'edit-movie' == $screen->id ) { $screen->add_help_tab( array( 'id' => 'tab1', 'title' => __('Prvi help tab'), 'content' => '<div>' . __(' <h2>Kako dodati novi film?</h2> <p>Kliknite na dugme "Dodaj Film"</p> ') .'</div>', )); } } add_action( 'contextual_help', 'my_contextual_help2', 10, 3 ); |
Šabloni za Custom Post Type
Prikazivanje jednog člana
Za prikazivanje jedanog člana posebnog tipa se koristi šablon single.php, ali ako postoji potreba da se prikazuje drugačije od običnog single post-a moramo napraviti poseban šablon sa imenom single-NazivTipa.php.
Prikazivanje liste
Za prikazivanje listi posebnih tipova se po default-u koristi standardni šablon archive.php a ako želimo da to bude specifičnije onda koristimo šablon pod nazivom archive-NazivTipa.php
NAPOMENA:
Da bi se u navigaciji napravio meni-link koji otvara listu sa svim postovima tipa custom type, koristićemo opciju custom link gde ćemo za URL staviti http://domen.com/NazivCustomPostType.
Objašnjenje na primerima
Ukoliko je npr. custom post tip “filmovi”, sledi da kada želimo prikaz jednog filma (npr. “Terminator”) WP će koristiti šablon single-filmovi.php ali ako želimo da prikažemo sve filmove unete kroz custom post tip koristi se šablon archive-filmovi.php.
Prikazivanje Custom post type
Prikazivanje zajedno sa standardnim postovima
Jedna od prednosti custom post tipova je što su odvojeni od “običnih” postova tj. prikazuju se na blog stranici zajedno sa “običnim” postovima. Ali ukoliko nam je ipak želja da ih spojimo moramo da koristimo sledeći kod:
1 2 3 4 5 6 |
function add_my_post_types_to_query( $query ) { if ( is_home() && $query->is_main_query() ) $query->set( 'post_type', array( 'post', 'movie' ) ); return $query; } add_action( 'pre_get_posts', 'add_my_post_types_to_query' ); |
U prethodnom kodu se koristi akciona udica pre_get_posts koja se poziva posle stvaranja $query objekta ali pre nego što je startovan u petlji. Stoga će se u trenutku kada deluje naša udica aktivirati kod koji objektu $query dodaje novi custom post tip “movie” zajedno sa standardnim post-ovima.
Pojavljivanje u nekom šablonu
Ukoliko želimo da prikažemo listu našeg custom post tipa u okviru nekog šablona, onda se koristi custom loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php $args = array( 'post_type' => 'movies', 'posts_per_page' => 10 ); $the_query = new WP_Query( $args ); ?> <?php if ( $the_query->have_posts() ) : ?> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <h2><?php the_title(); ?></h2> <div class="entry-content"> <?php the_content(); ?> </div> <?php endwhile?> <?php wp_reset_postdata(); ?> <?php else: ?> <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p> <?php endif; ?> |
Prikazivanje u widget area
Da vi prikazali listu skorašnjih custom post tipova koristićemo plugin “Ultimate Post Widget” koji nakon aktiviranja i stavljanja kao widget-a u sidebar omogućava da vidite skorašnje postove bilo kog tipa.