Support » Fixing WordPress » Function to change link if certain category

  • Hi,

    currently I am developping a new site, using the newspaper theme. This theme uses a menu option that is called “mega menu”. It offers the change to add a category to the menu bar, positioning the names of the categories to the left. When the visitor hovers over one of these links, on the right it shows the first new articles on the right.

    However due to theme restrictions (which I can explain in detail but simply wont since it would take us to far) I would need to do the following.

    The item “albumrecensies” correctly links to the category location. This category page is however very difficult to alter it’s layout, which is why I created a page.

    I am looking for an argument or piece of code that can simply alter the link it points to.

    so, right now if you hover over “albumrecensies” it will go to: http://v2.enola.be/category/muziek/muziek-recensies/
    whereas I would like it to go to
    http://v2.enola.be/albumrecensies/

    The goal is that it takes the theme takes the example photo’s on the right from the category page, whereas the link should take you to the albumrecensies page.

    Adding the page in the menu results in it loading a near full page of example articles which is simply not something we like to see.

    I came up with this, added to the theme’s functions.php but it breaks all the links so it’s not really what I’m looking for.

          add_filter( 'term_link', function ( $category )
    {
        
        if ( $category != 'muziek-recensies' )
            return $termlink;
    
        $termlink = 'http://v2.enola.be/albumrecensies/'; 
    }, 10, 3 );

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Well yeah, you use in your anonymous function $termlink which doesn’t seem to be defined. Or is it not your full code? That’s for starters.
    Besides, filter for term link has different params (take a look at developer docs).
    Probably you need to compare your slug (which is $taxonomy ?) with a given value, so try this code instead:

    add_filter( 'term_link', function ( $termlink, $term, $taxonomy  )
    {
        if ( $taxonomy != 'muziek-recensies' )
            return $termlink;
    
        return 'http://v2.enola.be/albumrecensies/'; 
    }, 10, 3 );
    • This reply was modified 2 years, 2 months ago by Ekaterina. Reason: wrong operator used
Viewing 1 replies (of 1 total)
  • The topic ‘Function to change link if certain category’ is closed to new replies.