Home > OS >  How do you add the ability to add a parent page to WordPress posts?
How do you add the ability to add a parent page to WordPress posts?

Time:08-26

Would something like this work? Am I missing anything?

function page_attributes_for_posts() {
    register_post_type('job-bank-posts',
        $args = array(
            'hierarchical' => true,
            'supports' => 'page-attributes'
        );
    );
};
add_action ('init', 'page_attributes_for_posts');

This is one of the sources I used: https://developer.wordpress.org/reference/functions/register_post_type/

CodePudding user response:

Maybe that will help, they added the parent feature for post https://make.wordpress.org/core/2021/02/10/introducing-new-post-parent-related-functions-in-wordpress-5-7/

CodePudding user response:

Maybe a complete example could be helpful:

 function create_job_bank_posts() {
  register_post_type( 'book',
    array(
      'labels' => array(
        'name' => __( 'Job Bank Posts' ),
        'singular_name' => __( 'Job Bank Post' ),
        'add_new' => _x('Add Job Bank Post', 'Job Bank Post'),
        'add_new_item' => __('Add Job Bank Post'),
        'edit_item' => __('Edit Job Bank Post'),
        'new_item' => __('New Job Bank Post'),
        'view_item' => __('View Job Bank Post'),
        'search_items' => __('Search Job Bank Post'),
        'not_found_in_trash' => __('Not found in trash'),
      ),
      'public' => true,
      'menu_icon' => 'your-dashicon',
      'rewrite' => array( 'slug' => 'job_bank_posts', 'with_front' => true ),
      'menu_position' => 3,
      'hierarchical' => true,
      'supports' => array(
                'title',
                'page-attributes',
                'thumbnail',
                'editor',
                'excerpt',
                'author',
                'comments',
                'custom-fields',
            ),
    )
  );
}
add_action( 'init', 'create_job_bank_posts' );

CodePudding user response:

Forgive me if this isn't quite what you are after. OK, I managed to achieve something like this purely using WordPress's built-in Permalink Settings. The other issue with this quick method is that you will no longer have the use of categories. But its works well for us.

  1. go to wp-admin/options-permalink.php

  2. Select: Custom Structure

  3. you'll want a structure like the following: /about/

  • Related