New answers tagged

0

I had thought I would just comment on it but then it would be too small a place to specify the details. The answer posted by Jan Fabry works nicely! However the drawback of this is, which he even pointed out in the comments is that if you have a URL like http://example.com/sports/world-sports/permalink, it will be stripped down to ...


1

Pieter's answer does work, but it displays all sticky posts even on pages when we want to display filtered posts (to a given category for instance). The following works for me. Adding this to functions.php only displays stickies within the subset of filtered posts : add_filter('the_posts', 'bump_sticky_posts_to_top'); function ...


1

First you will need the slug of the current category: $cat = get_query_var('cat'); $currentcat = get_category ($cat); Then you can use the same loop. Just add the following to $query_args to limit the outcome to the current category: 'category_name' => $currentcat->slug,


0

Please read this Codex page why you should not use query_posts(). If you really want to use query_posts(): query_posts( 'name' => 'test', 'category' => '-15' // notice "minus 15" ); The right way to exclude category: $query = new WP_Query( array( 'name' => 'test', 'category__not_in' => array(15) ) ); if ( ...


-1

Parent child taxonomy loop $taxonomy = 'product_cat'; $category = get_terms($taxonomy); echo '<ul>'; foreach( $category as $cat ){ $link = get_term_link( $cat->slug, $taxonomy ); if(empty($cat->parent)){ echo '<li><a href="' . $link . '">' . $cat->name . '</a>' . '<strong>' . $cat->count . ...


0

Wrap it up! Err, in a conditional statement that is. if ( is_category( 'My Category' ) ) { echo '<p>' . category_description() . '</p>'; } That is just a crude example, though. We would need to see your code to give a definitive answer. Reference: https://developer.wordpress.org/reference/functions/is_category/


0

in_category relies on a global post - it should only be used within the loop. Instead, use the arguments that are passed to your callback to query the categories assigned to the post being edited and check if one of them is video: $cats = get_the_category( $post['ID'] ); if ( in_array( 'video', wp_list_pluck( $cats, 'slug' ) ) { // In "video" category } ...


0

My first port of call is to call global $post; - as that sets you up to use $post->ID ... if you are using it then the next step is to display each part separately e.g. print_r($tags); echo $first_tag; see what's displayed in each part, this will help diagnose where the issue is, as without seeing your code working, we can't say why it's not working.


0

If your theme has body_class() in the body tag (normally in header.php) then the category name is added to the body tag as a class e.g. <body class="my-category"> So in a css file you can do .my-category #my-element { background:#FF0000; } To edit the html for the categories to make them card layouts then you would need to edit the category.php ...


0

I don't know what our single-XXX.php files look like, but if they include calls to get_header (and perhaps get_sidebar()and get_footer) you obviously will get the header twice as it is called again at the bottom of your first code block (and perhaps te rest of the regular page). In the second code block you avoid outputting the header twice, because it is ...


1

Use get_term_children() $term_id = 2; // use get_queried_object()->term_id; to get the current term id $taxonomy_name = 'mypages'; // use use get_queried_object()->taxonomy; to get the current taxonomy name $countchildren = count (get_term_children( $term_id, $taxonomy_name )); echo $countchildren;


0

ok, I was close and I have solution: SELECT wp_posts.ID, wp_postmeta.meta_value FROM wp_postmeta INNER JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id) WHERE 1=1 AND wp_postmeta.meta_key = ...


0

Example for the answer of goldenapples: $update_taxonomy = 'my_taxonomy'; $get_terms_args = array( 'taxonomy' => $update_taxonomy, 'fields' => 'ids', 'hide_empty' => false, ); $update_terms = get_terms($get_terms_args); wp_update_term_count_now($update_terms, $update_taxonomy);


0

1) If you want default text in POST EDITOR, then use @Milo's answer. 2) If you want add default content in front-end output of post,then use this code (in functions.php): function add_before_content($content) { global $post; if ( 'page' == $post->post_type ) {return 'YOUR MESSAGE'.$content;} if ( 'post' == $post->post_type ) {return 'YOUR ...


1

The idea here would be to use the queried object's ID to get the post terms (because we are most probably outside the loop here, if inside the loop, just use get_the_ID()). From there, we can use wp_list_pluck() to get all the term ID's and simply pass that to get_terms() exclude parameter Just a note, as from WordPress V4.5, the taxonomy should be passed ...


0

First you will need an array that contains all categories of the current post: $current_cats = wp_get_post_categories(); // assuming you are in the loop Then you have to loop through $current_cats and remove the items from $terms foreach( $current_cats as $cat ) { if (($key = array_search($cat, $terms)) !== false) { ...


0

The answer was found here: http://www.wprecipes.com/how-to-display-category-name-without-link For my purposes, I needed to define the category function and then call it by name-only inside the php echo: <?php $category = get_the_category($attachment->ID); echo 'html goes here'; echo ''.$category[0]->cat_name.'; ?>


2

Your condition is off and will always be true on category pages due to how logic operators work. This might be more clear if I write what is happening like this: is_category() || ( is_tag() && $query->is_main_query() ) Once is_category() is determined to be true the rest of condition doesn't matter. You should be more precise with your ...


1

WordPress doesn't do this out of the box. So if you want to know about something that has already happened you're too late :) But if you'd like to know in the future you could for instance have a look at this: https://wordpress.org/plugins/wp-security-audit-log/


2

First off, ensure that the category is exactly the same name as the page ( slug and all ). Secondly, the parameters you're passing to WP_Query is incorrect. category_name expects a slug. For example, Page 1 would have a slug of page-1. get_the_title() pulls the current page title - so you're passing Page 1 to a parameter that expects a slug page-1. What ...


0

OK. There where multiple issues here; wp_terms, wp_termmeta and wp_term_taxonomy all had their ID's set not to AUTO_INCREMENT. Changing these and removing the 0 values from each table seems to have resolved this - very odd though. Big thanks to @N00b for helping!


2

is_category() only check the built in post category. Product categories are custom taxonomies. So you need to use is_tax() instead of is_category() and get_term() instead of get_category(). Check this example:- function is_subcategory($cat_id = null) { if (is_tax('product_cat')) { if (empty($cat_id)){ $cat_id = ...


1

You don't need to! With a little understanding of how URLs and query vars work, we can do this entirely within the URL of the page, with no additional PHP code First we have a URL: example.com/abc/123 Then we have a rewrite rule. The rule has a regex pattern that matches the URL, and a second bit that says were all the bits that match go, e.g. ...


0

add_filter for widget_title does not allow more than one parameter when called so the above will throw PHP warnings. This seems to work instead: function my_repair_categories_empty_title() { if ( $base == 'categories' ) { if ( trim($instance['title']) == '' ) return ''; } return $title; } add_filter('widget_title', ...


0

You could use 2 different loops: 1 for some of your categories and another for the rest. //Here 2,6 are the category ids $query1 = new WP_Query( array( 'category__in' => array( 2, 6 ) ) ); // The Loop while ( $query1->have_posts() ) { $query1->the_post(); } // Restore original Post Data wp_reset_postdata(); $query2 = new WP_Query( array( ...


0

If anyone needs it $args = array( 'type' => 'product', 'parent' => get_queried_object_id(), 'orderby' => 'term_group', 'hide_empty' => true, 'hierarchical' => 1, 'taxonomy' => 'product_cat', 'pad_counts' ...


0

I used this script to select a category using jQuery. Inside wp-admin/post-new.php $category_name_check = "My Category Name"; echo "<script type=\"text/javascript\">"; echo "jQuery( \"label:contains('$category_name_check') > input\" ).attr('checked', true)"; echo "</script>"; where $category_name_check is the name of the category to be ...


0

Answer by Implode solve issue only if you are not using other queries like 'meta_key' even . I found complete solution here. function fix_nav_menu( $query ) { if ( $query->get( 'post_type' ) === 'nav_menu_item' ) { $query->set( 'tax_query', '' ); $query->set( 'meta_key', '' ); $query->set( 'orderby', '' ); } } ...


0

You could just set custom permalinks for your categories with this plugin.


1

You can use get_ancestors: <?php if ( $terms = get_ancestors( get_queried_object_id(), 'category', 'taxonomy' ) ) { $crumbs = []; foreach ( $terms as $term ) $crumbs[] = sprintf( '<a href="%s">%s</a>', esc_url( get_term_link( $term ) ), esc_html( $term->name ) ); echo implode( ' > ', array_reverse( $crumbs ) ); } ...


-1

Ok managed to get the children and parents to loop out correctly. $catid = get_query_var( 'cat' ); while( $catid ) { $cat = get_category( $catid ); // Get the object for the catid. $catid = $cat->category_parent; // assign parent ID (if exists) to $catid // the while loop will continue whilst there is a $catid // when there is no longer ...


0

This query will help you global $wpdb; $cat_id = 65; // this query will get parent relationships from term_taxonomy table and get category names from terms table $category = $wpdb->get_row( "SELECT t4.term_id as parent_id, t4.name as parent_name, t5.term_id as grandparent_id, t5.name as grandparent_name FROM `{$wpdb->prefix}term_taxonomy` t1 ...


1

I believe there is no such "All Categories" archive page natively (because it would be just like the blog page - since every post have a category) . I recommend you to create a page template copying that code and modifying the query to include all posts and then creating a page with the slug 'category'. <?php /* * Template Name: Category */ ?> ...


0

Yo don't need to write a plugin for that - what you need already exists. Check this link: https://wordpress.org/plugins/list-categories/ With this plugin you can display categories in any post or page. Of course, maybe you want to write your own plugin, and in that case this link can be helpful. I hope that this will help you to speed up a process.


0

I've modified your code to reflect the wp_list_categories() function reference as suggested by @Usce. This snippet will create an unordered list, remove the "Categories" list title, and add the li class with individual category ID so you can target each link in the list. function catting ($atts, $content = null) { echo '<div id="cats" ...


-1

There is build in wordpress function for that , so you can use wp_list_categories() for that. Please refer here to learn more And after you display them you an do some CSS if you want to display it as buttons. I hope this helps. Kind regards, Usce



Top 50 recent answers are included