- This topic has 6 replies, 2 voices, and was last updated 3 years ago by David.
-
AuthorPosts
-
December 6, 2019 at 12:28 am #1095460Paul
Looking to hard code links to my custom category pages and avoid the hop with a 301.
I did something similar here with custom author pages – https://generatepress.com/forums/topic/linking-to-custom-author-archive/ which works great.
is it possible to do something similar for categories?
Thanks
December 6, 2019 at 6:10 am #1095777DavidStaffCustomer SupportHi there,
maybe this doc from WP Show Posts is what you’re looking for:
https://docs.wpshowposts.com/article/use-static-pages-as-category-archives/
December 6, 2019 at 6:42 am #1095814PaulIs it possible to adjust this code for when the slugs do not match?
December 6, 2019 at 4:59 pm #1096528DavidStaffCustomer SupportHmmm.. its not going to be automatic as the names need to be identical – could try conditionally checking a category name and setting a different page name like so:
add_filter('request', function( array $query_vars ) { if ( is_admin() ) { return $query_vars; } if ( isset( $query_vars['category_name'] ) ) { $pagename = $query_vars['category_name']; // Check for specicific category name if ( $pagename == 'category-slug') { // if specific category return specific page $query_vars = array( 'pagename' => "my-page-name" ); } else { // else select page that has same name as category slug $query_vars = array( 'pagename' => "$pagename" ); } } return $query_vars; } );
November 18, 2021 at 4:01 pm #2010194HendrikI’m in the same boat with the need to use a page instead of the standard archive design.
Using the above script works fine, but what I noticed is that posts within the category that has a static archive page don’t render the correct slug.Example:
“mydomain.com/category/widgets” uses a static page “widgets” in place of the regular category page.
However, what happens is that a post “blue widgets” in the category “widgets” now renders this slug:
“mydomain.com/blue-widgets”whereas it should be
“mydomain.com/category/widgets/blue-widgets”
🙁
November 18, 2021 at 5:51 pm #2010250HendrikNevermind 😉
The problem is actually a different one.
When I use the script from here
https://generatepress.com/forums/topic/linking-to-custom-author-archive/inside my functions.php, with my permalink settings to “custom structure: /%category%/%postname%/”
the slug for a post in the category “widgets” will render
“mydomain.com/widgets/blue-widgets”
That looks as intended, HOWEVER, the page that gets rendered under that slug is not the post content for “blue-widgets”, but the category page (which is using the static page as per enforced by the above script).
Basically, it looks like URL for the post always redirects to the category page.
I’m not using any SEO or redirection plugins, btw.Would you have any ideas why this is happening? 🙁
November 19, 2021 at 5:03 am #2010788DavidStaffCustomer SupportHi there,
could you raise a new topic where you can share a link to the site so we can take a look and see whats going on ?
-
AuthorPosts
- You must be logged in to reply to this topic.