wordpress: how to add hierarchy to posts

后端 未结 8 1549
挽巷
挽巷 2020-12-30 16:01

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

8条回答
  •  隐瞒了意图╮
    2020-12-30 16:26

    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.

提交回复
热议问题