我已创建自定义用户元=shop_name
在用户配置文件和自定义帖子类型中gallery
帖子的默认URL为http://localhost/gallery/%gallery%
我使用下面给出的代码将其修改为下面给定的URL。
我希望我的永久链接以http://localhost/%shop_name%/gallery/%gallery%
.
每个用户都有一个自定义user\\u meta=shop_name
.
因此,如果我的用户shop_name="shop3"
还有一个画廊title="Washing Machine"
那么我的用户URL应该是http://localhost/shop3/gallery/mashing-machine
When i go to http://localhost/shop3/gallery/
I want all the galleries of shop3
should be listed. 而是显示索引页。How can I make it possible?
下面是我为创建自定义url编写的代码,但我想将其扩展到上述问题。
add_action(\'init\', \'tdd_add_rewrite_rules\');
function tdd_add_rewrite_rules(){
// Register custom rewrite rules
global $wp_rewrite;
//$wp_rewrite->add_rewrite_tag(\'%gallery%\', \'([^/]+)\', \'gallery=\');
$wp_rewrite->add_rewrite_tag(\'%shop_name%\', \'([^/]+)\', \'shop_name=\');
$wp_rewrite->add_permastruct(\'gallery\', \'/%shop_name%/gallery/%gallery%\', false);
}
add_filter(\'post_type_link\', \'tdd_permalinks\', 10, 3);
function tdd_permalinks($permalink, $post, $leavename){
$no_data = get_the_author_meta(\'ID\');;
$post_id = $post->ID;
if($post->post_type != \'gallery\' || empty($permalink) || in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\'))) return $permalink;
// $var1 = get_post_meta($post_id, \'posts_solicitorspeciality\', true);
$var1 = get_the_author_meta(\'shop_name\');
$var1 = sanitize_title($var1);
if(!$var1) { $var1 = $no_data; }
$permalink = str_replace(\'%shop_name%\', $var1, $permalink);
// $permalink = str_replace(\'%post_id%\', $post_id, $permalink);
return $permalink;
}
这是用户元-
%shop_name%
当查看作者发布的帖子时,我想将其加载到URL中,该帖子的店名为-
fila
. 我已使用ACF修改了用户配置文件设置,以开发此自定义用户设置。
默认的Permalink结构。
Cusotm post-Gallery
由用户以倾斜方式发布-gallery 1
这个URL
自定义帖子的gallery
具有默认permalink结构,即。http://localhost/gallery/gallery-1/
这个Custom URL
自定义帖子的gallery
使用上述代码定制permalink结构,即。http://localhost/fila/gallery/gallery-1/
当我转到URL时http://localhost/fila/gallery/
我想要所有的galleries 发布人author 店铺名称=fila
要显示,将显示主页。
类似地,当我转到URL时http://localhost/fila/
我想要information 属于shop fila 发布人author 要显示,将显示主页。
最合适的回答,由SO网友:Shahrukh Khan 整理而成
我使用以下函数来解决这个问题。
//This adds a custom query variable to the permalink
function add_custom_query_var( $vars ){
$vars[] = "shop_name";
return $vars;
}
add_filter( \'query_vars\', \'add_custom_query_var\' );
function add_rewrite_rules($aRules) {
$aNewRules = array(\'shop/([^/]+)/?$\' => \'index.php?pagename=shop&shop_name=$matches[1]\');
$aNewRules2 = array(\'shop/([^/]+)/gallery/?$\' => \'index.php?post_type=gallery\');
$aRules = $aNewRules + $aNewRules2 + $aRules;
return $aRules;
}
add_filter(\'rewrite_rules_array\', \'add_rewrite_rules\');
//Here we create a custom permalink structure and replace the shop_name with custom user feild value
function tdd_permalinks($permalink, $post, $leavename){
$no_data = get_the_author_meta(\'ID\');;
$post_id = $post->ID;
if($post->post_type != \'gallery\' || empty($permalink) || in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\'))) return $permalink;
$var1 = get_the_author_meta(\'shop_name\');
$var1 = sanitize_title($var1);
if(!$var1) { $var1 = $no_data; }
$permalink = str_replace(\'%shop_name%\', $var1, $permalink);
return $permalink;
}
add_filter(\'post_type_link\', \'tdd_permalinks\', 10, 3);
function rewrite_flush(){
global $wp_rewrite;
$gallery_structure = \'/shop/%shop_name%/gallery/%gallery%\';
$wp_rewrite->add_rewrite_tag("%gallery%", \'([^/]+)\', "gallery=");
$wp_rewrite->add_rewrite_tag("%shop_name%", \'([^/]+)\', "shop_name=");
$wp_rewrite->add_permastruct(\'gallery\', $gallery_structure, false);
$wp_rewrite->flush_rules();
}
add_action(\'init\',\'rewrite_flush\');