我试图通过将CPT“书名”的永久链接设置为默认页面“类型名”的子级来更改它。由于各种原因,我不使用元框,因此唯一的解决方案是ACF。
因此在CPT中,我将为父页面选择器-“流派名称”。
问题是ACF返回wp_post对象。我该如何处理才能在永久链接上进行更改并使它成为CPT的父级?
我遵循了使用元框的教程,但是现在迷路了。...
尝试了很多事情,这就是我现在所拥有的:
function rewrite_url_for_books() {
add_rewrite_tag('%book%', '([^/]+)', 'book=');
add_permastruct('book', '/%genre%/%book%', false);
add_rewrite_rule('^([^/]+/([^/]+/?', 'index.php?book=$matches[2]', 'top');
}
add_action( 'init', 'rewrite_url_for_book' );
function permalink_for_book($permalink, $post, $leavename) {
$post_id = $post->ID;
if($post->post_type != 'book' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))
return $permalink;
$genre = get_field('genre_field');
setup_postdata($genre);
$parent = $post->post_parent;
$parent_post = get_post($parent);
$permalink = str_replace('%genre%', $parent_post->post_name, $permalink);
return $permalink;
}
add_filter('post_type_link', 'permalink_for_book', 10, 3);
这是一个wp_post对象
WP_Post (object) => [
// Default data returned by WP_Post
ID => int,
post_author => string,
post_name => string,
post_type => string,
post_title => string,
post_date => string,
post_date_gmt => string,
post_content => string,
post_excerpt => string,
post_status => string,
comment_status => string,
ping_status => string,
post_password => string,
post_parent => int,
post_modified => string,
post_modified_gmt => string,
comment_count => string,
menu_order => string,
// Additional data I want to add
extra_data_1 => array,
more_data_key => string,
another_added => string
]