WordPress.org

Ready to get started?Download WordPress

Forums

Filter categories depending on user input in a previously visited page. (6 posts)

  1. banskt
    Member
    Posted 3 years ago #

    I would like to include the following functionality in a wordpress website. Is it possible? If so, how?

    PageOne - There is a list of posts (belonging to categories A, B and C). Using javascipt, I have created a menu to filter which category to view. The menu lets the user to view a list of posts in each category (javascript calls the list on that page itself), and hides the other categories.

    PageTwo - single.php displaying the posts. contains 'next' and 'previous' links.

    I want the 'next' to link to the next post of the category filtered in "PageOne"

    Any ideas would be greatly appreciated. I presume the only way out would be to pass variables to pages. But I would not like to hinder your creative thoughts.

    Thanks,
    banskt

  2. max@op
    Member
    Posted 3 years ago #

    Hi,

    if each post has only one category, I would use <?php next_post_link(); ?> because it has 'in_same_cat' parameter.

    If one post can have more categories assigned to it, than I would on single.php query posts by previous page category using $category = strstr($_SERVER['HTTP_REFERER'],"category") and if needed, use it as 'in_same_cat' parameter.
    Many say that $_SERVER['HTTP_REFERER'] is not reliable because it is affected by permalink structure, so the other solution I'd try is to get and store PageOne current category and then call it and use it on PageTwo.
    I'm new at coding, so this other solution is something I think I read somewhere.

  3. banskt
    Member
    Posted 3 years ago #

    Hi,

    Thanks for the reply, and the heads up.

    Each post has more than one category.

    Now, $_SERVER['HTTP_REFERER'] will show the permalink of the post. Even if I change the permalink mode to /%category%/%postname% the permalink will show only one category, which might not be the one which is selected in PageOne.

    As far as I understand, this method can be used for subcategory navigation, provided one click on links with subcategory permalinks. (Note that I am clicking on permalink of a post, whose permalink reflects the last category, and NOT the selected category).

    I would be more than glad if I am wrong.

    The other solution - "get and store PageOne current category and then call it and use it on PageTwo" is the most obvious solution. But how could I implement it?

    The way I could think of was to set a javascript variable onclick('menu item'). Then the problems are:
    a) how could I send it to php?
    b) how could I send the variable to another page?

    I do not know any method of changing php variable on mouseclick. That could also be a solution.

    ------------- Details about PageOne --------------

    Here I need to tell in more details about PageOne. I call all items of ParentCategory in a loop.

    <?php query_posts('cat=3&showposts='); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <li class="<?php foreach((get_the_category()) as $category ) { echo $category->cat_name . ' all'; } ?>">
    <?php the_post_thumbnail('post-thumb'); ?>
    <?php endwhile; ?>
    <?php endif; ?>

    Note that I have passed the subcategories as class of the

  4. Now I use a menu :
    <ul>
    <li><a href="#cows" title="cows">cows</a></li>
    <li><a href="#dogs" title="dogs">dogs</a></li>
    <li><a href="#goats" title="goats">goats</a></li>
    </ul>

    Using the attr('href') in javascript, I show or hide the list items of different categories (all, cows, dogs, goats), since I already have the <li class="cows dogs all">item1

  5. in my html.

    -------------- Details ends here -----------------

    Thanks,
    banskt

  • max@op
    Member
    Posted 3 years ago #

    Just to check if I understand what are you trying to do.
    When you land on PageOne you have an regular index page, post titles with thumbnails and excerpts from all categories, and a menu to filter categories. When we click on category, that category posts are displayed. When we open post from PageOne specified category, next/previous links should consider only posts in that category cohosen on previous PageOne.
    If so, for categores menu we can use wp_list_categories or calling custom nav menu in which 'All' link points to index or any custom page that will display all posts.
    By clicking on category in one of this menus, WP queries posts and displays them trough category.php template, if we have one, else uses index.php. In permalink of that page is category info - can be cat ID, cat nicename, name or slug.
    So i suppose that you use javascript not to filter posts, yet for an show/hide posts effect.
    $_SERVER['HTTP_REFERER'] will return previous page link. We use it on a theme we're just developing, for displaying different submenus on single page, depending on previous page. For example, if you open post from archive page, archive submenu will be displayed, if landed from tags page, there will be tag menu and so on.
    So, maybe you could just trigger javascript on menu item click, and the previous - next links query using $_SERVER['HTTP_REFERER'] - previous page permalink.
    When we click on category, WP reloads page, so maybe you could even if (is_category ()) page, trigger your script whenever category page loads.

  • banskt
    Member
    Posted 3 years ago #

    Thanks for your reply. I really appreciate your help to get this thing sorted out.

    Now here are a few points to clarify the situation further:
    a)

    When we click on category, WP reloads page

    -- No, to prevent reloading of pages (I want to make the browsing experience smoother), I load all category items at the beginning. All of them are shown by default. Javascript is used to hide them later, or re-show them.

    b)

    By clicking on category in one of this menus, WP queries posts and displays them trough category.php template

    -- No, I use javascript to filter them. I know that I can filter them through WP_Query('cat=#'), but I preferred to use javascript to prevent reloading of pages.

    c)

    So i suppose that you use javascript not to filter posts, yet for an show/hide posts effect.

    I use javascript to filter posts. Filtering = hiding other posts

    That being said, I just realised that while filtering I can change the a href values using javascript and that will do the trick. Once I have the desired href value for the links I can parse that href value to get the category in PageTwo, since I will get the href value via $_SERVER['HTTP_REFERER']

    I will try that one and post it here.

    Many thanks,
    banskt

  • gobanana
    Member
    Posted 2 years ago #

    I built a plugin WP-Filters that I think might do what you're looking for, or at least get you a little bit closer. At the moment it just hides the irrelevant posts on the screen while they are still there. I am working on modifying the plugin to instead call the posts using JSON which will then allow you to use that query to cycle through the posts.

    Check it out here: http://gobanana.ca/wp-filters

    You may want to consider loading the single post in a ligthbox so you don't lose the context of the filtered page and just use the lightbox to cycle through the posts on the page. It may take a little bit of creative coding, but that's the solution I can envision using my plugin. At least until the next version...

    Good luck!

  • Topic Closed

    This topic has been closed to new replies.

    About this Topic

    Tags