WordPress.org

Ready to get started?Download WordPress

Forums

Help with category page (12 posts)

  1. caioxavier
    Member
    Posted 10 months ago #

    Hey guys, I'm new to WP and PHP so I can't really rely on my hardcoding skills to get what I want, so I need your help. It should be simple:

    I have category pages and child categories in some of them. I want to display in the category page, links to the current parents and child categories, so the user can filter the posts.

    Imagine we are broswing Category 1, with has 2 child categories. All I want is to display:

    Category Title

    [All] - [Child 1] - [Child 2]

    [the loop]

    Only the content and category title would be displayed if there are no child categories.

    I'm aware I could do this by making specific page templates, but I want to keep everything lean and dynamic, so my client can change the categories as he pleases.

    Thank you guys in advance!

  2. bcworkz
    Member
    Posted 10 months ago #

    Just add wp_list_categories() to your category template. There's a slew of parameters to get almost anything you like. Combined with CSS, the possibilities are endless. And if that's not enough, the 'wp_list_categories' filter allows you to alter the output exactly as needed.

  3. caioxavier
    Member
    Posted 10 months ago #

    the problem is that I can't make wp_list_categories display the info based on the current page. The way I found around it is to use conditional function is_category() to call a different wp_list_categories() based on current page ID.
    That way I have to make a condition to each category (parent and child). Is there a better way?

  4. bcworkz
    Member
    Posted 10 months ago #

    Maybe, I don't really understand why you need a conditional at all. I probably just don't understand your hierarchy. I would expect you would get your desired results by passing the current category ID as the 'child_of' argument. Granted you would need a separate call to display any parent link, but still I see no need for a conditional.

    I feel like I'm missing something, can you fill me in? Or maybe posting your current code would help explain? (If longer than a dozen lines, post on pastebin.com and add a link here)

  5. caioxavier
    Member
    Posted 10 months ago #

    Well, I'll try to explain more about the hierarchy I have. It's pretty simple:

    I'm using the regular category.php template, and I would like to stick to that because that way the client can create a new category and the code would still work (if I can make it work...)

    I want the category page to display buttons to the child categories, that way you can filter the posts you get. So, if you are under "Category A", which has 2 childs called "Child 1" and "Child 2" I would like it to display:

    [Category A] [Child 1] [Child 2]

    [posts loop]

    and the same if you are in Category B or Category C (with their own child categories)

    Using wp_list_categories(), I would have to customize based on category ID's, which would make it impossible to use a single template (category.php) to list the right childs without using conditionals.

    I could solve this adding a bunch of templates or with a lot of conditionals, but I want to avoid that since the client would have to touch code in order to add a new category.

    Do you have it clearer now?

  6. bcworkz
    Member
    Posted 10 months ago #

    Thank you for your patience, I guess I understood all along, I was just confused by your apparent need for a conditional. Something like this in the category template should serve without need for conditionals or multiple templates:

    <?php $cat_id = get_query_var('cat');
    echo get_category_parents( $cat_id );
    wp_list_categories( array('child_of' => $cat_id,)); ?>

    The two category functions both take arguments which I have omitted that dictate how the HTML is output in order to better control the final look of the lists.

  7. caioxavier
    Member
    Posted 10 months ago #

    Thank you for trying to help me.
    Unfortunately the code you provided didn't work as expected. The childs are displayed in the parents category but not in the childs.

    I have solved using the if is_category() and wp_list_categories().

    If you want to see that in action so you can understand exactly what I was trying to achieve you can visit http://www.dailyskateboarding.com

    Thanks again for the help. This is the first website I make (I created the theme from scratch) and I feel I really need to learn solid PHP.

  8. KidsEatFree
    Member
    Posted 10 months ago #

    What I did was only add it to my blog portion of my website. My main website is not wordpress, but my blog is. Take a look to see what I am talking about. View the blog and you will see. Or go to http://www.ourkidseatfree.com

  9. anotherwpguy
    Member
    Posted 10 months ago #

    HI caioxavier,
    I looked at your site but did not see any dropdown/ajax search forms. Can you please share the information on how you did it?

    KidsEatFree
    The search box on the right hand looks really good. I am looking for something like that but I am looking something like this.
    3 dropdown lists, when user selects a country in the first box such as USA, the second dropdown should automatically be populated with all the states related to the country USA and after a user selects a state such as Texas the results should show everything related to the state Texas.
    Do you know how can I implement it in wordpress?

  10. caioxavier
    Member
    Posted 10 months ago #

    Hey, anotherwpguy.

    Actually my search form is the most simple it can be.

    <form action="http://www.dailyskateboarding.com" method="get" accept-charset="utf-8" id="searchform" role="search">
    <input type="text" name="s" id="s" value="Buscar..." onblur="if(this.value=='') this.value='Buscar...';" onfocus="if(this.value=='Buscar...') this.value='';" />
    <button onclick="submit();"><img src="http://www.dailyskateboarding.com/wp-content/themes/dailytheme/img/searchicon.png"/></button>
    </form>

    In your case I think an advanced search plugin or some custom PHP could solve the problem, but as you can see I'm only beggining to learn PHP and just made my first website, so I'm afraid I won't be of much help to you.

    Good luck!

  11. anotherwpguy
    Member
    Posted 10 months ago #

    Hey caioxavier,
    though the code you provided is not of much help but I appreciate your willingness to help.
    I talked to a couple of plugin developers and heard that it is a complex code but could be done with jquery and ajax.
    I will keep my eye open for this and will keep you posted.
    Thanks again

  12. bcworkz
    Member
    Posted 10 months ago #

    @anotherwpguy,
    If you understand how to utilize AJAX in WordPress, what your are trying to do is not really that complex, it is more a matter of setting up your data correctly so the AJAX calls can quickly populate the appropriate HTML elements.

    However, AJAX in WP is not exactly like what one would do in other sites so the code is a bit more complex than normal. Still, while not a beginning coder project, it is doable for anyone with reasonable coding skills.

    Basically, on selection of the first form element, the selection is sent for the server to return the content for the next element. The server queries the DB restricted by the selection, then builds the next field content from the query results in a structure much like the standard WP Loop.

    If this sounds like something you can tackle, start a new topic in this sub-forum and I'll try to answer any other questions you might have. If you really need a working solution, I cannot help you, but I wish you good luck in finding a solution. (If you do have more questions, I might be very slow to respond for the next week or so, but someone else may be able to help you)

Reply

You must log in to post.

About this Topic