WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Nov 16 2011

How to Get All the Children of a Specific Nav Menu Item

***UPDATED (12/6/2011): I have just updated this function to allow for basic depth (all or none).

Recently, I needed to get all the children (along with the parent) of a particular nav menu item. Since pages can do this very well, I looked into the function get_page_children(). So I modified that function to create get_nav_menu_item_children(). I have recommended this to WordPress core (ticket). What are your thoughts?

[php]
/**
* Returns all child nav_menu_items under a specific parent
*
* @param int the parent nav_menu_item ID
* @param array nav_menu_items
* @param bool gives all children or direct children only
* @return array returns filtered array of nav_menu_items
*/
function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) {
$nav_menu_item_list = array();
foreach ( (array) $nav_menu_items as $nav_menu_item ) {
if ( $nav_menu_item->menu_item_parent == $parent_id ) {
$nav_menu_item_list[] = $nav_menu_item;
if ( $depth ) {
if ( $children = get_nav_menu_item_children( $nav_menu_item->ID, $nav_menu_items ) )
$nav_menu_item_list = array_merge( $nav_menu_item_list, $children );
}
}
}
return $nav_menu_item_list;
}
[/php]

Written by Travis Smith · Categorized: WordPress

Nov 14 2011

in_taxonomy(): Check to Determine Whether a Post Is in a Specific Custom/Builtin Taxonomy

I found this great code snippet by Alex Leonard.

[php]
<?php

/**
* Conditional function to check if post belongs to term in a custom taxonomy.
*
* @param tax string taxonomy to which the term belons
* @param term int|string|array attributes of shortcode
* @param _post int post id to be checked
* @return BOOL True if term is matched, false otherwise
*/
function wps_in_taxonomy($tax, $term, $_post = NULL) {

// if neither tax nor term are specified, return false
if ( !$tax || !$term ) { return FALSE; }

// if post parameter is given, get it, otherwise use $GLOBALS to get post
if ( $_post ) {
$_post = get_post( $_post );
} else {
$_post =& $GLOBALS['post'];
}

// if no post return false
if ( !$_post ) { return FALSE; }

// check whether post matches term belongin to tax
$return = is_object_in_term( $_post->ID, $tax, $term );

// if error returned, then return false
if ( is_wp_error( $return ) ) { return FALSE; }

return $return;
}
[/php]

Written by Travis Smith · Categorized: WordPress

Nov 12 2011

How to Add Post Tags and Categories to Pages in WordPress

You can add categories to pages, which is fairly simple. To do this, simply add the following to the end of your functions.php file:

[php]
add_action( 'init' , 'wps_add_cats_to_page' );
function wps_add_cats_to_page() {
register_taxonomy_for_object_type('post_tag', 'page');
register_taxonomy_for_object_type('category', 'page');
}
[/php]

Written by Travis Smith · Categorized: WordPress

Oct 24 2011

How to Check to See if You Have the Most Recent/Latest Post

Add this code to your functions.php file:
[php]<?php
/**
* Checks to see if the post id is the most recent.
*
* @param string Post ID.
* @return boolean true/false
*
*/
function wps_check_latest( $post_id ) {
$args = array(
'numberposts' => 1,
'offset' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);

$latestpost = get_posts( $args );

if ( $latestpost[0]->ID == $post_id )
return true;
else
return false;
}
[/php]

To use you would use like this:
[php]
global $post;
if ( wps_check_latest( $post->ID ) )
//do something
else
//do something else
[/php]

Written by Travis Smith · Categorized: WordPress

Sep 29 2011

How to Include Another Post Type and Custom Taxonomy on the Genesis Blog Page

So recently, someone asked me to help them assimilate two post types on the Genesis blog page. Since because of the Genesis loops, this becomes quite easy. So here's what we need to do:

  1. Setup the Template Name
  2. Create the Custom Loop
  3. Modify the Post Meta
  4. Customize the Content Display

Setup the Template Name

First and as always, we need to set up the template. You can start by scratch, which this tutorial will do, or you can copy page_blog.php from the genesis folder.
[php]<?php
/**

Template Name: Blog

*/
[/php]

You may want to change the template name to something different so there aren't any confusions with Genesis's version. So something like this would work:
[php]<?php
/**

Template Name: Custom Blog

*/
[/php]

Create the Custom Loop

Now, I have a site with multiple custom post types and what I want to do is to combine two of those post types (post and wps_videos) into the blog feed. Then I want to limit them based on the Genesis category settings as well as incorporate the Video Category (wps_vidcats) in the same manner.

So first, I have to unhook the genesis standard loop.
[php]
remove_action('genesis_loop', 'genesis_do_loop');
[/php]

Then I add my own loop with its custom function.
[php]
add_action( 'genesis_loop', 'wps_custom_do_loop' , 5 );
function wps_custom_do_loop() {
$include = genesis_get_option( 'blog_cat' );
$exclude = genesis_get_option( 'blog_cat_exclude' ) ? explode( ',' , str_replace ( ' ' , '' , genesis_get_option( 'blog_cat_exclude' ) ) ) : '';
[/php]

These lines are right out of the Genesis standard loop. They pull the information set on the Theme Settings page and place them in variables to be used later. However, these are limited to just categories. Now, it is important to note that $include only contains one category id and $exclude contains an array of categories (even if there is only one).

Now, I need to build my $args. In my args, I want to add my post types as well as build my taxonomy query. So continuing inside wps_custom_do_loop:
[php]
$args = array(
'showposts' => genesis_get_option('blog_cat_num'),
'post_type' => array( 'post' , 'wps_videos' ),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'wps_vidcategory',
'field' => 'id',
'terms' => array( 92 )
),
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array( $include )
),
)
);
[/php]

So let me break this down for you. 'showposts' pulls from the Genesis Theme Settings page. 'post_type' is where we add the registered names of all the post types we would like to include in an array. 'tax_query' is where I limit the query based on my requirements. Now I wanted to only include posts from one category, which was set on the Genesis Theme Settings page, and I also wanted to do likewise with my videos and include videos from one specific Video Category (here term ID of 92). And since posts and videos do not share the same taxonomy, the tax_query is simple. So, in summary, I wanted posts from one category ($include) OR (and that's my relation argument) posts from my custom taxonomy video category (here 92).

Now, if I wanted, I could replace the last array with a different array to allow for all categories except (again based on category ids entered in Genesis Theme Settings). It would look something like this:
[php]
$args = array(
'showposts' => genesis_get_option('blog_cat_num'),
'post_type' => array( 'post' , 'wps_videos' ),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'wps_vidcategory',
'field' => 'id',
'terms' => array( 92 )
),
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $exclude,
'operator' => 'NOT IN'
),
)
);
[/php]

Now, we can finish the function by calling the Genesis Custom Loop.
[php]

genesis_custom_loop( $args );
}
[/php]

So all together the function looks like this (to simply limit posts to one category and vids from another category):
[php]<?php
remove_action('genesis_loop', 'genesis_do_loop');
add_action( 'genesis_loop', 'custom_do_loop' , 5 );

function custom_do_loop() {
$include = genesis_get_option('blog_cat');
$exclude = genesis_get_option('blog_cat_exclude') ? explode(',', str_replace(' ', '', genesis_get_option('blog_cat_exclude'))) : '';
$cf = genesis_get_custom_field('query_args'); /** Easter Egg **/
$args = array(
'showposts' => genesis_get_option('blog_cat_num'),
'post_type' => array( 'post' , 'wps_videos' ),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'wps_vidcategory',
'field' => 'id',
'terms' => array( 92 )
),
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array( $include )
),
)
);
genesis_custom_loop( $args );
}
[/php]

Modify the Post Meta

Now, because my videos do not share the same taxonomy with posts (and it could have [dare I say, should have] but the client didn't want it that way). My posts on the blog page that are from the videos custom post type won't have any post meta. I could simply remove it, but since the client wanted it, I simply filter them.

To filter the post meta to include terms from categories and my video categories, simply do a basic if-then statement. Normally Genesis has $post_meta = '[ post_categories] [ post_tags]';. However, I wanted to change it to just display categories.
[php]<?php
/** Customize the post meta function */
add_filter( 'genesis_post_meta' , 'post_meta_filter' );
function post_meta_filter( $post_meta ) {
global $post;
if ( $post->post_type == 'wps_videos' ) {
$vid_cats = get_the_term_list( $post->ID, 'wps_vidcategory' , '' , ', ' , '' );
$post_meta = sprintf( '<span class="categories">%2$s%1$s</span> ', $vid_cats, 'Filed Under: ' );
}
else {
$post_meta = 'Filed Under: Custom Post Types, Genesis, Tutorials, WordPress';
}
return $post_meta;
}
[/php]

Customize the Content Display

Now, because of UX, the video custom post type had a metabox to make displaying the videos consistent. However, if we do nothing then the videos never appear, which is not what we want.

First, I must unhook the standard Genesis content function.
[php]
/** Customize the content */
remove_action( 'genesis_post_content', 'genesis_do_post_content' );
[/php]

Second, I write a basic if-then function to display my videos on the blog page.
[php]<?php
add_action( 'genesis_post_content', 'custom_do_post_content' );
function custom_do_post_content() {
global $post;
if ( $post->post_type == 'wps_videos') {
if( genesis_get_custom_field('_wps_videoembedcode') != '' ) {
?>
<div id="post-<?php the_ID(); ?>" class="video-entry">
<div class="video-embed">
<?php echo genesis_get_custom_field('_wps_videoembedcode'); ?>
</div><!-- end .video-embed -->
<div class="clear"></div>
<?php genesis_do_post_content(); ?>
</div><!-- end .video-entry -->
<?php
}
} //end wps_videos
else {
genesis_do_post_content();
}
}
[/php]

Now, in both, I simply referred back to the genesis_do_post_content() because I just wanted a way to insert my videos for posts with the video custom post type.

Now as with any Genesis template, to initiate the framework, we have to add the Genesis call.
[php]<?php
genesis();
?>[/php]

All Together

[php]<?php
/**

Template Name: Custom Blog

*/

// Custom Loop
remove_action('genesis_loop', 'genesis_do_loop');
add_action( 'genesis_loop', 'custom_do_loop' , 5 );
function custom_do_loop() {
$include = genesis_get_option('blog_cat');
$exclude = genesis_get_option('blog_cat_exclude') ? explode(',', str_replace(' ', '', genesis_get_option('blog_cat_exclude'))) : '';
$cf = genesis_get_custom_field('query_args'); /** Easter Egg **/
$args = array(
'showposts' => genesis_get_option('blog_cat_num'),
'post_type' => array( 'post' , 'wps_videos' ),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'wps_vidcategory',
'field' => 'id',
'terms' => array( 92 )
),
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array( $include )
),
)
);
genesis_custom_loop( $args );
}

// Customize the post meta function
add_filter( 'genesis_post_meta' , 'post_meta_filter' );
function post_meta_filter( $post_meta ) {
global $post;
if ( $post->post_type == 'wps_videos' ) {
$vid_cats = get_the_term_list( $post->ID, 'wps_vidcategory' , '' , ', ' , '' );
$post_meta = sprintf( '<span class="categories">%2$s%1$s</span> ', $vid_cats, 'Filed Under: ' );
}
else {
$post_meta = 'Filed Under: Custom Post Types, Genesis, Tutorials, WordPress';
}
return $post_meta;
}

// Customize the content
remove_action( 'genesis_post_content', 'genesis_do_post_content' );
add_action( 'genesis_post_content', 'custom_do_post_content' );
function custom_do_post_content() {
global $post;
if ( $post->post_type == 'wps_videos') {
if( genesis_get_custom_field('_wps_videoembedcode') != '' ) {
?>
<div id="post-<?php the_ID(); ?>" class="video-entry">
<div class="video-embed">
<?php echo genesis_get_custom_field('_wps_videoembedcode'); ?>
</div><!-- end .video-embed -->
<div class="clear"></div>
<?php genesis_do_post_content(); ?>
</div><!-- end .video-entry -->
<?php
}
} //end wps_videos
else {
genesis_do_post_content();
}
}

genesis();
?>
[/php]

Written by Travis Smith · Categorized: Custom Post Types, Genesis, Tutorials, WordPress

  • « Previous Page
  • 1
  • …
  • 16
  • 17
  • 18
  • 19
  • 20
  • …
  • 25
  • Next Page »
  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in