我创建WordPress的平台,一个网站,我希望能够张贴我自己的书文本。 所以,我要的是有某种层次的,我想补充一个帖子,然后添加儿童它(章节)。 我找到了这个:
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
,但没有效果。任何人都可以帮忙吗?
这里是我的解决方法。 这实现你想要什么,才能够设置后父母内建柱式后。 您可以通过向一个动作做到这一点registred_post_type
行动挂钩。 只需添加这主题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' );
}
可以有很多理由,为什么决策职位层级会有所帮助。 我使用的情况是,客户希望构建自己的(现有)的帖子成为问题,在孩子的职位是一个问题(父的帖子)的文章。
这很容易通过限制查询只显示职位,没有父母,用实现。
'post_parent' => 0,
在查询中的$ args。
WP 4.9。*
上述解决办法使它疯狂友好的URL。
我的解决方案,以增加层次到任何存在的职位类型:
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;
}
重新保存在/wp-admin/options-permalink.php WP设置
我来到这里寻找到实现:
- 添加页面属性post_type职位,以增加家长的帖子
- 能够添加页面模板post_type职位
- 如果能够得到post_type职位分层固定链接结构
我能够使用公认的答案来完成1&2,但不是3。
注意 :要充分获得2到工作中,你需要在你这样的页面模板的模板评论指定post_type:
<?php
/*
Template Name: Your Post Template Name
Template Post Type: post
*/
3,我发现,毁了我的post_type网页插件,这是一个很大的很可怕,没有维护的代码。
所以我写了一个解决方案来完成这一切,从借用这个答案 :
(测试与4.9.8)
<?php
add_action('registered_post_type', 'make_posts_hierarchical', 10, 2 );
// Runs after each post type is registered
function 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' );
}
/**
* Get parent post slug
*
* Helpful function to get the post name of a posts parent
*/
function get_parent_post_slug($post) {
if (!is_object($post) || !$post->post_parent) {
return false;
}
return get_post($post->post_parent)->post_name;
}
/**
*
* Edit View of Permalink
*
* This affects editing permalinks, and $permalink is an array [template, replacement]
* where replacement is the post_name and template has %postname% in it.
*
**/
add_filter('get_sample_permalink', function($permalink, $post_id, $title, $name, $post) {
if ($post->post_type != 'post' || !$post->post_parent) {
return $permalink;
}
// Deconstruct the permalink parts
$template_permalink = current($permalink);
$replacement_permalink = next($permalink);
// Find string
$postname_string = '/%postname%/';
// Get parent post
$parent_slug = get_parent_post_slug($post);
$altered_template_with_parent_slug = '/' . $parent_slug . $postname_string;
$new_template = str_replace($postname_string, $altered_template_with_parent_slug, $template_permalink);
$new_permalink = [$new_template, $replacement_permalink];
return $new_permalink;
}, 99, 5);
/**
* Alter the link to the post
*
* This affects get_permalink, the_permalink etc.
* This will be the target of the edit permalink link too.
*
* Note: only fires on "post" post types.
*/
add_filter('post_link', function($post_link, $post, $leavename){
if ($post->post_type != 'post' || !$post->post_parent) {
return $post_link;
}
$parent_slug = get_parent_post_slug($post);
$new_post_link = str_replace($post->post_name, $parent_slug . '/' . $post->post_name, $post_link);
return $new_post_link;
}, 99, 3);
/**
* Before getting posts
*
* Has to do with routing... adjusts the main query settings
*
*/
add_action('pre_get_posts', function($query){
global $wpdb, $wp_query;
$original_query = $query;
$uri = $_SERVER['REQUEST_URI'];
// Do not do this post check all the time
if ( $query->is_main_query() && !is_admin()) {
// get the post_name
$basename = basename($uri);
// find out if we have a post that matches this post_name
$test_query = sprintf("select * from $wpdb->posts where post_type = '%s' and post_name = '%s';", 'post', $basename);
$result = $wpdb->get_results($test_query);
// if no match, return default query, or if there's no parent post, this is not necessary
if (!($post = current($result)) || !$post->post_parent) {
return $original_query;
}
// get the parent slug
$parent_slug = get_parent_post_slug($post);
// concat the parent slug with the post_name to get most of the url
$hierarchal_slug = $parent_slug . '/' . $post->post_name;
// if the concat of parent-slug/post-name is not in the uri, this is not the right post.
if (!stristr($uri, $hierarchal_slug)) {
return $original_query;
}
// pretty high confidence that we need to override the query.
$query->query_vars['post_type'] = ['post'];
$query->is_home = false;
$query->is_page = true;
$query->is_single = true;
$query->queried_object_id = $post->ID;
$query->set('page_id', $post->ID);
return $query;
}
}, 1);
您可以将此保存到文件custom-posts-hierarchy.php
在你的主题在你的functions.php文件,包括它,也可以添加到顶部:
/*
Plugin Name: Custom Posts Hierarchy
Plugin URI:
Description: Add page attributes to posts and support hiearchichal
Author: Angela Murrell
Version:
Author URI:
*/
拖放到你的插件文件夹。 祝好运!
在WordPress的帖子应该是典型的时间博客文章。 页面静态内容所做的,他们可以在一个层次结构来组织开箱。
对于任何一个页面,你可以选择一个父页面。 通过这种方式,您可以创建多个子嵌套层次。 听起来像是你需要什么。
检查WordPress的文档的详细信息。
如果你有一个深刻的,复杂的树状结构,插件可以帮助你管理它,像WordPress的页面树 。 它提供了比WordPress默认页,列出一个更好的界面。
最好的解决办法是创建自定义分类[1]: http://codex.wordpress.org/Function_Reference/register_taxonomy创造主段塞-书籍或别的东西。
使用插件,像CPT UI
,你可以创建一个自定义后类型,并将其设置有分层树。
然后,只需检查后类型page-attribute
设置为这个自定义后的类型和纱,你的帖子现在有层次的状态。
https://wordpress.org/plugins/custom-post-type-ui/
存在的插件,这对于创建层次post
:
https://wordpress.org/plugins/add-hierarchy-parent-to-post/
这不是一个更好的选择?
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'),
)
);
我已经添加了:
'分层'=>真,
和它的作品。