WordPress.org

Ready to get started?Download WordPress

Forums

Dislaying Title of Two Category After Search (5 posts)

  1. leeyha
    Member
    Posted 7 years ago #

    Hi, I was able to allow the user to select from 2 categories and wordpress would display all the posts matching these categories. Only the posts in both categories are displayed.

    On the post page, I want to display the title of the two categories selected before the list of posts - some what like "Here are archives to XXX and YYY categories". I can display the 1st category title but not the second selected category title.

    The input string goes something like this http://website/?cat=3&cat=12. If a search string is supplied, it goes like this http://website/?cat=3&cat=12&s=searchtext

    Anyone please help.

  2. leeyha
    Member
    Posted 7 years ago #

    Think I wasn't clear on the help required. The help question is: How can I display the title of the two categories selected on the post page?

  3. Kafkaesqui

    Posted 7 years ago #

    I'm not sure the issue here is one of clarity rather than difficulty.

    First thing, as far as I know using:

    http://website/?cat=3&cat=12

    will not work. The second cat value overrides the first in your GET array. WordPress however will parse:

    http://website/?cat=3,12

    as a query for posts in categories 3 and 12. With that, to display the categories we can do something like:

    <?php
    global $wp_query;
    if( $wp_query->query_vars['cat'] ) :
    $cats = explode(',', $wp_query->query_vars['cat']);
    foreach ($cats as $catid) :
    $category = get_the_category_by_id($catid);
    echo $category . ' ';
    endforeach;
    endif;
    ?>

  4. leeyha
    Member
    Posted 7 years ago #

    Hi, thanks for this. It comes close to what I wanted & found my solution from this hint you provide.

    Thanks Very Much & Merry Xmas.

    Here is my solution.

    The string that I am passing is actually:
    http://website/?s=&cat=3&cat=12

    Here is my code:
    $cats = explode('=', $_SERVER['QUERY_STRING']);
    echo $_SERVER['QUERY_STRING']. ' is server query | ';
    echo count($cats). ' is count | ';
    echo $cats[0]. ' is 0 | ';
    echo $cats[1]. ' is 1 | ';
    echo $cats[2]. ' is 2 | ';
    echo $cats[3]. ' is 3 | ';
    if (count($cats) > 2) :
    echo get_the_category_by_id($cats[2]). ' and '. get_the_category_by_id($cats[3]). ' categories.';
    else :
    echo get_the_category_by_id($cats[1]). ' category.';
    endif;
    ?>

    Here is my output:
    s=&cat=3&cat=12 is server query | 4 is count | s is 0 | &cat is 1 | 3&cat is 2 | 12 is 3 | All Learning Providers and All Locations categories.

    I know it is not 'neat' to pass a string like '3&cat' to get_the_category_by_id() function, but this this case, WordPress is smart enough to pick up the cat_id. I am really pleasantly surprised by this.

    Thanks Again.

  5. lxg
    Member
    Posted 7 years ago #

    People, don't forget to sanitize your user input. Many of the functions you use are not designed to take input from "outside". At least you should run a mysql_real_escape_string() on the input variables before inserting them into any internal WordPress function. You don't want your WP database poisoned (and maybe welcome some new admin members), do you?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.