categories - How do I rewrite a single category link to point to a custom page?

admin2025-06-06  5

I have categories Cats, Dogs and Rabbits.

I would like to change the link used for "dogs" when listing category on the front end to a custom page instead of the category archive.

I have categories Cats, Dogs and Rabbits.

I would like to change the link used for "dogs" when listing category on the front end to a custom page instead of the category archive.

Share Improve this question asked Oct 27, 2018 at 9:41 David BarclayDavid Barclay 58 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

Your goal seems to be to change the link which is output when listing the categories rather than making the existing link point to a different page as my last answer assumed. So I am going to try a different answer with a different solution.

Using the filter term_link in the function get_term_link() you can change the link which is generated for a specific category.

function wpse_317747_filter_term_link( $termlink, $term, $taxonomy ) {
    if ( "category" !== $taxonomy || "dogs" !== $term->slug ) {
        return $termlink;
    }
    return get_permalink( $target_page_id );
}
add_filter( "term_link", "wpse_317747_filter_term_link", 10, 3 );

This changes the generated link if working on category taxonomy and the current term slug is dogs. Just set $target_page_id to correspond to the page you want the link to point to.

For special behavior for a category, you can add a template file for the specific category. Such files are called Taxonomy Templates. Creating the file ´category-dogs.php´ in your theme's directory will make wordpress load this file when the category with the slug ´dogs´ is requestd. As model for the new file you can copy the original ´category.php´ in your theme or just redirect the user to another existing page.

Depending on how you display categories, you could create a menu where the link to Dogs is a page, instead of the category. Do you have posts in the Dogs category?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749204948a317241.html

最新回复(0)