I have set up a custom post type (team) and a custom taxonomy (team_tax) for this post type, both with the same slug. Down to the core:
register_post_type( 'team', array(
'labels' => array( 'Name' => 'Pages' ),
'hierarchical' => false,
'rewrite' => array( 'slug' => 'team', 'with_front' => true ),
'has_archive' => 'teams'
);
);
register_taxonomy( 'team_tax', 'team', array(
'hierarchical' => true,
'rewrite' => array( 'slug' => 'team', 'with_front' => true )
);
);
The taxonomy will be the team names. The post type will be the pages for these teams.
Everything works fine by default, but I want to display these as a specific hierarchical url like so:
/team/team_tax/post
To get the team_tax permalink to behave like this I used the following plugin: WP Better Permalinks. This adds the team_tax to the url.
Now eventually I want to create a structure like so:
/team/example-team-1/info
/team/example-team-1/members
/team/example-team-1/schedule
/team/example-team-2/info
/team/example-team-2/members
/team/example-team-2/schedule
etc.
Now the problem is, Wordpress doesn't recognize the pages as a child of this taxonomy. So it will start adding -2, -3 etc. to the slugs, so the following happens instead:
/team/example-team-1/info
/team/example-team-2/info-2
/team/example-team-3/info-3
I have tried to use a filter on 'wp_unique_post_slug' to ignore the newly made slug and use the default one instead. But then the page will load the content from all these 3 pages. Also a bit more simplified for demonstration:
function team_non_unique_post_slug( $slug, $original_slug ) {
if( $post_type === 'team' ) {
return $original_slug;
}
}
add_filter( 'wp_unique_post_slug', 'team_non_unique_post_slug' );
So my question is, is there a way for me to achieve this without any conflicts? Basically it should be behaving as the hierachical structure of default Wordpress pages, but with a taxonomy instead. I'm also open for better suggestions/solutions!
Edit:
function alter_query_for_duplicates( $query ) {
if ( is_admin() ) {
return;
}
if ( is_singular() ) {
if ( $query->is_main_query() ) {
if ( in_array ( $query->get('post_type'), array('team') ) ) {
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$split_url = explode( "/", $actual_link );
$split_url = array_filter( $split_url );
$url_tax = $split_url[count($split_url)-1];
$url_page = end($split_url);
$args = array(
'posts_per_page' => -1,
'post_type' => 'team',
'tax_query' => array(
array(
'taxonomy' => 'team_tax',
'field' => 'slug',
'terms' => $url_tax,
'operator' => 'IN'
)
),
// 'name' => $url_page (doesn't work because of duplicate slugs)
);
$posts_array = get_posts( $args );
$current_post_key = search_for_name($url_page, $posts_array);
$query->set( 'page_id', $posts_array[$current_post_key]->ID );
}
}
}
return $query;
}
add_filter( 'pre_get_posts', 'alter_query_for_duplicates' );
function search_for_name($name, $array) {
foreach ($array as $key => $val) {
if ($val->post_name === $name) {
return $key;
}
}
return null;
}
I currently added this extremely hacky way to get the content based on the current page url. It will get all posts from the specified taxonomy, and then search for the page slug in these posts and set the pre_get_posts query to only search for this page's ID.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745611610a4635978.html
评论列表(0条)