WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Nov 20 2011

How to Manually Call Gravity Forms CSS

Here's how to manually call Gravity Forms CSS. For Genesis users, this is important because our widget may call the form up, as in Scribble, but the CSS is not called because the shortcode is never technically called on that page.

[php]
if ( is_home() )
wp_enqueue_style("gforms_css", GFCommon::get_base_url() . "/css/forms.css", null, GFCommon::$version);
[/php]

Written by Travis Smith · Categorized: Genesis, Plugins

Nov 17 2011

How to Automatically Add Custom Taxonomies to a WordPress Menu

While there is probably a better way to do this, here's my first shot at adding custom taxonomies to the menu. Assuming that you have a custom taxonomy created, say 'wps_projects'. What are your thoughts?

[php]
add_action( 'init' , 'wps_add_custom_tax_menu_item' , 15 );
/**
* Adds terms to a specific part of the menu by location
*
*/
function wps_add_custom_tax_menu_item( ) {
// Edit these three variables
$top_menu_item_name = 'Projects';
$taxonomy = 'wps_projects';
$menu_location = 'primary';

// Get menu by location
$menu = wp_get_nav_menus( array( 'location' => $menu_location ) );
$menu_id = $menu[0]->term_id;

// Get terms from wps_projects
$term_args = array(
'hide_empty' => false,
'parent' => 0,
);

// Get only top level $terms
$terms = get_terms( $taxonomy , $term_args );

// Get items
$items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );

// Set parent item id
$default_parent_id = $parent_id = wps_get_nav_menu_item_id( $top_menu_item_name , $items );

// Filter items to only the part of the menu necessary
$items = get_nav_menu_item_children( $parent_id , $items );

// Cycle through top-level terms
foreach ( $terms as $term ) {

if ( !$term->term_id || $term->term_id == 0 )
continue;

// Check for term in menu
$match = wps_check_nav_menu_items( $term , $items );

// If not found, add
if ( $match == false ) {
wps_create_nav_menu_item( $menu_id , $term , $taxonomy , $default_parent_id );
}

// Get term's children
$term_args = array(
'hide_empty' => false,
'child_of' => $term->term_id,
);

$child_terms = get_terms( $taxonomy , $term_args );

// Set new parent item id
$parent_id = wps_get_nav_menu_item_id( $term->name , $items );
if ( ! $parent_id )
$parent_id = $default_parent_id;

if ( $child_terms ) {
// Cycle through children
foreach ( $child_terms as $child_term ) {
if ( !$child_term->term_id || $child_term->term_id == 0 )
continue;
$match = wps_check_nav_menu_items( $child_term , $items );

if ( $match == false ) {
wps_create_nav_menu_item( $menu_id , $child_term , $taxonomy , $parent_id );
}
}
}
}
}

/**
* Returns true/false whether an item is already in the items (via its items)
*
* @param object the parent nav_menu_item ID
* @param object nav_menu_items
* @return boolean returns true/false
*/
function wps_check_nav_menu_items( $term , $items ) {
$match = false;
foreach ( $items as $item ) {
if ( $term->term_id == $item->object_id ) {
$match = true;
break;
}
}
return $match;
}

// Creates a nav_menu_item
/**
* Returns true/false whether an item is already in the items (via its items)
*
* @uses wp_update_nav_menu_item
* @param object the parent nav_menu_item ID
* @param object nav_menu_items
* @return boolean returns true/false
*/
function wps_create_nav_menu_item( $menu_id , $term , $taxonomy , $parent_id , $args = array() ) {
// Setup Menu Item Args
$args = array(
'menu-item-object-id' => $term->term_id,
'menu-item-object' => $taxonomy,
'menu-item-type' => 'taxonomy',
'menu-item-status' => 'publish',
'menu-item-parent-id' => $parent_id,
'menu-item-attr-title' => $term->name,
'menu-item-description' => $term->description,
'menu-item-title' => $term->name,
'menu-item-target' => '',
'menu-item-classes' => 'termid-'.$term->term_id.' parentid-'.$parent_id,
'menu-item-xfn' => '',
);
return wp_update_nav_menu_item( $menu_id, 0, $args );
}

/**
* Returns the nav_menu_item ID of an item that has the term name
*
* @param object the parent nav_menu_item ID
* @param object nav_menu_items
* @return boolean returns true/false
*/
function wps_get_nav_menu_item_id( $term , $nav_menu_items ) {
foreach ( $nav_menu_items as $nav_menu_item ) {
if ( $term == $nav_menu_item->post_excerpt || $term == $nav_menu_item->post_title ) {
return $nav_menu_item->ID;
}
}
return false;
}

/**
* Returns all child nav_menu_items under a specific parent
*
* @param int the parent nav_menu_item ID
* @param array nav_menu_items
* @return array returns filtered array of nav_menu_items
*/
function get_nav_menu_item_children( $parent_id, $nav_menu_items ) {
$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 ( $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]

For use of get_nav_menu_item_children() see previous post: .

Written by Travis Smith · Categorized: WordPress

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

  • « Previous Page
  • 1
  • …
  • 31
  • 32
  • 33
  • 34
  • 35
  • …
  • 61
  • Next Page »
  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

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