add_filter( 'woocommerce_product_filters', 'wpninja_filter_by_custom_taxonomy_dashboard_products' ); function wpninja_filter_by_custom_taxonomy_dashboard_products( $output ) { global $wp_query; $output .= wc_product_dropdown_categories( array( 'show_option_none' => 'Filter by product tag', 'taxonomy' => 'product_tag', 'name' => 'product_tag', 'selected' => isset( $wp_query->query_vars['product_tag'] ) ? $wp_query->query_vars['product_tag'] : '', ) ); return $output; }
Add class only to clicked element with JS
jQuery(document).on('click', function() { jQuery('.collapse').removeClass("show"); }); jQuery(".collapse").click(function(event) { // remove classes from all jQuery('.show').removeClass('show'); // add class to the one we clicked jQuery('.collapse', this).addBack(this).addClass("show"); event.stopPropagation(); });
How to set featured image as background cover
Echo the return value from wp_get_attachment_image_src(). It also returns an Array(), so you need to grab the needed part from that array. In this case it’s the first/0 value. Example:
<?php $bgImage = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); ?>
Include tha variable inside the inline stylesheet
<section class="contact-cover" style=" background: url('<?php echo $bgImage[0]; ?>'); background-size: cover; background-attachment: fixed; padding: 60px 0;"> </section>
Social Icons SVG
jQuery Horizontal Carousel/Tabs
Add featured image to Genesis single post
Bootstrap Slider with Custom Post Type
<div id="slider-wrapper" class="container-fluid"> <div id="mySlider" class="carousel slide" data-ride="carousel"> <ol class="carousel-indicators"> <li data-target="#mySlider" data-slide-to="0" class="active"></li> <li data-target="#mySlider" data-slide-to="1"></li> <li data-target="#mySlider" data-slide-to="2"></li> <li data-target="#mySlider" data-slide-to="3"></li> </ol> <div class="carousel-inner" role="listbox"> <!--LOOP ACTIVE ITEMS--> <?php wp_reset_postdata(); $the_query = new WP_Query(array( 'post_type' => 'slider', 'posts_per_page' => 1 )); while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <div class="carousel-item active"> <img src="<?php the_field('slide'); ?>" alt="<?php the_field('caption_title'); ?>"> <div class="carousel-caption"> <h2><?php the_title(); ?></h2> <p class="hidden"><?php the_field('caption_text'); ?></p> </div> </div><!--item-active--> <?php endwhile; wp_reset_postdata(); ?> <!--LOOP ITEMS--> <?php $the_query = new WP_Query(array( 'post_type' => 'slider', 'posts_per_page' => 2, 'offset' => 1 )); while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <div class="carousel-item"> <img src="<?php the_field('slide'); ?>" alt="<?php the_field('caption_title'); ?>"> <div class="carousel-caption"> <h2><?php the_title(); ?></h2> <p class="hidden"><?php the_field('caption_text'); ?></p> </div> </div><!--item-active--> <?php endwhile; wp_reset_postdata(); ?> </div> <div class="carousel-control-wrapper hidden"> <a class="carousel-control-prev" data-target="#mySlider" role="button" data-slide="prev"> <span class="carousel-control-prev-icon" aria-hidden="true"></span> <span class="sr-only">Previous</span> </a> <a class="carousel-control-next" data-target="#mySlider" role="button" data-slide="next"> <span class="carousel-control-next-icon" aria-hidden="true"></span> <span class="sr-only">Next</span> </a> </div> </div> </div>
How to add True / False checkbox with ACF
<?php if( get_field('field') ){ echo ''; } else{ echo ''; } ?>
How to echo CPT taxonomy name and slug
Echo taxonomy slug
<?php foreach (get_the_terms(get_the_ID(), 'your_taxonomy') as $cat) { echo $cat->slug; } ?>
Echo taxonomy name
<?php foreach (get_the_terms(get_the_ID(), 'your_taxonomy') as $cat_name) { echo $cat_name->name; } ?>
Loop Custom Post Type Sub Category
<?php wp_reset_postdata(); $args = array( 'post_type' => 'posttype', 'posts_per_page' => 20, 'tax_query' => array( array( 'taxonomy' => 'taxonomy', 'field' => 'slug', 'terms' => 'subcategory', ), ), ); $query = new WP_Query( $args ); ?> <?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?> <?php endwhile; endif; ?> <?php wp_reset_postdata(); ?>
How to get post title slug
<?php global $post; $post_slug=$post->post_name; ?>
jQuery Custom Events – click event- add / remove classes
Example
<div class="room" id="kitchen"> <div class="lightbulb on"></div> <div class="switch"></div> <div class="switch"></div> <div class="clapper"></div> </div>
$( ".switch, .clapper" ).click(function() { var light = $( this ).closest( ".room" ).find( ".lightbulb" ); if ( light.is( ".on" ) ) { light.removeClass( "on" ).addClass( "off" ); } else { light.removeClass( "off" ).addClass( "on" ); } });
$( ".lightbulb" ).on( "light:toggle", function( event ) { var light = $( this ); if ( light.is( ".on" ) ) { light.removeClass( "on" ).addClass( "off" ); } else { light.removeClass( "off" ).addClass( "on" ); } }); $( ".switch, .clapper" ).click(function() { var room = $( this ).closest( ".room" ); room.find( ".lightbulb" ).trigger( "light:toggle" ); });
Enqueue Bootstrap CDN in WordPress
// ========================================================================= // BOOTSTRAP CSS // ========================================================================= function enqueue_bootstrap_styles(){ wp_enqueue_style('bootstrap_css', '//stackpath.bootstrapcdn.com/bootstrap/4.4.0/css/bootstrap.min.css'); } add_action( 'wp_enqueue_scripts', 'enqueue_bootstrap_styles' ); // ========================================================================= // BOOTSTRAP JAVASCRIPT // ========================================================================= function enqueue_bootstrap_scripts() { wp_enqueue_script( 'bootstrap_jquery', '//code.jquery.com/jquery-3.4.1.slim.min.js', array(), '3.3.1', true ); wp_enqueue_script( 'bootstrap_popper', '//cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js', array(), '1.14.7', true ); wp_enqueue_script( 'bootstrap_javascript', '//stackpath.bootstrapcdn.com/bootstrap/4.4.0/js/bootstrap.min.js', array(), '4.3.1', true ); } add_action( 'wp_enqueue_scripts', 'enqueue_bootstrap_scripts' );
How to Move Genesis Nav Above the Header
//How to Move Genesis Nav Above the Header remove_action( 'genesis_after_header', 'genesis_do_nav' ); add_action( 'genesis_before_header', 'genesis_do_nav' );
Contact Form 7 Placeholder Hide on Focus
jQuery(document).ready(function(){ jQuery('input,textarea').focus(function(){ jQuery(this).data('placeholder',jQuery(this).attr('placeholder')) .attr('placeholder',''); }).blur(function(){ jQuery(this).attr('placeholder',jQuery(this).data('placeholder')); }); });
Syntax of CF7 placeholder
[text* your-name placeholder "NAME"]
CSS for placeholder
::-webkit-input-placeholder { /* WebKit browsers */ color: #fff !important; opacity: 1; } :-moz-placeholder { /* Mozilla Firefox 4 to 18 */ color: #fff !important; opacity: 1; } ::-moz-placeholder { /* Mozilla Firefox 19+ */ color: #fff !important; opacity: 1; } :-ms-input-placeholder { /* Internet Explorer 10+ */ color: #fff !important; opacity: 1; }
Search by product SKU
/** * Search by product SKU */ function wpninja_product_search_sku( $join, $query ) { if ( ! $query->is_main_query() || is_admin() || ! is_search() || ! is_woocommerce() ) { return $join; } global $wpdb; $join .= " LEFT JOIN {$wpdb->postmeta} wpninja_post_meta ON {$wpdb->posts}.ID = wpninja_post_meta.post_id "; return $join; } add_filter( 'posts_join', 'wpninja_product_search_sku', 10, 2 ); /** * Modify the search query with posts_where. */ function wpninja_product_search_where( $where, $query ) { if ( ! $query->is_main_query() || is_admin() || ! is_search() || ! is_woocommerce() ) { return $where; } global $wpdb; $where = preg_replace( "/\(\s*{$wpdb->posts}.post_title\s+LIKE\s*(\'[^\']+\')\s*\)/", "({$wpdb->posts}.post_title LIKE $1) OR (wpninja_post_meta.meta_key = '_sku' AND wpninja_post_meta.meta_value LIKE $1)", $where ); return $where; } add_filter( 'posts_where', 'wpninja_product_search_where', 10, 2 );
Show SKU above the product title
//Show SKU above the product title function fdesign_show_sku(){ global $product; echo '<small>Cikkszám:</small> <small>' . $product->get_sku() .'</small>'; } add_action( 'woocommerce_before_single_product_summary', 'fdesign_show_sku', 5 );
Show Product Search in Header
//Show Product Search in Header function new_nav_menu_items($items, $args) { $my_product_search = get_product_search_form( $echo ); if($args->theme_location == 'main_navigation'){ $items = $items . $my_product_search; } return $items; } add_filter('wp_nav_menu_items', 'new_nav_menu_items', 10, 2);
Loop Popular Posts
Add this snippet to functions.php
function shapeSpace_popular_posts($post_id) { $count_key = 'popular_posts'; $count = get_post_meta($post_id, $count_key, true); if ($count == '') { $count = 0; delete_post_meta($post_id, $count_key); add_post_meta($post_id, $count_key, '0'); } else { $count++; update_post_meta($post_id, $count_key, $count); } } function shapeSpace_track_posts($post_id) { if (!is_single()) return; if (empty($post_id)) { global $post; $post_id = $post->ID; } shapeSpace_popular_posts($post_id); } add_action('wp_head', 'shapeSpace_track_posts');
Displaying Popular Posts
<h3>Legnépszerűbb bejegyzések</h3> <?php $args = array( 'posts_per_page'=>3, 'meta_key'=>'popular_posts', 'orderby'=>'meta_value_num', 'order'=>'DESC' ); $popular = new WP_Query( $args ); ?> <div class="popular-posts"> <?php while ($popular->have_posts()) : $popular->the_post(); ?> <div class="row"> <div class="col-md-4"> <?php the_post_thumbnail( 'full', array('class' => 'img-responsive ') ); ?> </div> <div class="col-md-8"> <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4> <time class="entry-date"><?php the_date('Y-m-d'); ?></time> </div> </div> <?php endwhile; wp_reset_postdata(); ?> </div>
Example of the comments_template() filter
/* * * Let's us override the default comment template * located at wp-includes/comment-template.php and * use our own comment file instead. * */ function custom_comment_template( $comment_template ) { return dirname(__FILE__) . '/inc/custom-comment-template.php'; } add_filter( 'comments_template', 'custom_comment_template' );