Better Practice, Dec. 2018

(singke) #1
http://www.phparch.com \ December 2018 \ 5

Custom Post Types in WordPress

Next, create a function to add sub-menu pages to your
parent menu item as in Listing 3.
In the example above, custom post types of “Hiking Trails”
and “Biking Trails” are nested under a Trails parent menu
item (see Figure 2.) Trails don’t have a custom post type,
just a menu link. Note that for accessibility reasons, the WP
Admin will automatically repeat the parent menu item above
the child custom post type links in the secondary navigation.
The fifth argument in this add_menupage function is an
optional function call. You can use this function to create a
landing page for the parent link (in our example, Trails).
This simple function example adds some content to a page
in the WP Admin. Then we can add links to the WordPress
post edit pages of the associated custom post types that we
created earlier (Listing 4).
Then you add a call to your new function from your add

menu_page function’s fifth argument:


add_menu_page(
'Trails', 'Trails', 'edit_posts',
'mycpt_trails', 'mycpt_trails_page',
'dashicons-location-alt', 5
);

Now, if a site administrator clicks on
the “Trails” link, they will see links to our
Hiking Trails and Biking Trails custom post
type editing pages as shown in Figure 3.
Read more about adding to the WP
Admin menu^5.


Changing Permalinks for Custom Post Types
In the example above, we created two
custom post types that fall under a parent.
We can update our site’s permalinks, or
page URLs, to reflect that hiking is nested
inside of trails by editing the rewrite and
has_archive properties.

5 WP Admin menu:
https://phpa.me/wp-add-menu-page

Figure 1. Custom Post Type Menu Items

Listing 3


  1. if (!function_exists('mycpt_trails_menu')) {

  2. function mycpt_trails_menu() {

  3. add_menu_page(

  4. 'Trails', 'Trails', 'edit_posts', 'mycpt_trails',

  5. '', 'dashicons-location-alt', 5 );



  6. add_submenu_page(

  7. 'mycpt_trails', 'Hiking Trails', 'Hiking Trails',

  8. 'edit_posts', 'edit.php?post_type=wiverb_hike');



  9. add_submenu_page(

  10. 'mycpt_trails', 'Biking Trails', 'Biking Trails',

  11. 'edit_posts', 'edit.php?post_type=mycpt_bike');

  12. }

  13. add_action( 'admin_menu', 'mycpt_trails_menu' );

  14. }


Figure 2. Nested Menu with Page of Links

Listing 4


  1. if (!function_exists('mycpt_trails_page')) {

  2. function mycpt_trails_page() {

  3. if (!current_user_can( 'edit_posts')) {

  4. wp_die(

  5. __('You do not have sufficient permissions to access this page.')

  6. );

  7. }



  8. echo '
    ';

  9. echo '

    Trails

    Edit trails by selecting from the list below';


  10. echo '
      ';
    • echo '
    • <a href="'. home_url()

    • .'/wp-admin/edit.php?post_type=wiverb_hike">Hiking Trails
    • ';
    • echo '
    • <a href="'. home_url()

    • .'/wp-admin/edit.php?post_type=mycpt_bike">Biking Trails
    • ';
    • echo '
    ';

  11. echo '
';
  • }

  • }

  • Free download pdf