1

Can anyone tell me the correct code for getting category URL + current URL?

I'm trying to get => [categoryURL]/?sortby=asc

I've tried different codes, but it didn't work.

if (is_page('echo esc_url( wp_get_current_url()/?sortby=asc'))
<?php if (is_page(array('/?sortby=asc', '/?sortby=desc'))) { ?>
<?php> $category_link = get_category_link( $category_id ); ?>
<?php if (is_page(array('$category_link/?sortby=asc', '$category_link/?sortby=desc')))
1
  • If a sufficient answer has been given, please mark it as the accepted answer.
    – t31os
    Oct 13 at 19:47

1 Answer 1

2

The documentation on is_page(), when you scroll down, has some great examples of how to use it:

// When any single Page is being displayed.
is_page();

// When Page 42 (ID) is being displayed.
is_page( 42 );

// When the Page with a post_title of "Contact" is being displayed.
is_page( 'Contact' );

// When the Page with a post_name (slug) of "about-me" is being displayed.
is_page( 'about-me' );

/*
 * Returns true when the Pages displayed is either post ID 42,
 * or post_name "about-me", or post_title "Contact".
 * Note: the array ability was added in version 2.5.
 */
is_page( array( 42, 'about-me', 'Contact' ) );

Note how it's just the slug, not the URL of the page, and it only works for the page post type, whereas a category is something else (a taxonomy).

To do what you want, you may want to get the complete URL of a page, including the query string. (According to this post) you can do this:

global $wp;
$page_url = add_query_arg( $wp->query_vars, home_url( $wp->request ) );

Then you can run comparisons on $page_url, look for strings inside of it with stristr(), etc.

One thing that might be helpful about debugging and working with WordPress code:

If I'm not sure about how a function works, I Google for (function_name) wordpress — the top result is typically the WordPress developer guide, which tells me what the function does, the parameters it accepts, and sometimes even has examples down below

You may have done that, but I thought to mention it!

2
  • 1
    Thank you! I've fixed the problem using <?php $url = $_SERVER["REQUEST_URI"]; $isitorder = strpos($url, 'sortby'); if ($isitorder!==false) { ?>
    – gfo95
    Sep 24 at 18:33
  • Cool, that works, too. I like your use of a variable, isitorder, since it helps meaningfully clarify what is going on in the if statement. Sep 24 at 18:50

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.