├── .gitignore
├── add-extra-css-class-to-the-anchor-element-in-nav-menu.md
├── add-extra-css-class-to-the-li-element-in-nav-menu.md
├── fix-no-order-found-bug-for-woocommerce.md
├── get-posts-by-category-name.md
├── get-rid-of-nagging-woocommerce-admin-notice.md
└── get-single-category-name-of-a-post.md
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
--------------------------------------------------------------------------------
/add-extra-css-class-to-the-anchor-element-in-nav-menu.md:
--------------------------------------------------------------------------------
1 | # Add extra css class to the anchor tag in nav menu
2 |
3 | You may run into situations where the `` tag requires some extra css class in your nav menu, but the function which renders the menu leaves you disappointed because there is no default way to do that, except using a special filter. So let's do that
4 |
5 | ```php
6 | function set_anchor_class( $class = '', $location = 'primary', $append = false ) {
7 | add_filter( 'nav_menu_link_attributes', function ( $atts, $item, $args ) use ( $class, $append, $location ) {
8 | if ( !$append ) {
9 | $atts['class'] = $class;
10 | } else {
11 | $atts['class'] .= " " . $class;
12 | }
13 | return $atts;
14 | }, 10, 3 );
15 | }
16 | ```
17 |
18 | Instead of adding css class to every `` tag found in every `theme_location` we can also restrict it to a specific location, like this, which will work only for the `primary` theme location.
19 |
20 | ```php
21 | function set_anchor_class( $class = '', $location = 'primary', $append = false ) {
22 | add_filter( 'nav_menu_link_attributes', function ( $atts, $item, $args ) use ( $class, $append, $location ) {
23 | if ( $args->theme_location == $location ) {
24 | if ( !$append ) {
25 | $atts['class'] = $class;
26 | } else {
27 | $atts['class'] .= " " . $class;
28 | }
29 | }
30 | return $atts;
31 | }, 10, 3 );
32 | }
33 | ```
34 |
35 | :) Nice, eh?
--------------------------------------------------------------------------------
/add-extra-css-class-to-the-li-element-in-nav-menu.md:
--------------------------------------------------------------------------------
1 | # Add extra css class to the li tag in nav menu
2 |
3 | You may run into situations where the `` tag requires some extra css class in your nav menu, but the function which renders the menu leaves you disappointed because there is no default way to do that, except using a special filter. So let's do that
4 |
5 | ```php
6 | function set_li_class( $class = '', $location = 'primary' ) {
7 | add_filter( 'nav_menu_css_class', function ( $classes, $item, $args ) use ( $class, $location ) {
8 | $classes[] = $class;
9 | return $classes;
10 | }, 10, 3 );
11 | }
12 | ```
13 |
14 | Instead of adding css class to every `` tag found in every `theme_location` we can also restrict it to a specific location, like this, which will work only for the `primary` theme location.
15 |
16 | ```php
17 | function set_li_class( $class = '', $location = 'primary' ) {
18 | add_filter( 'nav_menu_css_class', function ( $classes, $item, $args ) use ( $class, $location ) {
19 | if ( $args->theme_location == $location ) {
20 | $classes[] = $class;
21 | }
22 | return $classes;
23 | }, 10, 3 );
24 | }
25 | ```
26 |
27 | That was not tough, or was it?
--------------------------------------------------------------------------------
/fix-no-order-found-bug-for-woocommerce.md:
--------------------------------------------------------------------------------
1 | # Fix no orders found bug in WooCommerces + WordPress 5x
2 |
3 | There could be many **valid** reasons for a shop owner not updating his WooCommerce to a later version. However, if he/she updates the WordPress core, he could fall into a strange situation when he will find out that no orders are displaying in his WooCommerce admin area. This is frustrating and gives a panic attack. The fix is easy though. Add the following code as a plugin, or inside the `functions.php` of your theme.
4 |
5 | ```php
6 | function fix_request_query_args_for_woocommerce( $query_args ) {
7 | if ( isset( $query_args['post_status'] ) && empty( $query_args['post_status'] ) ) {
8 | unset( $query_args['post_status'] );
9 | }
10 | return $query_args;
11 | }
12 | add_filter( 'request', 'fix_request_query_args_for_woocommerce', 1 );
13 |
14 | ```
15 |
16 | And no more panic attack after that!
--------------------------------------------------------------------------------
/get-posts-by-category-name.md:
--------------------------------------------------------------------------------
1 | # Find posts by category name
2 |
3 | If you need to find posts by category name, you can do that quickly using the following piece of code.
4 |
5 | ```php
6 | $post_query = new WP_Query( array(
7 | 'posts_per_page' => -1, //return all posts
8 | 'category_name' => 'your category name',
9 | ) );
10 |
11 | echo "";
12 | while ($post_query->have_posts()){
13 | $post_query->the_post();
14 | ?>
15 |
16 |
17 | }
18 | echo "
";
19 |
20 | wp_reset_postdata();
21 |
22 | ```
23 |
24 | If you need to find posts by multiple category names, just pass them as csv (comma separated value) and you're done!
25 |
26 | ```php
27 | $post_query = new WP_Query( array(
28 | 'posts_per_page' => -1, //return all posts
29 | 'category_name' => join(',',$categories),
30 | ) );
31 | ```
32 |
33 | :)
--------------------------------------------------------------------------------
/get-rid-of-nagging-woocommerce-admin-notice.md:
--------------------------------------------------------------------------------
1 | # How to Hide WooCommerce Nagging Admin Notice
2 |
3 | If you are not a pro subscriber, you will see this nagging message "connect your woocommerce store blah blah" in your admin area, and it cannot be dismissed. Super annoying I must say. But you can easily get rid of it using the following filter
4 |
5 | ```php
6 | add_filter( 'woocommerce_helper_suppress_admin_notices', '__return_true' );
7 | ```
8 |
9 | Your admin panel is now clean, enjoy.
--------------------------------------------------------------------------------
/get-single-category-name-of-a-post.md:
--------------------------------------------------------------------------------
1 | # Get a single category name of a post
2 |
3 | A post may have multiple categories, and to pick one needs more than one line of code. Besides, there are tricky decisions you need to make how you are going to pick that particular one, a simple array_shift or a random one. Let's get things done!
4 |
5 | ### Get a name, whichever comes first
6 | ```php
7 | function get_single_category( $post_id = null ) {
8 | global $post;
9 | $post_id = is_null( $post_id ) ? $post->ID : $post_id;
10 | $categories = wp_get_post_categories( $post_id, array( 'fields' => 'names' ) );
11 | $single_category = array_shift( $categories );
12 |
13 | return $single_category;
14 | }
15 | ```
16 |
17 | ### Get a random choice
18 | ```php
19 | function get_single_category( $post_id = null ) {
20 | global $post;
21 | $post_id = is_null( $post_id ) ? $post->ID : $post_id;
22 | $categories = wp_get_post_categories( $post_id, array( 'fields' => 'names' ) );
23 | shuffle( $categories );
24 | $single_category = array_shift( $categories );
25 |
26 | return $single_category;
27 | }
28 | ```
29 |
30 | ### Get id, name pair
31 | ```php
32 | function get_single_category( $post_id = null ) {
33 | global $post;
34 | $post_id = is_null( $post_id ) ? $post->ID : $post_id;
35 | $categories = wp_get_post_categories( $post_id, array( 'fields' => 'all' ) );
36 | shuffle( $categories );
37 | $single_category = array_shift( $categories );
38 |
39 | return array($single_category->term_id => $single_category->name);
40 | }
41 | ```
42 |
43 | And that's it! Enjoy.
--------------------------------------------------------------------------------