I am attempting to create some posts to display on a CPT taxonomy page which follow the same URL structure.
I have a post type of animal and taxonomy of animal-category, the generated URLs for these pages are:
I then want to create a standard page (not part of this post type) which will have the URL:
Structurally the page is unrelated to the animal posts and could be a standard page or in a post type, it doesn't matter.
After research and previous knowledge I cannot find a rewrite rule to force a page to use domain.com/animal-category before the page-name is added - as it has already been rewritten for the taxonomy.
Maybe there is a completely different solution to achieve having a page (with this URL structure) on a taxonomy listing?
You can totally do that :)
1) In your theme create the animal-taxonomy template and name it taxonomy-animal-category.php
2) In the loop apply a condition to get the displayed item (queried object) as follows:
<?php $queried_slug = get_queried_object()->slug;
if($queried_slug=="page-name"){ //IT IS THE PAGE we have
}else{ ?>
<?php if ( have_posts() ) : ?>
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
// End the loop.
endwhile;
else :
echo "NO animals found :)";
endif;
<?php } ?>
I will take your desired URL to be as following,
http://example.com/%taxonomy%/page-name
Please have a look at the filter _get_page_link. Though this filter is not intended to use with theme or plugin development, you can use it to achieve your task.
So use it as follows,
add_action( 'init', array( $this, 'custom_rewrite_rules' ), 1, 0 );
/*
* Rewrite page urls
* adding prefix to page urls
*/
function custom_rewrite_rules()
{
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag( "%taxonomy%", '([^/]+)', 'taxonomy=' );
$wp_rewrite->page_structure = $wp_rewrite->root. '%taxonomy%/%pagename%';
}
Above function will create the desired url. Now use the filter to change %taxonomy%
to your value.
add_filter( '_get_page_link', 'custom_taxonomy_page_link', 10, 2 );
function custom_taxonomy_page_link( $link, $post_id ) {
$link = str_replace('%taxonomy%', 'animal-category', $link);
return $link;
}
This is not tested properly, but with little research it might help you.