1

I have Area pages like below:

http://www.example.com/area1

http://www.example.com/area2

On each area page common wp menus are displaying categories. To illustrate:

Menu Items:

-> category1

-> category2

I have set my Permalink settings as below:

Custom Structure: http://www.example.com/artiklar/%post_id%

So for now My category URL is:

http://www.example.com/artiklar/category/category1

But my requirements is when user visit category page suppose category1 from area page say area1 then category URL turns out like below URL:

http://www.example.com/artiklar/category/category1?tag=area1

And if category menu is visited from area 2 then it should be :

http://www.example.com/artiklar/category/category1?tag=area2

Please suggest, I am not getting any idea by searching on web how to implement this kind of structure.

EDIT:

Trying to make it more clear that I want to append slug of page to category URL (page from where user will visit category link, slug of that page will get added to category URL at end of URL)

  • You could use the HTTP referrer header to know where the user came from, for example. Not sure if we can give an exact answer without a deeper knowlodge of what should the results. You may want to investigate something about tracking the referrer. – cybmeta Nov 25 '15 at 7:35
  • Thanks, we can get value but I was not aware about how to alter category/term url and add query string. But as shown in answer by @Pieter Goosen I came to know how to add query string in category urls. – jas Nov 25 '15 at 9:47
  • 1
    @cybmeta Check my answer. Also HTTP referrers are very unreliable and should in general not be used – Pieter Goosen Nov 25 '15 at 9:49
  • I didn't answer, I just posted a comment. I thought that the question wanted to track where the user came from, tracking referrers are the standard for that, may be not HTTP referrers I agree with that. Anyway, I understood the question in this way: add "page-a" slug to category link if user came from "page-a", not if user is in page-a, which is what you do in the answer. It seems I understood the quesiton incorrectly. But not only me, there was two "unclear what you asking" votes; I've retracted mine. – cybmeta Nov 25 '15 at 10:02
  • I have update question to make it more clear considering your comment. Thx – jas Nov 25 '15 at 11:45
1

You would want to filter the category link to add the custom variable to the category link. You can make use of the get_term_link filter to filter the category link address

Here is a very basic idea:

Suppose that we, taken from OP, that you need to add the custom variable only to terms belonging to the build in taxonomy category, and only when we are on a real page and we want to add the page ID as value to the URL, we can create a filter function to just do exactly that, and by using add_query_arg(), we can add a new query variable, lets call it frompage, to the URL.

Here is a basic function, which you can extend and modify as you see fit. I have commented the code to make it easy to follow

add_filter( 'term_link', function ( $termlink, $term, $taxonomy )
{
    // If this is not a page, return the unmodified term link
    if ( !is_page() ) // Change this to what is specific to your needs
        return $termlink;

    // Only target the build in taxonomy 'category'
    if ( $taxonomy !== 'category' ) // Adjust to your exact taxonomy
        return $termlink;

    // Get the current viewed paged id
    $current_id = get_queried_object_id();

    // Make sure we actually have a value, if not, return the term link
    if ( $current_id == 0 )
        return $termlink;

    // If we reached this point, we are good to go and should add our query var to the URL
    $termlink = esc_url( add_query_arg( ['frompage' => $current_id], $termlink ) );

    return  $termlink;
}, 10, 3 );

You can then retrieve the value as follow:

$value = filter_input( INPUT_GET, 'frompage', FILTER_VALIDATE_INT ); 
if ( $value )
    echo $value;

Note that I have used filter_input here to get the $_GET value. That is because that filter_input validates if the passed value exists and either returns it or returns false. Also, you can pass a filter to be used on the value, in this case FILTER_VALIDATE_INT. All in all, it is a much better and safer way than $_GET( 'frompage )`

  • Really appreciate that ! such a great answer with detailed explanations. I just had to get current page viewed title like that $current_title = get_the_title($current_id); and rest worked great and its really very helpful to understand how things going. Thanks ton for your efforts and explanations. – jas Nov 11 '15 at 18:13
  • 1
    My pleasure, glad it worked and that I made some sense ;-). Enjoy – Pieter Goosen Nov 11 '15 at 18:17
  • 1
    Note, you can also get the title as follow: get_queried_object()->post_title – Pieter Goosen Nov 11 '15 at 18:18
  • 1
    Thanks I will get title like this. Really helpful now I came to know how taxonomy links works and how to add args using add_query_arg(). Thanks a lot again :) – jas Nov 11 '15 at 18:19

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.