wordpress: how to add hierarchy to posts

2019-04-13 12:04发布

I am creating a web-site on wordpress platform where I want to be able to post my own book texts. So what I want is to have a some kind of hierarchy where I would add a post and then add children to it (chapters). I found this:

register_post_type( 'post', array(
        'labels' => array(
            'name_admin_bar' => _x( 'Post', 'add new on admin bar' ),
        ),
        'public'  => true,
        '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
        '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
        'capability_type' => 'post',
        'map_meta_cap' => true,
        'hierarchical' => false,
        'rewrite' => false,
        'query_var' => false,
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
    ) );

and tried to make the 'hierarchical"=>true, but there was no effect. Can anyone help?

8条回答
该账号已被封号
2楼-- · 2019-04-13 12:42

There exists plugin, which creates hierarchy for post:

https://wordpress.org/plugins/add-hierarchy-parent-to-post/

查看更多
疯言疯语
3楼-- · 2019-04-13 12:47

WP 4.9.*

Workaround above makes it crazy with Friendly URLs.

My solution to add hierarchy to any existent post type:

add_filter( 'register_post_type_args', 'add_hierarchy_support', 10, 2 );
function add_hierarchy_support( $args, $post_type ){

    if ($post_type === 'post') { // <-- enter desired post type here

        $args['hierarchical'] = true;
        $args['supports'] = array_merge($args['supports'], array ('page-attributes') );
    }

    return $args;
}

Resave wp settings at /wp-admin/options-permalink.php

查看更多
相关推荐>>
4楼-- · 2019-04-13 12:51

Isn't this a better option?

register_post_type( 'MYPOSTTYPE',
    array(
        'labels' => array(
            'name' => __( 'MYPOSTTYPE' ),
            'singular_name' => __( 'MYPOSTTYPE' )
        ),
        'supports' => array('title', 'editor', 'page-attributes'),
        'public' => true,
        'has_archive' => true,
        'hierarchical' => true,
        'rewrite' => array('slug' => 'MYPOSTTYPE'),
    )
);

I have added:

'hierarchical' => true,

and it works.

查看更多
Anthone
5楼-- · 2019-04-13 12:53

Here is my workaround. This achieves exactly what you want, to be able to set post parents for the builtin post type post. You can achieve this by adding an action to the registred_post_type action hook. Just add this to your theme's functions.php.

add_action('registered_post_type', 'igy2411_make_posts_hierarchical', 10, 2 );

// Runs after each post type is registered
function igy2411_make_posts_hierarchical($post_type, $pto){

    // Return, if not post type posts
    if ($post_type != 'post') return;

    // access $wp_post_types global variable
    global $wp_post_types;

    // Set post type "post" to be hierarchical
    $wp_post_types['post']->hierarchical = 1;

    // Add page attributes to post backend
    // This adds the box to set up parent and menu order on edit posts.
    add_post_type_support( 'post', 'page-attributes' );

}

There can be dozens of reasons why making posts hierarchical can be helpful. My use case is that the client wanted to structure their (already existing) posts into issues, where child posts are articles of one issue (parent posts).

This is easily achieved by limiting the query to only show posts that have no parents, using.

 'post_parent' => 0,

in your query $args.

查看更多
Deceive 欺骗
6楼-- · 2019-04-13 12:55

Posts in Wordpress are supposed to be typical chronological Blog Posts. Pages are made for static content, they can be organised in a hierarchical structure out of the box.

For any Page, you can select a parent page. This way, you can create nested hierarchies with multiple children. Sounds like what you need.

Check the Wordpress Documentation for details.

If you have a deep, complicated tree structure, a plugin might help you manage it, like Wordpress Page Tree. It provides a better interface than the default Wordpress Page listing.

查看更多
倾城 Initia
7楼-- · 2019-04-13 13:04

Using a plugins like CPT UI, you can create a custom post type and set it to have hierarchical tree.

Then just check that the post type page-attribute is set for this custom post type and voile, your posts now have hierarchical states.

https://wordpress.org/plugins/custom-post-type-ui/

查看更多
登录 后发表回答