Categories not appearing in correct hierarchy - wordpress

When I create a post and try to assign the post to categories, the widget on the right-hand side shows the categories mixed up. For example, when I created the categories, I had the "accessories" category under the parent category "Men", but in the widget it doesn't appear under it; only when I go to category on the left-hand side menu does it appear in the correct order. What is wrong?
In categories I have this hierarchy
but when I assign a post to but when I assign a post to categories for example Men-Accesories-Ties, Men appears second in categories and not first thus messing up the breadcrumbs too

I'll leave the first version of this Answer at the bottom, as it may be useful.
There is no way of organizing that column in a hierarchical way. The solution is to make a custom column and use this Codex snippet:
// Change "post_" for the desired post type
add_filter( 'manage_edit-post_columns', 'custom_categories_register_so_15813936', 20, 1 );
add_action( 'manage_post_posts_custom_column', 'custom_categories_display_so_15813936', 20, 2 );
function custom_categories_register_so_15813936( $columns )
{
$columns[ 'custom-cat' ] = 'Categories';
return $columns;
}
function custom_categories_display_so_15813936( $column_name, $post_id )
{
if ( 'custom-cat' != $column_name )
return;
// get the category IDs assigned to post
$categories = wp_get_post_categories( $post_id, array( 'fields' => 'ids' ) );
// separator between links
$separator = ', ';
if ( $categories ) {
$cat_ids = implode( ',' , $categories );
$cats = wp_list_categories( 'title_li=&style=none&echo=0&include=' . $cat_ids );
$cats = rtrim( trim( str_replace( '<br />', $separator, $cats ) ), $separator );
$cats = str_replace( site_url('category/'), admin_url('edit.php?category_name='), $cats );
echo str_replace( '/" title', '" title', $cats );
}
}
At the left, the default column, and at the right, the custom one.
[first version]
I suppose you are talking about the back-end (/wp-admin). And the behavior you see is WordPress default. To get rid of that "feature", you need the plugin Category Checklist Tree:
On the post editing screen, after saving a post, you will notice that
the checked categories are displayed on top, breaking the category
hierarchy. This plugin removes that "feature".
Additionally, it automatically scrolls to the first checked category.
Works with custom taxonomies too.

Related

How to show only current parent product category with subcategories and hide others in widget on the product category page?

For example, there are 2 different catalogs - men and women (clothes).
There is a widget in sidebar.
Is it possible to hide 'men' category with its child subcategories in all 'women' category pages and subcategory pages, and the same in 'men' catalog in all parent categories and child subcategories hide all from 'women'? From product cat widget.
I tried conditions like
if( has_term(267, 'product_cat' ) ) {
add_filter( 'woocommerce_product_categories_widget_args', 'organicweb_exclude_widget_category' );
function organicweb_exclude_widget_category( $args ) {
// Enter the id of the category you want to exclude in place of '30'
$args['exclude'] = array('262' );
return $args;
}
}
if( is_product_category( array( '267',) ) ) {
add_filter( 'woocommerce_product_categories_widget_args', 'organicweb_exclude_widget_category' );
function organicweb_exclude_widget_category( $args ) {
// Enter the id of the category you want to exclude in place of '30'
$args['exclude'] = array('262' );
return $args;
}
}
The filter works without 'if///{}', hides excluded category from widget at all, but not with if.
I start to think that the simplest way is to hide them using css(body class+parent li id), but maybe it is a correct way?
You can solve it like this way:
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'product_cat' => 'your-cat-name'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
endwhile;
wp_reset_query();
Also this article will help you https://www.codecheef.org/article/how-to-display-category-wise-product-in-woocommerce
Thank you

How to block certain tags from creating in WordPress

Is there any WordPress theme function code that blocks certain wordpress tags from being created? I'd want to exclude some tags from the keyword list, for example, I don't want WordPress to create the following stop-words tags:
adult, bikini, enjoyment, fun, block, admin
You can use the pre_insert_term filter hook. that will help you to prevent tags before inserting. try the below code.
function prevent_some_tags_from_being_add( $term, $taxonomy ){
if( $taxonomy == 'post_tag' ){
$prevent_tags = array( 'adult', 'bikini', 'enjoyment', 'fun', 'block', 'admin' );
if( in_array( $term, $prevent_tags ) ){
return new WP_Error( 'invalid_term', __( 'Sorry this tag is not allowed.' ) );
}
}
return $term;
}
add_filter( 'pre_insert_term', 'prevent_some_tags_from_being_add', 10, 2 );

Apply the_title filter to posts but not on nav menus

I want to apply a filter to the title of a post type, but only on its single page. So I made this function:
function title_filter( $title, $post_id = null ) {
if ( ! empty( wp_get_associated_nav_menu_items( $post_id ) ) && is_singular( 'review' ) ) {
$title = sprintf( '%1$s %2$s', $title, __( 'Review', 'stackoverflow' ) );
}
return $title;
}
add_filter( 'the_title', 'title_filter', 10, 2 );
But the problem is that the filter is also applied to the nav menu items that have a post of that type. I tried to exclude the filter from nav items using wp_get_associated_nav_menu_items() but that doesn't seem to work. Also tried checking the post type using get_post_type() but since the menu items respond to the post type when the item is added using the post type options on the menu editor, they always match and the filter is applied anyway.
The solution given on this question didn't work for me.
Any ideas?
You can check to make sure you're only filtering titles in the loop by comparing with the in_the_loop() boolean function:
function mithc_title_filter( $title, $post_id = null ) {
if( in_the_loop() && is_singular( 'review' ) ){
$title = sprintf( '%1$s %2$s', $title, __( 'Review', 'stackoverflow' ) );
}
return $title;
}
add_filter( 'the_title', 'mithc_title_filter', 10, 2 );

Wordpress Custom Post type archive display

I have a custom post type named STORIES.
Within that post type, I have categories, so each STORY is also assigned a category like this...
Story 1 (assigned category RED)
Story 2 (assigned category BLUE)
Story 3 (assigned category GREEN) etc.
I need to display a page which lists the categories, and when clicked, then lists the posts from that category.
So the page would be a list with...
RED
BLUE
GREEN
What template would control this list?
Then I click on RED and it takes me to a template that displays only the RED Stories.
What template would control this list?
I am guessing I need three specific templates?
One called template-stories.php and one called archive-stores.php and a final one called single-stories.php?
you are guessing right, first you need a page template where you are going to call your terms, but you need to call by his taxonomy, right?, To do that, you need to use the function get_terms( $taxonomies, $args );, and with the function get_term_link($term), you are going to obtain the url of the current term:
$args = array( 'hide_empty=0' );
$terms = get_terms( 'my_term', $args );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
$count = count( $terms );
$i = 0;
$term_list = '<p class="my_term-archive">';
foreach ( $terms as $term ) {
$i++;
$term_list .= '' . $term->name . '';
if ( $count != $i ) {
$term_list .= ' ยท ';
}
else {
$term_list .= '</p>';
}
}
echo $term_list;
}
Then, you need the archive.php where you are going to display all posts associated to this current term.
But first you need to get the current term, and you can do it using the function get_term_by($field, $value, $taxonomy, $output, $filter).
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
Now you can make an if statement:
if($term = 'red'){
the_title();
//Or get_template_part( 'content', 'stories' );
}else{
//...
}

Wordpress category hierarchy - sub category but parent permalink

I have a Wordpress blog. There are more categories. Category hierarchy like below.
Technology News
Mobile News
Internet News
...
in address bar it shown like
site.com/category/technology-news/internet-news
but I want to see at address bar like below
site.com/category/internet-news
so how can I show a sub category as parent category. Remember it's still a sub category actually.
Go into Settings -> Permalinks. Select "Custom Structure" and paste this line in.
/%category%/%postname%/
Next, open your themes functions.php file and drop in the following code.
add_filter( 'post_link', 'remove_parent_cats_from_link', 10, 3 );
function remove_parent_cats_from_link( $permalink, $post, $leavename ) {
$cats = get_the_category( $post->ID );
if ( $cats ) {
usort( $cats, '_usort_terms_by_ID' );
$category = $cats[0]->slug;
if ( $parent = $cats[0]->parent ) {
$parentcats = get_category_parents( $parent, false, '/', true );
$permalink = str_replace( $parentcats, '', $permalink );
}
}
return $permalink;
}

Resources