Skip to content

Instantly share code, notes, and snippets.

@spencejs
spencejs / Search By Post Type
Created March 23, 2013 05:41
Search Form For Specific Post Type
<!--Search Form for Custom Post Type Only-->
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<input type="text" name="s" id="s" value="" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"/>
<input type="hidden" name="post_type" value="art" />
<input type="submit" id="searchsubmit" value="Search Art" />
</form>
@spencejs
spencejs / Reset Query
Created March 23, 2013 05:35
Reset Query
<?php wp_reset_query(); ?>
@spencejs
spencejs / Redirect To First Child
Created March 23, 2013 05:32
Redirect To First Child Page
<?php
/*
Template Name: redirect to first child
*/
if (have_posts()) {
while (have_posts()) {
the_post();
$pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
$firstchild = $pagekids[0];
wp_redirect(get_permalink($firstchild->ID));
@spencejs
spencejs / Query Custom Post Type
Created March 23, 2013 05:30
Query Custom Post Type
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( 'post_type=projects&posts_per_page=5&paged='.$paged);
?>
@spencejs
spencejs / Query By Date And Expiration
Created March 23, 2013 05:29
Query By Date And Expiration (Events Loop)
<?php
$currentdate = date("Y-m-d");
$wp_query = new WP_Query();
$wp_query->query('cat=4,5&posts_per_page=6&meta_key=date&orderby=meta_value&order=ASC&meta_compare=>=&meta_value=' . $currentdate .'&paged='.$paged);
?>
@spencejs
spencejs / Pagination
Created March 23, 2013 05:27
Pagination
// get total number of pages
global $wp_query;
$total = $wp_query->max_num_pages;
// only bother with the rest if we have more than 1 page!
if ( $total > 1 ) {
// get the current page
if ( !$current_page = get_query_var('paged') )
$current_page = 1;
// structure of “format” depends on whether we’re using pretty permalinks
$format = empty( get_option('permalink_structure') ) ? '&page=%#%' : 'page/%#%/';
@spencejs
spencejs / List Pages
Created March 23, 2013 05:15
List Pages
<ul id="nav">
<?php wp_list_pages('sort_column=menu_order&include=2,8,10,6&title_li='); ?>
</ul>
@spencejs
spencejs / List Taxonomy Terms
Created March 23, 2013 05:09
List All Terms In Taxonomy
<?php //List All Terms from Custom Taxonomy
$args = array(
'taxonomy' => 'shop_artists',
'orderby' => 'name',
'hierarchical' => False,
'title_li' => '<h2>Artists</h2>'
);
?>
<ul>
@spencejs
spencejs / Link To Parent Page
Created March 23, 2013 05:02
Link Back To Parent Page
@spencejs
spencejs / Display Latest Tweet
Created March 23, 2013 04:59
Display Latest Tweet From Twitter Without a Plugin
<?php
// Your twitter username.
$username = "Antinym";
// Prefix - some text you want displayed before your latest tweet.
// (HTML is OK, but be sure to escape quotes with backslashes: for example href=\"link.html\")
$prefix = "<h2>The Latest on Twitter</h2>";
// Suffix - some text you want display after your latest tweet. (Same rules as the prefix.)
$suffix = "";
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";
function parse_feed($feed) {