我想在自定义帖子的存档页面上显示帖子-gallery
基于author_id
i、 e.theauthor_id
作为permalink变量传递。存档页面上显示的帖子将仅是作者发布的帖子author_id
已通过。
我的自定义帖子gallery
存档页面具有urlhttp://localhost/?post_type=gallery
在post\\u类型中发布的所有帖子-gallery
已列出。
function my_custom_gallery() {
$gallery_labels = array(
\'name\' => _x(\'Gallery\', \'post type general name\'),
\'singular_name\' => _x(\'Gallery\', \'post type singular name\'),
\'add_new\' => _x(\'Add New\', \'gallery\'),
\'add_new_item\' => __("Add New Gallery"),
\'edit_item\' => __("Edit Gallery"),
\'new_item\' => __("New Gallery"),
\'view_item\' => __("View Gallery"),
\'search_items\' => __("Search Gallery"),
\'not_found\' => __(\'No galleries found\'),
\'not_found_in_trash\' => __(\'No galleries found in Trash\'),
\'parent_item_colon\' => \'\'
);
$gallery_args = array(
\'labels\' => $gallery_labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => false,
\'hierarchical\' => false,
\'has_archive\' => true,
\'menu_position\' => null,
\'capability_type\' => \'post\',
\'supports\' => array(\'title\'),
\'show_ui\' => true,
\'query_var\' => true,
\'menu_position\' => 4,
\'menu_icon\' => \'dashicons-images-alt2\'
);
register_post_type(\'gallery\', $gallery_args);
}
add_action( \'init\', \'my_custom_gallery\' );
我使用以下函数引入了一个自定义查询变量
function add_custom_query_var( $vars ){
$vars[] = "custom_user_id";
return $vars;
}
add_filter( \'query_vars\', \'add_custom_query_var\' );
我已经重写了URL结构,以包含
custom_user_id
function add_rewrite_rules($aRules) {
$aNewRules = array(\'/gallery/user/([^/]+)?$\' => \'index.php?post_type=gallery&custom_user_id=$matches[1]\');
$aRules = $aNewRules + $aRules;
return $aRules;
}
add_filter(\'rewrite_rules_array\', \'add_rewrite_rules\');
function rewrite_flush(){
global $wp_rewrite;
$gallery_structure = \'/gallery/%user_id%/%gallery%\';
$wp_rewrite->add_rewrite_tag("%gallery%", \'([^/]+)\', "gallery=");
$wp_rewrite->add_permastruct(\'gallery\', $gallery_structure, false);
$wp_rewrite->flush_rules();
}
add_action(\'init\',\'rewrite_flush\');
function tdd_permalinks($permalink, $post, $leavename){
$no_data = get_the_author_meta(\'ID\');
if($post->post_type != \'gallery\' || empty($permalink) || in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\'))) return $permalink;
$var1 = sanitize_title($no_data);
$permalink = str_replace(\'%custom_user_id%\', $var1, $permalink);
return $permalink;
}
add_filter(\'post_type_link\', \'tdd_permalinks\', 10, 3);
我使用检索自定义查询变量
$query_author_id = get_query_var(custom_user_id)
并希望显示作者具有
author_id
-
$query_author_id
在存档页上。