Skip to content

Instantly share code, notes, and snippets.

@spencejs
spencejs / Deepest Level Category
Created April 25, 2013 06:06
Deepest Level Category
//Deepest Level Category
function get_deep_child_category( $categories )
{
$maxId = 0;
$maxKey = 0;
foreach ( $categories as $key => $value )
{
if ( $value->parent > $maxId )
{
$maxId = $value->term_id;
@spencejs
spencejs / Add Post Type to Main Query
Created April 25, 2013 06:05
Add Post Type to Main Query, Categories Query, and Tags Query
// Add Best Albums to Main Query
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( (is_home() && $query->is_main_query() && !is_admin()) || (is_category() && $query->is_main_query() && !is_admin()) || (is_tag() && $query->is_main_query() && !is_admin()) )
$query->set( 'post_type', array( 'post', 'best_albums' ) );
return $query;
}
@spencejs
spencejs / Add Post Type Archives To Wordpress Menus.php
Created April 13, 2013 06:45
Add Post Type Archives To Wordpress Menus
//Add Post Type Archives To Wordpress Menus
add_action( 'admin_head-nav-menus.php', 'inject_cpt_archives_menu_meta_box');
function inject_cpt_archives_menu_meta_box( $object ) {
add_meta_box( 'add-cpt', __( 'Post Type Archives' ), 'wp_nav_menu_cpt_archives_meta_box', 'nav-menus', 'side', 'default' );
return $object; /* pass */
}
/* render custom post type archives meta box */
function wp_nav_menu_cpt_archives_meta_box() {
@spencejs
spencejs / Ignore Sticky Posts
Created April 13, 2013 06:44
Ignore Sticky Posts In Main Loop
//Ingore Sticky Posts
// this is key!
add_action('pre_get_posts', 'wpse74620_ignore_sticky');
// the function that does the work
function wpse74620_ignore_sticky($query)
{
// sure we're were we want to be.
if (is_home() && $query->is_main_query())
$query->set('ignore_sticky_posts', true);
}
@spencejs
spencejs / Insert Custom HTML Into Wordpress Menu.php
Last active December 16, 2015 04:29
Insert Custom HTML Into Wordpress Menu
//Insert Icon Links Into Menu
add_filter( 'wp_nav_menu_items', 'your_custom_menu_item', 10, 2 );
function your_custom_menu_item ( $items, $args ) {
if ($args->theme_location == 'primary_navigation') {
$items .= '<li><a href="http://alarmpress.com" class="icon-alone">
<span aria-hidden="true" class="icon-alarmpress_icon"></span>
<span class="screen-reader-text">ALARM Press</span>
</a></li>';
}
return $items;
@spencejs
spencejs / Dollarfy and Commafy Functions
Created March 28, 2013 22:40
Dollarfy and Commafy Functions - Renders numbers in money and comma formats (Commafy can be used alone, but Dollarfy requires Commafy) Use: echo dollarfy($somenumber);
//Dollarfy Function
function dollarfy ($num,$dec="2") {
$format="%.$dec" . "f";
$number=sprintf($format,$num);
$str=strtok($number,".");
$dc=strtok(".");
$str=commafy($str);
$return="\$$str";
if ($dec!=0){
@spencejs
spencejs / Template URL
Created March 23, 2013 05:50
Echo Template URL
<?php bloginfo('template_url'); ?>
@spencejs
spencejs / Queried Taxonomy Name
Created March 23, 2013 05:46
Echo Queried Taxonomy Name
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
echo $term->name;
?>
@spencejs
spencejs / Run Shortcode
Created March 23, 2013 05:44
Run Shortcode In PHP
<?php echo do_shortcode( '[contact-form 1 "Contact form 1"]' ); ?>
@spencejs
spencejs / Search By Category
Created March 23, 2013 05:43
Search Form For Specific Category
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
<input type="hidden" name="cat" value="22" />
<input type="submit" id="searchsubmit" value="Search" />
</form>