Auto Embed Videos with Advanced Custom Fields

How to auto embed using Advanced Custom Fields so the client doesn’t have to embed. All they have to do is enter the video’s url in the field.

Paste this in your theme where you want to use it.


Obviously, “the_field_name is replaced by the name in how you set up your custom field. And remember, “get_field” becomes “get_sub_field” if you’re dealing with repeaters.

Improved is_tree function. If page is an ancestor. Child, Grandchild…

This site has a function to test if a page is a child or not. Problem is that it only goes one level deep. Use this to go further.

This goes in the functions file.

function is_tree($pid)
{
  global $post;

  $ancestors = get_post_ancestors($post->$pid);
  $root = count($ancestors) - 1;
  $parent = $ancestors[$root];

  if(is_page() && (is_page($pid) || $post->post_parent == $pid || in_array($pid, $ancestors)))
  {
    return true;
  }
  else
  {
    return false;
  }
};

Usage:

if (is_tree(2)) {
   // stuff
}

Expanding Divs with Height Auto jQuery

There is no expand a div to auto height. Cool trick to achieve this is with a max-height. Set it to 0 in the css, then expand with jQuery. The div will take the height you’re looking for.

CSS:

.expand-this-div {
	width: 170px;
	float:right;
	max-height: 0px;
	overflow: hidden;	
}

jQuery:

$('.expand-this-div').animate({ "max-height": "1000" }, 'slow');

And then to toggle that:

$('.expand-this-div').hover(
function(){
  $(this).animate({ "max-height": "1000" }, 'fast');
},
function() {
  $(this).animate({ "max-height": "0" }, 'fast');
});

 

 

Remove Sub Menu items from WordPress Admin’s Left Side

Sometimes you want to use a custom taxonomy as a way for the client to choose one or a couple of their custom post types as a Highlighted post, or a Hero Project, or a Show on Front Page post. When setting this up, there is really no reason to add terms to the custom taxonomy because it’s usually a “yes” or “no”. So we really don’t need it as a Admin sub menu as it just adds to the clutter.

Let’s remove it.

In this instance we are wanting to remove our custom taxonomy registered to our custom post type of “projects”. But don’t be alarmed when you first throw this in your functions file. you will be looking for the array # to your custom taxonomy.

add_action('admin_menu', 'remove_some_stuff');
function remove_some_stuff() {
    global $submenu;
    unset($submenu['edit.php?post_type=projects'][18]);
    print_r($submenu); exit;
}

Once you find it, in my case it was 18, you can comment out the last line. Like this:

add_action('admin_menu', 'remove_some_stuff');
function remove_some_stuff() {
    global $submenu;
    unset($submenu['edit.php?post_type=projects'][18]);
    //print_r($submenu); exit;
}

How to paginate a custom query on the single.php file

Say you’re viewing a Custom Post type of “Events” using, “single-events.php” Paginating won’t work because the canonical redirects strip the “/page/2/ for your url. What that basically means is that WordPress tries to find the right post even if the url entered is slightly wrong. So, it strips what you need to paginate. We can override that with a simple function:

// Disable canonical redirects on the events so we can paginate  a single post view.
add_filter('redirect_canonical','simple_disable_redirect_canonical');

function simple_disable_redirect_canonical($redirect_url) {
if (is_singular('my_custom_posttype')) $redirect_url = false; // change 'my_custom_posttype' , obviously
return $redirect_url;
}

Advanced Custom Fields Get Field from Custom Taxonomy on taxonomy.php dynamically

Yep, long title. But what we are doing here is pulling an advanced custom field that is linked to a custom taxonomy and setting it up to be dynamic on the taxonomy template page.

The example here will show you how to check for the “Alternate Title” field on a Custom Taxonomy, and if not empty to display it within the current taxonomy. If empty, show the title. The advanced Custom Field requires the registered name of the taxonomy, an underscore, and the id number of the tax.

You can always create a special template off the taxonomy.php For example, you could have a taxonomy of “eventtypes”. So you could create a page template called, “taxonomy-eventtypes.php” and within that template, you could write your title like this:

But you can get carried away with lots of different templates covering all your custom taxonomies. So how do we make it dynamic? You can do that with the queried_object and extract the term_id and the taxonomy. That way all you’d have to have is one template, “taxonomy.php” and the rest is dynamic. Example:

queried_object->term_id;
	$taxonomy = $wp_query->queried_object->taxonomy;  
?>   
     
     

Style posts differently with a counter

You can add different classes to your posts within the loop so that you can style them differently. A good example would be that you have 3 posts on a line where the last post needs a margin of 0 to line up with the edge of the container. Or, style every other one or every third one a different color. Choices are limitless. You can change the number below to target which posts you want to style differently.

<?php
  ++$counter;
  if($counter == 3) { // change number to pick which one is going to be different
    $postclass = 'third-post';
    $counter = 0;
  } else { $postclass = 'other-post-cat'; }
?>

 

Here is an example of a normal loop and styling every other post:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
  ++$counter;
  if($counter == 2) { // "2" to style every other post
    $postclass = 'second-post';
    $counter = 0;
  } else { $postclass = 'other-post'; }
?>
<div class="blogentry-tax <?php echo $postclass; ?>">
<h2><?php the_title(); ?></h2>
</div>
<?php endwhile; endif; ?>

 

Here is an example of a Custom Query when viewing a Custom Taxonomy and styling every third post:

<?php
$current_query = $wp_query->query_vars;
$wp_query = new WP_Query();
$wp_query->query(array(
	$current_query['taxonomy'] => $current_query['term'],
	'paged' => $paged,
	'posts_per_page' => 10,
));

while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

<?php
  ++$counter;
  if($counter == 3) {
    $postclass = 'third-post';
    $counter = 0;
  } else { $postclass = 'other-post'; }
?>

<div class="blogentry-tax <?php echo $postclass; ?>">
     <?php the_title(); ?>
</div>

<?php endwhile; wp_reset_postdata(); ?>