If you want to add css class to current menu item in WordPress. add_filter(‘nav_menu_css_class’ , ‘rs_special_nav_class’ , 10 , 2); function rs_special_nav_class ($classes, $item) { if (in_array(‘current-menu-parent’, $classes) ){ $classes[] = ‘active-item’; } if (in_array(‘current-menu-item’, $classes) ){ $classes[] = ‘active-item’; } return $classes; }
How to show subcategory inherit the parent category template in WordPress ?
If you want to show subcategory posts in parent category template then you have to include below code in functions.php file. function rs_subcategory_hierarchy() { $category = get_queried_object(); $parent_id = $category->category_parent; $templates = array(); if ( $parent_id == 0 ) { // Use default values from get_category_template() $templates[] = “category-{$category->slug}.php”; $templates[] = “category-{$category->term_id}.php”; $templates[] = ‘category.php’;
Exclude specific category, post, page from Search in WordPress ?
If you want to exclude a specific category or post or page then you have to put the id in below code. function exclude_category_search( $query ) { if ( is_admin() || ! $query->is_main_query() ) return; if ( $query->is_search ) { $query->set( ‘post__not_in’, array( 668,5004,5409,5407,4392,5416,500,502,3817,3819,4708,671) ); $query->set( ‘cat’, ‘-3625,-40’ ); } } add_action( ‘pre_get_posts’, ‘exclude_category_search’, 1
How can you change the version of style.css in WordPress theme?
If you need to change your style.css or any css file after the file edited or modified in wordpress theme then you need to add this code in your functions.php file. if(!is_admin()) wp_enqueue_style( ‘stylesheet-id’, get_stylesheet_directory_uri() . ‘/style.css’, array(), filemtime( get_stylesheet_directory() . ‘/style.css’ ) ); ‘stylesheet-id‘ this id is important please keep in mind if you
How to Stop Google from Indexing your WordPress Image Attachment Pages ?
Just add the below code to your functions.php. First of all this code checks if the attachment is related post or not if related then it redirect to the particular post else it is redirect to your home page. function media_redirect_attachment_page() { if ( is_attachment() ) { global $post; if ( $post && $post->post_parent )
Most commonly functions used in WordPress.
wp_nav_menu() :- Displays a navigation menu. is_front_page() :- Check is front page or not. is_404() :- Check is 404 page or not. is_home() :- Check is blog page or not. is_page() :- to check if this is page OR NOT, will return boolean value. get_the_excerpt() :- Copy the excerpt of the post into a specified
What are the default tables in WordPress ?
At present version, there are about 12 tables in WordPress by default. You can check the number of tables in WordPress by phpMyAdmin. wp_commentmeta wp_comments wp_links wp_options wp_postmeta wp_posts wp_terms wp_termmeta wp_term_relationships wp_term_taxonomy wp_usermeta wp_users
Remove P and Br tags in WordPress posts.
Open functions.php in your currently active theme. remove_filter( ‘the_content’, ‘wpautop’ ); remove_filter( ‘the_excerpt’, ‘wpautop’ );
Optimize WordPress site performance.
Optimization increases the speed of your site and gives the best possible outcome. It improves the ranking of a site. There are a lot of tricks to optimize a WordPress site. Some of them are given below: Use CDN Use Optimized Images Keeps Minified CSS Keeps Minified JS as possible Use a caching plugin Use
Improve security in a WordPress site.
Security in a WordPress site is just as these websites are favorite targets for hackers. A site can’t be perfectly secured but one can at least reduce the risk by applying different security measures. Some of the measures to secure a WordPress site are given below: Username must not be admin Strong password 2 step