Skip to content

Instantly share code, notes, and snippets.

View davemac's full-sized avatar

davemac davemac

View GitHub Profile
@davemac
davemac / query_posts-date.php
Created October 4, 2011 04:47
WordPress output posts by date for current category
<div id="loop" class="clear">
<?php // List Pages by Month
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$cat = get_query_var('cat');
$args = array(
'paged' => $paged,
'cat' => $cat,
'post_type' => 'post',
@davemac
davemac / gist:1294606
Created October 18, 2011 04:25
WordPress return custom taxonomy terms with category description
<ul>
<?php
$args=array(
'taxonomy' => 'productcat',
'orderby' => 'name',
'order' => 'ASC'
);
$categories=get_categories($args);
foreach($categories as $category) {
@davemac
davemac / gist:1297140
Created October 19, 2011 00:16
WordPress query taxonomy by meta key and value
<?php
$myquery = array(
'tax_query' => array(
array(
'taxonomy' => 'productcat',
'terms' => array('equip', 'pro-rack', 'slat-tech', 'acrylics' , 'i60-system' , 'prestige', 'shop-x', 'shopfitters'),
'field' => 'slug'
)
),
'order' => 'ASC',
@davemac
davemac / gist:1454851
Created December 10, 2011 09:04
WordPress query categories and return posts that are in one category, and also could be in multiple other categories
<?php $showFactsheets->query( array( 'category__in' => array( 4, 5, 6, 7 ) , 'category__and' => array( 25 ) ) ); ?>
@davemac
davemac / gist:1456920
Created December 10, 2011 23:03
Wordpress display the current category's parent, and link to it
<?php
$cat = get_the_category();
$parentCatName = get_cat_name($cat[0]->parent);
$parentCatLink = get_category_link($cat[0]->parent);
echo '<a href="'.$parentCatLink.'">'.$parentCatName.'</a>';
?>
@davemac
davemac / gist:1456987
Created December 10, 2011 23:14
Function to exclude a category(s) from the main loop (pre WP 3.3) in certain categories
add_action( 'pre_get_posts', 'dm_exclude_category_from_category' );
function dm_exclude_category_from_category( $query ) {
global $wp_the_query;
if( $wp_the_query === $query && $query->is_category( array (4,5,6,7) ) ) {
$query->set( 'cat', '-25' );
}
}
@davemac
davemac / gist:1475179
Created December 14, 2011 03:49
WordPress is post is not in a category and if it is not a specific custom post type
<?php if ( ! (in_category( array ( 20,25 ) ) || 'tips' == get_post_type() ) ) : ?>
@davemac
davemac / gist:1480832
Created December 15, 2011 11:47
WordPress get the category parent slug and include in body_class - works in posts with multiple cats
<?php if (is_single() || is_category()) {
$category = get_the_category();
$nice_name=array();
foreach($category as $c)
{
$parent = $c->category_parent;
$parent_cat =get_category($parent);
$nice_name[]= $parent_cat->category_nicename;
}
@davemac
davemac / gist:1991197
Created March 7, 2012 05:21
Custom Fields Creator syntax
<?php $inlinecontentblocks = get_post_meta( $post->ID, 'inlinecontentblocks', true );
foreach( $inlinecontentblocks as $inlinecontentblock){ ?>
<div class="inline-thumb">
// here is where I think the below is not right
<?php $inlineImage = get_post_custom_values( $inlinecontentblock['content-block-image'] );
if ( $inlineImage ) { ?>
<img class="alignleft" title="<?php echo $inlinecontentblock['content-block-title']; ?>" src="<?php echo $inlinecontentblock['content-block-image']; ?>" alt="<?php echo $inlinecontentblock['content-block-title']; ?>" width="120" height="115" />
<?php } ?>
@davemac
davemac / gist:2155056
Created March 22, 2012 01:37
List current page's children
<?php
global $post;
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) { ?>
<ul>
<?php echo $children; ?>
</ul>
<?php } ?>