wordpress:如何向帖子添加层次结构 [英] wordpress: how to add hierarchy to posts

查看:99
本文介绍了wordpress:如何向帖子添加层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在wordpress平台上创建一个网站,希望能够发布自己的书本文字.因此,我想要的是具有某种层次结构,在该层次结构中,我将添加一个帖子,然后向其添加子代(各章).我发现了:

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' ),
    ) );

并尝试制作'hierarchical"=>true,但没有效果.有人可以帮忙吗?

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

推荐答案

这是我的解决方法.这正是您想要的,能够为内置帖子类型的帖子设置父帖子.您可以通过将动作添加到registred_post_type动作钩子来实现.只需将其添加到主题的functions.php中即可.

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,

在您的查询$ args中.

in your query $args.

这篇关于wordpress:如何向帖子添加层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆