All posts by acrane

About acrane

A web designer from Charlotte, NC, I enjoy working in the yard, kayaking, building stuff, making things, creating, thinking and of course WordPress.

Remove options from the sort by for Woocommerce products page.

You can remove certain ways to sort product by simply unsetting a few options from the sort menu. Add the following to your theme’s functions.php page. The available options are listed in the comments above the function.

// Modify the default WooCommerce orderby dropdown
//
// Options: menu_order, popularity, rating, date, price, price-desc
function my_woocommerce_catalog_orderby( $orderby ) {
    unset($orderby["rating"]);
    unset($orderby["popularity"]);
    return $orderby;
}
add_filter( "woocommerce_catalog_orderby", "my_woocommerce_catalog_orderby", 20 );

Example Woocommerce Flexslider Slider Loop with Categories

Example of a Loop within a loop using Woocommerce categories and it’s related products. Loops through the categories, shows the product category image, then shows three products related to that category. As an added bonus, there is also a query to only pull the products with the Advanced Custom Field of, “yes” when referring to “Show on Homepage Slider?” (feature_on_homepage_slider)

This example is just the flexslider loop. It does not show how to implement the flexslider.

$number, 'orderby' => $orderby, 'order' => $order, 'hide_empty' => $hide_empty, 'include' => $ids, 'parent' => 0 ); $product_categories = get_terms( 'product_cat', $args ); ?>
  • term_id, 'thumbnail_id', true ); // get the image URL $image = wp_get_attachment_url( $thumbnail_id ); ?> slug; $args = array( 'post_type' => 'product', 'posts_per_page' => 3, 'product_cat' => $productCat, // which product category to pull // we can also query only those chosen with an Advanced Custom Field of "Yes" 'meta_query' => array( array( 'key' => 'feature_on_homepage_slider', // Custom field radio button 'value' => 'yes', // default set to no, so they have to check yes. 'compare' => '=', 'type' => 'CHAR' ) ) ); $loop = new WP_Query( $args ); if ( $loop->have_posts() ) : ?>
    have_posts() ) : $loop->the_post(); ?>
    $

Get the Terms for a Post or Custom Taxonomy

Get the Custom Taxonomy Term assigned to your posts.

$taxonomy  = 'leadershiptype';
$terms = get_the_terms( $post->ID, $taxonomy );
if ( !empty( $terms ) ){
    // get the first term
    $term = array_shift( $terms );
    
}

Then you can do cool things and echo stuff.

// Echo the slug
echo $term->slug; 

// Echo the Name
echo $term->name; 

// Echo the Description
echo $term->description;  

Ect, Ect…

Using the Advanced Custom Fields Date Picker for Ordering Posts and Displaying the Date

How to use the date picker with Advanced Custom Fields to order your posts and show the date. With this example you can show the start date and if it’s a multiple day event, it will show the end date if you have one. It will also not show expired dates.

First, add two Custom Fields to your posts called, Start Date (start_date) and End Date (end_date).

Fill out a couple test posts. Some with just a start date and some with a start and end date.

Here’s the code:

Custom Post Type Query with Advanced Custom Field Check

Say you want to query a Custom post Type, but want to see if an Advanced Custom Field is checked, and just filter those posts. In this example, we want to show testimonials on the homepage, but only if the “Show on Homepage” custom field is checked, “yes”.

Here it is:

 'testimonials', // Custom Post Type name
	'posts_per_page' => '3',
	'meta_query' => array(
                     array(
                        'key' => 'show_on_homepage', // Custom field radio button
                        'value' => 'yes', // default set to no, so they have to check yes.
                        'compare' => '=', 
                        'type'      => 'CHAR'
                      )
                 )
);
$the_query = new WP_Query( $args ); ?>
have_posts() ) : ?>

  • Custom WordPress Login with Custom Sizes

    This function will not only give you a custom login logo for your site, but it will also allow you to control the custom size you want.

    Place the following in your themes functions.php file.

    function my_login_logo() { ?>
    
    

    Obviously you'll want to put in your own sizes. And change the image name if you need to.

    jQuery hidden div system

    Simple setup that will hide and show divs…

    Link1
    Link2
    Link3
    Link4
    Div 1
    Div 2
    Div 3
    Div 4
    $(".myLink").bind("click", function() {
        $(".myDiv").hide();
        var divId= $(this).attr("divId");
        $("#" + divId).show();
    });
    
    // Or with active nav toggles
    
    $(".myLink").bind("click", function() {
     $(".hidden-div").hide();
     // deactivate the active class for the bubble
     $(".myLink").removeClass('active');
     var divId= $(this).attr("divId");
     $("#" + divId).show();
     // activate the active class for the bubble
     $(this).addClass('active');
    });

    Then you can setup a loop to do it automagically with the WordPress loop or the Advanced Custom Fields loop. Here is one that uses the Advanced Custom Fields repeater loop.

    First, create your list items to link to the hidden divs. Start a counter to have unique div names.

    <?php // Loop through the custom field repeater field.
       if(get_field('our_team')): $i = 0; ?>
      <?php while(has_sub_field('our_team')): $i++; ?>
    
        <li>
           <a class="myLink" href="javascript:void(0);" divId="div-<?php echo $i; ?>">
                <?php the_sub_field('name'); ?>
           </a>
        </li>
       
     <?php endwhile; ?>
      <?php endif; // end of get field ?>

     

    Then loop through the divs the same way and add the counter…

    <?php // Loop through the custom field repeater field.
       if(get_field('our_team')): $i = 0; ?>
      <?php while(has_sub_field('our_team')): $i++; ?>
    
          <div class="myDiv" id="div-<?php echo $i; ?>">Div 1</div>
    
     <?php endwhile; ?>
      <?php endif; // end of get field ?>