1

I've found this is on of my theme files:

<?php the_category( ' ' ) ?>

Ideally I'd like to wrap the whole thing in a function and replace some of the URL's being output, but I don't seem to be able to affect the output at all:

<?php updateToPerfectURLs(the_category( ' ' )); ?>

Ultimately I want to change the URL's wordpress ic creating for my category pages. And it'd beyond what you can do with the category base setting....

  • May I know how you want the URL to be? – Prasad Nevase Sep 17 '15 at 9:21
2

I'm not sure why you need this or what is your exact use case here, but, in short, the_category() simply echos the returned value from get_the_category_list() which uses get_category_link() which uses get_term_link() to get the URL of the category page.

This is where you should look if you need to change the URL of link linking to the category page. In get_term_link(), there is a term_link filter that you can use to alter the URL returned for a specific term/category

return apply_filters( 'term_link', $termlink, $term, $taxonomy );

So, if you need to change the URL of term ID 1 for the category taxonomy, you can try something like the following piece of code (NOTE: The code is untested)

add_filter( 'term_link', function ( $termlink, $term, $taxonomy )
{
    // Check if we have the correct term and taxonomy, if not, bail early
    if (    $term->term_id != 1 // Check if current term is 1 or not
         && $taxonomy != 'category' // Check if the current term is from the taxonomy category
    )
        return $termlink;

    // If we came to this point, we have the desired term and taxonomy, so lets alter the URL
    // Here should be the code to construct your new URL, you need to work on this
    $termlink = 'VALUE_OF_NEW_URL'; // This will be the new URL our term to link to its term page

    return $termlink;
}, 10, 3 );
  • So do I just add the second code block to my functions.php file? I tried that and get: Parse error: syntax error, unexpected '10' (T_LNUMBER) – wilsonf1 Sep 17 '15 at 10:38
  • Jip, it goes into functions.php. Sorry, I forgot a comma, have updated the code. Should be fine now – Pieter Goosen Sep 17 '15 at 10:42
  • Wow worked a treat! Thank you so much for helping. – wilsonf1 Sep 17 '15 at 11:12
  • Glad to hear it worked :-). Enjoy – Pieter Goosen Sep 17 '15 at 11:21

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.