0

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.

0

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.

  • Thanks very much this solved my query! Unable to upvote due to newbie and low reputation myself. Thanks anyway! – David Barclay Nov 1 '18 at 0:25
  • I went ahead and added this code to my functions and changed $target_page_id to the desired ID and its return a blank page. – David Barclay Nov 3 '18 at 17:33
  • @DavidBarclay I just added two more arguments to the add_filter() call, as the filter function takes 3 parameters. If that was not the cause, you need to activate debug mode, to see the relevant error messages. – JHoffmann Nov 5 '18 at 9:14
0

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.

  • That won’t change the url which I’m also looking for. – David Barclay Oct 27 '18 at 17:22
  • Why are you even using categories if you don't want to use the URL or template? – Jacob Peattie Oct 28 '18 at 3:36
-1

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?

  • This would mean having duplicated content. I would only like some of my categories to use a custom page. – David Barclay Jan 2 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.