$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - How do I rewrite a single category link to point to a custom page?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论

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?

Post a comment

comment list (0)

  1. No comments so far