Subcategory open by itself
-
Hey!
We want to use subcategories, it works fine when you click the new parent category. Clicking parent shows the child categories.
The error is that whatever category we click, all the parent categories open up.
Further explanation –
Let’s say you click a category that has no children. Then all parent categories show their subcategories aswell.This is really irritating and strange, is it just something wrong on our page? We have gained some codesnippets from wpadverts, maybe it is one of them. If not, how can we fix this?
We do not have this feature active atm as we see it very bad looking. But we will in the near future when it is as we please!
Thanks in advance
The page I need help with: [log in to see the link]
-
Hi,
can you let me know what code snippet you are using? There is a couple of them and are working differently (depending on what the user wanted to achieve) related to showing the categories above the [adverts_list] so I would need to know which one you are using.Additionally, if you could post a screenshot showing the incorrect view that would be helpful as well.
Thansk for a fast response!
I will send images for the 3 codesnippets we are using, my guess is that something might be going wrong with codeSnippetThree.
Codesnippets:
1-add_action( "adverts_sh_list_before", "categories_above_adverts_list" ); function categories_above_adverts_list( $args ) { global $wp_query; if( is_tax( 'advert_category' ) ) { $current_category = $wp_query->get_queried_object_id(); // code for getting the sub categories and displaying them here $columns = "adverts-flexbox-columns-4"; $show_count = 1; $terms = get_terms( 'advert_category', array( 'hide_empty' => 0, 'number' => 0 ) ); include ADVERTS_PATH . '/templates/categories-top.php'; } }2-
add_action( "wp_footer", function() { if( ! is_tax( 'advert_category' ) ) { return; } $term = get_queried_object(); ?> <style type="text/css"> .adverts-category-slug-<?php echo $term->slug ?> { background-color: lightgrey !important; } </style> <?php });3-
/** Plugin Name: WPAdverts Snippets - Search by category Version: 1.0 Author: Greg Winiarski Description: Adds categories dropdown input to [adverts_list] search bar. */ // The code below you can paste in your theme functions.php or create // new plugin and paste the code there. add_filter( 'adverts_form_load', 'search_by_category_form_load' ); add_filter( 'adverts_list_query', 'search_by_category_query' ); /** * Adds category dropdown into search form in [adverts_list]. * * @param array $form Search form scheme * @return array Customized search form scheme */ function search_by_category_form_load( $form ) { if( $form['name'] != 'search' ) { return $form; } $form['field'][] = array( "name" => "category_search", "type" => "adverts_field_select", "order" => 20, "label" => __("Kategorier", "adverts"), "max_choices" => 10, "options" => array(), "options_callback" => "adverts_taxonomies", "meta" => array( "search_group" => "visible", "search_type" => "full" ) ); return $form; } /** * Adds tax_query param to WP_Query * * The tax_query is added only if it is in $_GET['advert_category'] * * @param array $args WP_Query args * @return array Modified WP_Query args */ function search_by_category_query( $args ) { if( ! adverts_request( "category_search" ) ) { return $args; } $args["tax_query"] = array( array( 'taxonomy' => 'advert_category', 'field' => 'term_id', 'terms' => adverts_request( "category_search" ), ), ); return $args; }4-
/* * Plugin Name: Enable Adverts Comments. * Plugin URI: http://wpadverts.com/ * Description: This code snippet/plugin enables comments on ads. * Author: Greg Winiarski */ add_filter( "adverts_tax_shortcode_args", function( $args ) { $args["allow_sorting"] = 1; return $args; } ); add_action("adverts_post_type", "enable_adverts_comments"); add_action("adverts_insert_post", "enable_adverts_comments_check"); add_action("adverts_update_post", "enable_adverts_comments_check"); /** * Enables comments in Ads * * This will enable support for comments in wp-admin panel when editing Ad. * * Note you can use this function to further customize Adverts Custom Post Type the * $args argument is "$args" used in register_post_type function. In other words * this function (or rather "customize_post_type" filter) gives you an opportunity * to customize adverts Custom Post Type parameters before * register_post_type function will be called. * * @see register_post_type() * @link https://codex.ww.wp.xz.cn/Function_Reference/register_post_type * * @param array $args Custom Post Type init params * @return array */ function enable_adverts_comments( $args ) { $args["supports"][] = "comments"; return $args; } /** * Set Ad comment status to "open". * * Sets comment_status field to open for a single Ad, this will allow users * to post comment below an Ad. * * @param array $data * @return array Updated $data */ function enable_adverts_comments_check( $data ) { $data["comment_status"] = "open"; return $data; }My guess is that it has something to do with number 3, but dunno.
I will also attach two images with example of how it looks, second image is when clicked into any other category except parent. child categories are subcategories of Parent category in this test.
Picture 1 shows just how it looks from start page.
Could not find any attach file thingy so i used imgur:
picture 1 – https://imgur.com/OOREWBj
picture 2 – https://imgur.com/cPoo3zxThanks,
PhilipHi,
only snippet #1 is related to the category boxes on the top, the way you have it set-up it should show only the top categories as all the params for the get_terms() function are static.It is possible there is some conflicting code somewhere else, you can try to make the get_terms() call ignore it by modifying
$terms = get_terms( 'advert_category', array( 'hide_empty' => 0, 'number' => 0 ) );to
$terms = get_terms( 'advert_category', array( 'hide_empty' => 0, 'number' => 0, 'parent' => 0, 'suppress_filter' => true ) );Hey!
There is no more codesnippets used than that.
That code did not fix it, now we can’t open the parent.
It shows the classifieds inside but not showing the childs inside.Any other idea? This is a feature we really need π
/Philip
Hi,
the code i sent you was meant not to open sub-categories at all, so if they do not show then it seems it is doing its job.From your description, it is not clear to me how do you want it to work like? What do you want to see when you are in the Beten category here https://sportfisketorget.se/advert-category/beten/?
Hey,
Yes it did look like your code were doing just that.
The image were just an example, I will try to better explain.
Image:That image show you that the parent category opens up even if you don’t click just that. So it show you that I clicked on category beten, yet category parent opens up and show all subcategories. That i do not want to happen, imagine having alot of subcategories on different parents and they all open up even when you don’t click it. It gets messy.
Edit: I only want subcategories to show when I clicked on the parent category of the subcategories. Not when I click any random category.
Hopefully clearer? π
/ Philip
-
This reply was modified 5 years, 1 month ago by
nihtrepap.
Hi,
ok so the main problem here is that none of the snippets you posted has a functionality that would show the sub-categories, the first one is the closest as it generates the list of top categories, but it does not have any dynamic params to show sub-categories.The first step would be in this case to figure out where are those sub-categories coming from, for example in the first snippet add
echo "<pre>"; print_r( $terms ); echo "</pre>";and see if the “child0”, “child1” and “child2” categories are inside the $terms variable at all as right now it looks like this is coming from some other code snippet.
Hey.
Okey this is now way off.
We need to back it up abit, my explanation has taken you way out of the real problem.I will try again from the beginning.
child1, child2 and child3 are example subcategories to have something to show you.
They belong to the “Parent” Category, wich also is an example category.
“Parent” category is the parent to the “child1″,”child2” and “child3” subcategories.Here is an updated image:
So the strange thing is that “Parent” category opens up and show “child1”, “child2” and “child3” even when it is not clicked.
“Parent” and it subcategories are not used because it is only an example to show you, so this problem will not show itself on the page. Sorry for the mislead.
I hope this made it clear?
Hi,
hmm one more question what is the desired result on the page like https://sportfisketorget.se/advert-category/beten/? Do you want to show the top categories and the beten sub-categories or always show top categories only?Also if you could enable the child categories so I could see the error on site it will be easier for me to understand it.
Hey.
Yeah I know this might be confusing, but there is no error on that page you linked. It were merely used as an example page for the image made.I would prefer not to activate that as it is not something we want to have as it works right now.
But if it is a must then sure. I could not see any console errors, thought I can check again.
I just don’t want the random subcategories to show if not clicked. As i said in the example beten is clicked, but beten has no subcategories. Parent has subcategories and for some reason they show up when clicking at beten.
I just want to see the “parent” category there and not in an opened state. As it were bot clicked
Hi,
i am afraid i am not sure how to help you with this.The codes you are using do not show the sub-categories, it is meant to show the top categories only and nothing else, if you are seeing some random child categories when visiting one of the category pages then it looks like a conflict with some other plugin/theme and would need to be troubleshot on site.
Hello!
I have activated Test category with 2 TestSub categories.
Click around and you will see what I mean.
They open up even when you don’t even click the Test category.This needs to be fixed, and it do not look like it is to do with my code.
1. if in your snippet you are using the get_terms() like this
$terms = get_terms( 'advert_category', array( 'hide_empty' => 0, 'number' => 0, ) );Then WPAdverts/WordPress correctly shows every possible category as your querying it to show every possible public category.
2. if you modify it as I posted in one of the previous messages
$terms = get_terms( 'advert_category', array( 'hide_empty' => 0, 'number' => 0, 'parent' => 0, 'suppress_filter' => true ) );then it will show the top (level 0) categories only.
3. if you want to show top categories and some (selected) category sub-categories then a single get_terms() call will not do this.
You will need to run two get_terms() calls like this
add_action( "adverts_sh_list_before", "categories_above_adverts_list" ); function categories_above_adverts_list( $args ) { global $wp_query; if( is_tax( 'advert_category' ) ) { $current_category = $wp_query->get_queried_object_id(); // code for getting the sub categories and displaying them here $columns = "adverts-flexbox-columns-4"; $show_count = 1; $terms = get_terms( 'advert_category', array( 'hide_empty' => 0, 'parent' => 0, 'number' => 0 ) ); include ADVERTS_PATH . '/templates/categories-top.php'; $columns = "adverts-flexbox-columns-4"; $show_count = 1; $terms = get_terms( 'advert_category', array( 'hide_empty' => 0, 'parent' => $current_category, 'number' => 0 ) ); if( $current_category < 1 || empty( $terms ) ) { return; } echo "<h2>Sub-Categories</h2>"; include ADVERTS_PATH . '/templates/categories-top.php'; } }4. you are correct in that there isn’t a problem with your code, however, you are expecting the snippet copied from a different thread to behave exactly as you want it to, while the snippet was built with a different purpose/goal and simply does not have this functionality.
That seems like to do the job, great support man!
Will give you feedback about the results if you want!
Big thanks π
Hi,
sure, please let me know, or mark the thread as resolved, so someone in the future maybe will also find this thread useful :). -
This reply was modified 5 years, 1 month ago by
The topic ‘Subcategory open by itself’ is closed to new replies.