Skip to content

Instantly share code, notes, and snippets.

@jaredwilli
Created February 2, 2011 06:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredwilli/807326 to your computer and use it in GitHub Desktop.
Save jaredwilli/807326 to your computer and use it in GitHub Desktop.
Extends the WordPress Recent Posts widget to support custom post types
<?php
/*
define('COOKIEPATH', preg_replace('|https?://[^/]+|i', '', home_url() . '/' ) );
define('SITECOOKIEPATH', preg_replace('|https?://[^/]+|i', '', site_url() . '/' ) );
define('ADMIN_COOKIE_PATH', SITECOOKIEPATH . 'wp-admin' );
define('PLUGINS_COOKIE_PATH', preg_replace('|https?://[^/]+|i', '', WP_PLUGIN_URL) );
*/
define( 'WPH_FUNC_PATH', get_template_directory() . '/functions' );
define( 'WPH_FUNC_URL', get_bloginfo('template_directory' ).'/functions' );
define( 'WPH_JS_PATH', get_template_directory() . '/js' );
define( 'WPH_JS_URL', get_bloginfo('template_directory' ).'/js' );
$functions = TEMPLATEPATH . '/functions';
get_template_part ( 'functions/typeproducts' );
//get_template_part ( 'functions/uploadmetabox' );
//get_template_part ( 'functions/cust-thumb' );
get_template_part ( 'functions/upl' );
//get_template_part ( 'functions/upload' );
/**
* Add Actions and Filters
*/
add_action( 'right_now_content_table_end', 'ucc_right_now_content_table_end' );
add_action( 'wp_dashboard_setup', 'remove_dashboard_widgets' );
add_action( 'wp_dashboard_setup', 'custom_dashboard_widgets' );
add_action( 'widgets_init', 'remove_some_wp_widgets', 1 );
add_action( 'widgets_init', 'n2wp_latest_cpt_init' );
add_filter( 'login_head', 'custom_login_logo' );
add_filter( 'admin_footer_text', 'custom_admin_footer' );
add_filter( 'admin_body_class', 'base_admin_body_class' );
add_filter( 'the_generator', 'complete_version_removal' );
add_filter( 'the_search_query', 'searchAll' );
add_filter( 'request', 'any_ptype_on_cat' );
add_filter( 'request', 'custom_feed_request' );
add_filter( 'get_the_excerpt', 'trim_excerpt' );
// Add 3.0 Theme Supports
// add_theme_support( 'automatic-feed-links' );
add_post_type_support( 'page', 'excerpt' );
add_theme_support( 'post-thumbnails' );
add_theme_support( 'menus' );
add_action( 'init', 'register_navmenus' );
function register_navmenus() {
register_nav_menus( array(
'Top' => __( 'Top Navigation' ),
'Header' => __( 'Header Navigation' ),
'Footer' => __( 'Footer Navigation' ),
));
}
/**
* Remove Dashboard Widgets
*/
function remove_dashboard_widgets() {
global $wp_meta_boxes;
// unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
// unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
// unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
}
/**
* Sidebar widgets not needed
*/
function remove_some_wp_widgets(){
unregister_widget( 'WP_Widget_Calendar' );
// unregister_widget( 'WP_Widget_Search' );
// unregister_widget( 'WP_Widget_Recent_Comments' );
}
// remove [...] from excerpts
function trim_excerpt($text) { return rtrim($text, '[...]' ); }
// remove version info from head and feeds
function complete_version_removal() { return ''; }
/**
* Search all post types
*/
function searchAll( $query ) {
if ( $query->is_search ) { $query->set( 'post_type', array( 'product' )); }
return $query;
}
/**
* Add custom post types to feed
*/
function custom_feed_request( $vars ) {
if (isset($vars['feed']) && !isset($vars['post_type']))
$vars['post_type'] = array( 'post', 'product' );
return $vars;
}
/**
* Admin footer + login page
* TODO: CHANGE LOGIN PAGE LOGO
*/
function custom_admin_footer() { echo '<a href="http://new2wp.com">Site created by Jared Williams</a> - Copyright <a href="http://portcandle.com">The Port Candle</a>'; }
/** Login logo */
function custom_login_logo() {
$logoimg = site_url('wp-content/uploads/2010/12/portcandle-loginlogo.png');
echo '<style type="text/css">#login h1 { position: relative; top:10px; height:127px; }
#login h1 a{ background-image:url(' . $logoimg . ') !important; height:127px; }
#loginform { background: #eee; border:1px solid #ccc; position:relative; top:25px; }
#nav { margin:30px 0 0 8px; }
.updated,.login .message, { background-color: #eee; border-color: #ccc; margin: 25px 0 0 8px; }
.error, .login #login_error { margin: 25px 0 0 8px; }
</style>';
}
/**
* Add support for post thumbnails and add thumb
* image to coloumn to post manage page
*
* @param, since 2.9
*/
if ( !function_exists('fb_AddThumbColumn') && function_exists('add_theme_support') ) {
add_theme_support( 'post-thumbnails', array( 'post', 'product', 'page' ));
function fb_AddThumbColumn($cols) { $cols['thumbnail'] = __('Thumbnail'); return $cols; }
function fb_AddThumbValue( $column_name, $post_id ) {
$width = (int) 90;
$height = (int) 90;
if ( 'thumbnail' == $column_name ) {
$thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true ); // thumbnail of WP 2.9
// image from gallery
$attachments = get_children( array(
'post_parent' => $post_id,
'post_type' => 'attachment',
'post_mime_type' => 'image'
)
);
if ( $thumbnail_id ) $thumb = wp_get_attachment_image( $thumbnail_id, array($width, $height), true );
elseif ( $attachments ) { foreach ( $attachments as $attachment_id => $attachment ) { $thumb = wp_get_attachment_image( $attachment_id, array($width, $height), true ); } }
if ( isset($thumb) && $thumb ) { echo $thumb; } else { echo __('None'); }
}
}
// post thumbnails column on posts manage page
add_filter( 'manage_posts_columns', 'fb_AddThumbColumn' );
add_action( 'manage_posts_custom_column', 'fb_AddThumbValue', 10, 2 );
// page thumbnails on pages manage page
add_filter( 'manage_pages_columns', 'fb_AddThumbColumn' );
add_action( 'manage_pages_custom_column', 'fb_AddThumbValue', 10, 2 );
}
function thumbnail_src() {
global $post;
$thumb = get_the_post_thumbnail($post->ID);
$pattern= "/(?<=src=['|\"])[^'|\"]*?(?=['|\"])/i";
preg_match($pattern, $thumb, $thumbsrc);
$thesrc = $thumbsrc[0];
return $thesrc;
}
/**
* jQuery featured posts slider
* TODO: EDIT THIS SO IT WORKS TO SHOW FEATURED PRODUCTS
*/
function home_slider() { ?>
<div id="slider2">
<ul id="slide">
<?php $q = new WP_query();
$q->query( array( 'post_type' => 'product', 'taxonomy' => 'category' ));
if ($q->have_posts()) : while ($q->have_posts()) : $q->the_post(); ?>
<li><h2 class="posttitle"><a href="<?php the_permalink(); ?>" rel="product" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<div class="postsnip"><?php the_excerpt(); ?></div>
</li>
<?php endwhile; endif; ?>
</ul>
</div>
<?php
}
// admin classes
function base_admin_body_class( $classes ) {
if (is_admin() && isset($_GET['action'])) { $classes .= 'action-'.$_GET['action']; }
if (is_admin() && isset($_GET['post'])){ $classes .= ' '; $classes.='post-'.$_GET['post']; }
// Return the $classes array
return $classes;
}
// Generates semantic classes for BODY element
function base_body_class( $print = true ) {
global $wp_query, $current_user;
// It's surely a WordPress blog, right?
$c = array('wordpress');
// Applies the time- and date-based classes (below) to BODY element
thematic_date_classes( time(), $c );
// Generic semantic classes for what type of content is displayed
is_front_page() ? $c[] = 'home' : null; // For the front page, if set
is_home() ? $c[] = 'blog' : null; // For the blog posts page, if set
is_archive() ? $c[] = 'archive' : null;
is_date() ? $c[] = 'date' : null;
is_search() ? $c[] = 'search' : null;
is_paged() ? $c[] = 'paged' : null;
is_page() ? $c[] = 'page' : null;
is_single() ? $c[] = 'post' : null;
is_attachment() ? $c[] = 'attachment' : null;
is_404() ? $c[] = 'four04' : null; // CSS doesnt allow a digit as first char
//is_tax() ? $c[] = 'taxonomy' : null;
// Special classes for BODY element when a singular post
if (is_singular()) { $c[] = 'singular'; } else { $c[] = 'not-singular'; }
// Special classes for BODY element when on a single post
if (is_single()) {
$postID = $wp_query->post->ID;
the_post();
// Adds post slug class, prefixed by 'slug-'
$c[] = 'slug-' . $wp_query->post->post_name;
// Adds 'single' class and class with the post ID
$c[] = 'single postid-' . $postID;
// Adds classes for the month, day, and hour when the post was published
if (isset( $wp_query->post->post_date ))
thematic_date_classes( mysql2date( 'U', $wp_query->post->post_date ), $c, 's-' );
// Adds category classes for each category on single posts
if ($cats = get_the_category()) foreach ($cats as $cat) $c[] = 's-category-'.$cat->slug;
// Adds tag classes for each tags on single posts
if ($tags = get_the_tags()) foreach ($tags as $tag) $c[] = 's-tag-'.$tag->slug;
// Adds author class for the post author
$c[] = 's-author-' . sanitize_title_with_dashes(strtolower(get_the_author_meta('login')));
rewind_posts();
// For posts with excerpts
if (has_excerpt()) $c[] = 's-has-excerpt';
// For posts with comments open or closed
if (comments_open()) { $c[] = 's-comments-open'; } else { $c[] = 's-comments-closed'; }
// For posts with pings open or closed
if (pings_open()) { $c[] = 's-pings-open'; } else { $c[] = 's-pings-closed'; }
// For password-protected posts
if ($post->post_password) $c[] = 's-protected';
// For sticky posts
if (is_sticky()) $c[] = 's-sticky';
} // end IF is_single()
// Author name classes for BODY on author archives
elseif (is_author()) {
$author = $wp_query->get_queried_object();
$c[] = 'author';
$c[] = 'author-' . $author->user_nicename;
}
// Page author for BODY on 'pages'
elseif (is_page()) {
$pageID = $wp_query->post->ID;
$page_children = wp_list_pages("child_of=$pageID&echo=0");
the_post();
// Adds post slug class, prefixed by 'slug-'
$c[] = 'slug-' . $wp_query->post->post_name;
$c[] = 'page pageid-' . $pageID;
// Checks to see if the page has children and/or is a child page; props to Adam
if ($page_children) $c[] = 'page-parent';
if ($wp_query->post->post_parent) $c[] = 'page-child parent-pageid-' . $wp_query->post->post_parent;
// For pages with excerpts
if (has_excerpt()) $c[] = 'page-has-excerpt';
// For pages with comments open or closed
if (comments_open()) { $c[] = 'page-comments-open'; }
else { $c[] = 'page-comments-closed'; }
// For pages with pings open or closed
if (pings_open()) { $c[] = 'page-pings-open'; } else { $c[] = 'page-pings-closed'; }
// For password-protected pages
if ($post->post_password) $c[] = 'page-protected';
// Checks to see if the page is using a template
if (is_page_template() & !is_page_template('default'))
$c[] = 'page-template page-template-' . str_replace( '.php', '-php', get_post_meta( $pageID, '_wp_page_template', true ) );
rewind_posts();
}
// Search classes for results or no results
elseif (is_search()) { the_post();
if ( have_posts() ) { $c[] = 'search-results'; } else { $c[] = 'search-no-results'; }
rewind_posts();
}
// For when a visitor is logged in while browsing
if ( $current_user->ID ) $c[] = 'loggedin';
// Paged classes; for 'page X' classes of index, single, etc.
if ( (( $page = $wp_query->get( 'paged' )) || ( $page = $wp_query->get( 'page' )) ) && $page > 1 ) {
// Thanks to Prentiss Riddle, twitter.com/pzriddle, for the security fix below.
// Ensures that an integer (not some dangerous script) is passed for the variable
$page = intval($page);
$c[] = 'paged-' . $page;
if ( is_single()) { $c[] = 'single-paged-' . $page; }
elseif ( is_page()) { $c[] = 'page-paged-' . $page; }
elseif ( is_category()) { $c[] = 'category-paged-' . $page; }
elseif ( is_tag()) { $c[] = 'tag-paged-' . $page; }
//elseif ( is_tax()) { $c[] = 'tax-paged-' . $page; }
elseif ( is_date()) { $c[] = 'date-paged-' . $page; }
elseif ( is_author()) { $c[] = 'author-paged-' . $page; }
elseif ( is_search()) { $c[] = 'search-paged-' . $page; }
}
// A little Browser detection shall we?
$browser = $_SERVER[ 'HTTP_USER_AGENT' ];
// Mac, PC ...or Linux
if ( preg_match( "/Mac/", $browser )) { $c[] = 'mac'; }
elseif ( preg_match( "/Windows/", $browser)) { $c[] = 'windows'; }
elseif ( preg_match( "/Linux/", $browser )) { $c[] = 'linux'; }
else { $c[] = 'unknown-os'; }
// Checks browsers in this order: Chrome, Safari, Opera, MSIE, FF
if (preg_match( "/Chrome/", $browser )) { $c[] = 'chrome'; /* CHROME */
preg_match( "/Chrome\/(\d.\d)/si", $browser, $matches);
$ch_version = 'ch' . str_replace( '.', '-', $matches[1] );
$c[] = $ch_version; }
elseif (preg_match( "/Safari/", $browser )) { $c[] = 'safari'; /* SAFARI */
preg_match( "/Version\/(\d.\d)/si", $browser, $matches);
$sf_version = 'sf' . str_replace( '.', '-', $matches[1] );
$c[] = $sf_version; }
elseif (preg_match( "/Opera/", $browser )) { $c[] = 'opera'; /* OPERA */
preg_match( "/Opera\/(\d.\d)/si", $browser, $matches);
$op_version = 'op' . str_replace( '.', '-', $matches[1] );
$c[] = $op_version; }
elseif (preg_match( "/MSIE/", $browser )) { $c[] = 'msie'; /* IE */
if (preg_match( "/MSIE 6.0/", $browser)) { $c[] = 'ie6'; } /* IE 6.0 */
elseif (preg_match("/MSIE 7.0/",$browser)) { $c[] = 'ie7';} /* IE 7.0 */
elseif (preg_match("/MSIE 8.0/",$browser)) { $c[] = 'ie8';} /* IE 8.0 */
}
elseif (preg_match( "/Firefox/", $browser ) &&
preg_match("/Gecko/", $browser )) { $c[] = 'firefox'; /* FIREFOX */
preg_match( "/Firefox\/(\d)/si", $browser, $matches);
$ff_version = 'ff' . str_replace( '.', '-', $matches[1] );
$c[] = $ff_version; }
else { $c[] = 'unknown-browser'; } /* UNKNOWN BROWSER */
// Separates classes with a single space, collates classes for BODY
$c = join( ' ', apply_filters( 'body_class', $c ) ); // Available filter: body_class
// And tada!
return $print ? print($c) : $c;
}
// Generates time- and date-based classes for BODY,
// post DIVs, and comment LIs; relative to GMT (UTC)
function thematic_date_classes( $t, &$c, $p = '' ) {
$t = $t + ( get_option('gmt_offset') * 3600 );
$c[] = $p . 'y' . gmdate( 'Y', $t ); // Year
$c[] = $p . 'm' . gmdate( 'm', $t ); // Month
$c[] = $p . 'd' . gmdate( 'd', $t ); // Day
$c[] = $p . 'h' . gmdate( 'H', $t ); // Hour
}
/**
* Default sidebar widgets
*/
function showDefaultWidgets() { ?>
<li id="searchbox"><?php get_search_form(); ?></li>
<li>
<h2>Pages</h2>
<ul>
<?php wp_list_pages('title_li=' ); ?>
</ul>
</li>
<li>
<h2>Recent Posts</h2>
<ul>
<?php get_archives('postbypost', 10); ?>
</ul>
</li>
<li>
<h2>Monthly Archives</h2>
<ul>
<?php wp_get_archives('type=monthly'); ?>
</ul>
</li>
<li>
<h2>Categories</h2>
<ul>
<?php wp_list_categories('show_count=0&hide_empty=1&title_li='); ?>
</ul>
</li>
<?php wp_list_bookmarks('title_li=');
}
/**
* Multiple Sidebars
*/
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name'=>'Products Sidebar',
'before_widget' => '<li id="%1$s" class="callout">',
'after_widget' => '</li>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
register_sidebar(array(
'name'=>'Featured',
'before_widget' => '<li id="%1$s" class="callout">',
'after_widget' => '</li>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
register_sidebar(array(
'name'=>'Shopping Cart',
'before_widget' => '<div id="%1$s" class="callout">',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
}
// Wordpress 2.7 Legacy Comments
function base_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; ?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
<div id="comment-<?php comment_ID(); ?>">
<div class="author-data">
<?php echo get_avatar( $comment, $size='40', $default='<path_to_url>' ); ?>
<?php printf( __( '<h3 class="author">%s</h3>' ), get_comment_author_link() );?>
<div class="comment-meta commentmetadata">
<?php printf( __( '%1$s at %2$s' ), get_comment_date(), get_comment_time() ); ?>
<?php edit_comment_link( __( '(Edit)' ),' ',''); ?>
</div>
</div>
<?php if ($comment->comment_approved == '0') : ?>
<em><?php _e( 'Your comment is awaiting moderation.' ); ?></em>
<?php endif; ?>
<div class="comment-entry"><?php comment_text(); ?></div>
<div class="reply">
<?php comment_reply_link( array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']) )); ?>
</div>
</div>
</li>
<?php
}
// Add is_child() comment Conditional
function is_child($parent) { global $wp_query;
if ($wp_query->post->post_parent == $parent) { $return = true; } else { $return = false; } return $return;
}
/**
* Add custom post types to Right Now dashboard widget
*/
function ucc_right_now_content_table_end() {
$args = array(
'public' => true ,
'_builtin' => false
);
$output = 'object';
$operator = 'and';
$post_types = get_post_types( $args , $output , $operator );
foreach( $post_types as $post_type ) {
$num_posts = wp_count_posts( $post_type->name );
$num = number_format_i18n( $num_posts->publish );
$text = _n( $post_type->labels->singular_name, $post_type->labels->name , intval( $num_posts->publish ));
if ( current_user_can( 'edit_posts' )) {
$num = "<a href='edit.php?post_type=$post_type->name'>$num</a>";
$text = "<a href='edit.php?post_type=$post_type->name'>$text</a>";
}
echo '<tr><td class="first b b-' . $post_type->name . '">' . $num . '</td>';
echo '<td class="t ' . $post_type->name . '">' . $text . '</td></tr>';
}
$taxonomies = get_taxonomies( $args , $output , $operator );
foreach( $taxonomies as $taxonomy ) {
$num_terms = wp_count_terms( $taxonomy->name );
$num = number_format_i18n( $num_terms );
$text = _n( $taxonomy->labels->singular_name, $taxonomy->labels->name , intval( $num_terms ));
if ( current_user_can( 'manage_categories' ) ) {
$num = "<a href='edit-tags.php?taxonomy=$taxonomy->name'>$num</a>";
$text = "<a href='edit-tags.php?taxonomy=$taxonomy->name'>$text</a>";
}
echo '<tr><td class="first b b-' . $taxonomy->name . '">' . $num . '</td>';
echo '<td class="t ' . $taxonomy->name . '">' . $text . '</td></tr>';
}
}
/**
* Post Type Archives
*/
$posttypes = get_post_types(array( 'post', 'product' ));
add_filter( 'getarchives_where', "WHERE post_type = '$posttypes' AND post_status = 'publish'",10,2 );
function ucc_getarchives_where_filter( $where , $r ) {
$args = array(
'public' => true ,
'_builtin' => false
);
$output = 'names';
$operator = 'and';
$post_types = get_post_types( $args , $output , $operator );
$post_types = array_merge( $post_types , array( 'post' ) );
$post_types = "'" . implode( "' , '" , $post_types ) . "'";
return str_replace( "post_type = 'post'" , "post_type IN ( $post_types )" , $where );
}
/**
* Custom Dashboard Widget
*/
function custom_dashboard_widgets() {
wp_add_dashboard_widget( 'pc_latest_posts_id', __( 'New Products' ), 'pc_latest_posts' );
}
/**
* Latest Posts
*/
function pc_latest_posts() {
global $post;
echo '<ul>';
$args = array( 'post_type' => 'product', 'showposts' => '10' );
$wpsnip = new WP_Query( $args);
if($wpsnip->have_posts()): while($wpsnip->have_posts()) : $wpsnip->the_post(); ?>
<li><strong><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></strong>></li>
<?php wp_reset_query();
endwhile; endif;
echo '</ul>';
}
/**
* List posts from a specified post type and how many to display
*/
function pc_show_latest($ptype, $howmany) {
global $post;
echo '<ul>';
rewind_posts();
$wpsnip = new WP_Query( array( 'post_type' => $ptype, 'showposts' => $howmany ));
if ($wpsnip->have_posts()): while($wpsnip->have_posts()) : $wpsnip->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php wp_reset_query();
endwhile;
endif;
echo '</ul>';
}
/**
* related post with category
* @param: int $limit limit of posts
* @param: bool $catName echo category name
* @param: string $title string before all entries
* Example: echo fb_get_cat_related_posts();
*/
if ( !function_exists('fb_get_cat_related_posts') ) {
function fb_get_cat_related_posts( $limit = 8, $catName = TRUE, $title = '<h3>Suggested Items</h3>' ) {
if ( !is_single() )
return;
$limit = (int) $limit;
$output = '';
$output .= '<div id="related-posts"><h3>' . $title . '</h3>';
$category = get_the_category();
$category = (int) $category[0]->cat_ID;
if ( $catName )
$output .= __( 'Category: ' ) . get_cat_name($category) . ' ';
$output .= '<ul class="display thumb_view">';
$args = array(
'taxonomy' => 'category',
'post_type' => $ptype,
'cat' => $category,
'showposts' => $limit,
'orderby' => 'rand'
);
$recentposts = get_posts( $args );
foreach($recentposts as $post) {
setup_postdata($post);
$output .= '<li><a href="' . get_permalink($post->ID) . '" title="' . get_the_title($post->ID) . '">' . the_post_thumbnail(array(100,100), $post->ID) . '</a>';
$output .= '<a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></li>';
}
$output .= '</ul></div>';
return $output;
}
}
/**
* related post with category
* @param: int $limit limit of posts
* @param: bool $catName echo category name
* @param: string $title string before all entries
* Example: echo pc_cat_related_posts();
*/
function pc_get_cat_related_posts( $ptype = 'product', $limit = 8, $catName = TRUE, $title = 'Suggested Items' ) {
global $post;
if ( !is_single() )
return;
$limit = (int) $limit;
$category = get_the_category();
$category = (int) $category[0]->cat_ID;
$args = array(
'showposts' => $limit,
'category__in' => $category,
'post__not_in' => array( $post->ID ),
'post_type' => $ptype,
'offset' => 1,
'orderby' => 'rand'
);
$related = new WP_Query( $args );
if ($related->have_posts()) : ?>
<div id="related-posts"><h3><?php echo $title; ?></h3>
<ul class="display thumb_view">
<?php while($related->have_posts()) : $related->the_post(); ?>
<li class="pc-related-posts">
<?php if (has_post_thumbnail($post->ID)) : ?>
<a href="<?php the_permalink($post->ID);?>" title="<?php the_title($post->ID);?>">
<?php the_post_thumbnail(array(100,100), $post->ID); ?>
</a>
<?php endif; ?>
<a href="<?php the_permalink($post->ID);?>"><?php the_title(); ?></a>
</li>
<?php wp_reset_query(); endwhile; ?>
</ul>
</div>
<?php endif;
}
/**
* Make category archives display
*/
function any_ptype_on_cat($request) {
if ( isset($request['category_name']) )
$request['post_type'] = 'product';
return $request;
}
/**
* Show categories thumbnails
*/
function showCategoryThumbs() {
$cats = get_categories(array( 'hide_empty' => 0, 'orderby' => 'name' ));
echo '<ul class="display thumb_view">';
foreach($cats as $c) {
switch ($c->slug) {
case 'candle-cards' :
$cimg = '/wp-content/uploads/wpsc/product_images/candlecards-1-2-e1288613260866.jpg';
break;
case 'candle-essentials' :
$cimg = '/wp-content/uploads/2010/11/stickwax-150x150.jpg';
break;
case 'candleholders' :
$cimg = '/wp-content/uploads/2010/11/stratford-pedestal1-150x150.jpg';
break;
case 'container-candles' :
$cimg = '/wp-content/uploads/2010/09/clip_image035-150x150.jpg';
break;
case 'eden-candles' :
$cimg = '/wp-content/uploads/2010/09/clip_image014-150x150.jpg';
break;
case 'holiday' :
$cimg = '/wp-content/uploads/2010/11/holiday-angels3-150x150.jpg';
break;
case 'pillars' :
$cimg ='/wp-content/uploads/2010/11/3x6pillar-cinn-buns-150x150.jpg';
break;
case 'tapers' :
$cimg = '/wp-content/uploads/2010/11/autumnbreeze-150x150.jpg';
break;
case 'tealights-melters' :
$cimg = '/wp-content/uploads/2010/11/tea-med-fig-150x150.jpg';
break;
case 'votives-tarts' :
$cimg = '/wp-content/uploads/2010/11/18201W1-150x150.jpg';
break;
default :
$cimg = '/wp-content/themes/candles/images/default-thumb.png';
break;
}
echo '<li id="product-' . $c->term_id . '" class="products" style="text-align:center; background:#eee;">
<div class="content_block">
<div class="thumb-cat">
<a href="' . get_category_link( $c->term_id ) . '" title="' . sprintf( __( "View all products in %s" ), $c->name ) . '" ' . '>
<img src="' . site_url($cimg) . '" title="' . sprintf( __( "View all products in %s" ), $c->name ) . '" ' . ' alt="' . sprintf( __( "View all products in %s" ), $c->name ) . '" ' . ' />
</a>
</div>
<div class="clear"></div>
<h2 class="product-title"><a href="' . get_category_link( $c->term_id ) . '" title="' . sprintf( __( "view all products in %s" ), $c->name ) . '" ' . '>' . $c->name.'</a></h2>
<p>'. $c->description . '</p>
<p>'. $c->count . ' Products</p>
</div>
</li>';
}
echo '</ul>';
}
/**
* Plugin Name: Latest Posts For Custom Types Widget
* Plugin URI: http://new2wp.com/pro/latest-custom-post-type-posts-sidebar-widget
* Description: A New2WP sidebar Widget for displaying the latest posts of any post type including custom types. Control the widget title formatting and the number of posts to display. Plus the widget is completely localized for other languages.
* Version: 1.1
* Author: Jared Williams
* Author URI: http://new2wp.com
* Tags: custom post types, post types, latest posts, sidebar widget, plugin
* License: GPL
*/
function n2wp_latest_cpt_init() {
if ( !function_exists( 'register_sidebar_widget' ))
return;
function n2wp_latest_cpt($args) {
global $post;
extract($args);
// These are our own options
$options = get_option( 'n2wp_latest_cpt' );
$title = $options['title']; // Widget title
$phead = $options['phead']; // Heading format
$ptype = $options['ptype']; // Post type
$pshow = $options['pshow']; // Number of Tweets
$beforetitle = '<'.$phead.'>';
$aftertitle = '</'.$phead.'>';
// Output
echo $before_widget;
if ($title) echo $beforetitle . $title . $aftertitle;
$pq = new WP_Query(array( 'post_type' => $ptype, 'showposts' => $pshow ));
if( $pq->have_posts() ) :
?>
<ul id="latest_cpt_list">
<?php while($pq->have_posts()) : $pq->the_post(); ?>
<li><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php wp_reset_query();
endwhile; ?>
</ul>
<?php endif; ?>
<!-- NEEDS FIX: to display link to full list of posts page
<?php $obj = get_post_type_object($ptype); ?>
<div class="latest_cpt_icon"><a href="<?php site_url('/'.$obj->query_var); ?>" rel="bookmark"><?php _e( 'View all ' . $obj->labels->name . ' posts' ); ?>&rarr;</a></div>
//-->
<?php
// echo widget closing tag
echo $after_widget;
}
/**
* Widget settings form function
*/
function n2wp_latest_cpt_control() {
// Get options
$options = get_option( 'n2wp_latest_cpt' );
// options exist? if not set defaults
if ( !is_array( $options ))
$options = array(
'title' => 'Latest Posts',
'phead' => 'h2',
'ptype' => 'post',
'pshow' => '5'
);
// form posted?
if ( $_POST['latest-cpt-submit'] ) {
$options['title'] = strip_tags( $_POST['latest-cpt-title'] );
$options['phead'] = $_POST['latest-cpt-phead'];
$options['ptype'] = $_POST['latest-cpt-ptype'];
$options['pshow'] = $_POST['latest-cpt-pshow'];
update_option( 'n2wp_latest_cpt', $options );
}
// Get options for form fields to show
$title = $options['title'];
$phead = $options['phead'];
$ptype = $options['ptype'];
$pshow = $options['pshow'];
// The widget form fields
?>
<p>
<label for="latest-cpt-title"><?php echo __( 'Widget Title' ); ?><br />
<input id="latest-cpt-title" name="latest-cpt-title" type="text" value="<?php echo $title; ?>" size="30" />
</label>
</p>
<p>
<label for="latest-cpt-phead"><?php echo __( 'Widget Heading Format' ); ?><br />
<select name="latest-cpt-phead">
<option value="h2" <?php if ($phead == 'h2') { echo 'selected="selected"'; } ?>>H2 - &lt;h2&gt;&lt;/h2&gt;</option>
<option value="h3" <?php if ($phead == 'h3') { echo 'selected="selected"'; } ?>>H3 - &lt;h3&gt;&lt;/h3&gt;</option>
<option value="h4" <?php if ($phead == 'h4') { echo 'selected="selected"'; } ?>>H4 - &lt;h4&gt;&lt;/h4&gt;</option>
<option value="strong" <?php if ($phead == 'strong') { echo 'selected="selected"'; } ?>>Bold - &lt;strong&gt;&lt;/strong&gt;</option>
</select>
</label>
</p>
<p>
<label for="latest-cpt-ptype">
<select name="latest-cpt-ptype">
<option value=""> - <?php echo __( 'Select Post Type' ); ?> - </option>
<?php $args = array( 'public' => true );
$post_types = get_post_types( $args, 'names' );
foreach ($post_types as $post_type ) { ?>
<option value="<?php echo $post_type; ?>" <?php if( $options['ptype'] == $post_type) { echo 'selected="selected"'; } ?>><?php echo $post_type;?></option>
<?php } ?>
</select>
</label>
</p>
<p>
<label for="latest-cpt-pshow"><?php echo __( 'Number of posts to show' ); ?>
<input id="latest-cpt-pshow" name="latest-cpt-pshow" type="text" value="<?php echo $pshow; ?>" size="2" />
</label>
</p>
<input type="hidden" id="latest-cpt-submit" name="latest-cpt-submit" value="1" />
<?php
}
wp_register_sidebar_widget( 'widget_latest_cpt', __('Latest Custom Posts'), 'n2wp_latest_cpt' );
wp_register_widget_control( 'widget_latest_cpt', __('Latest Custom Posts'), 'n2wp_latest_cpt_control', 300, 200 );
}
/**
* My_Custom_Recent_Posts widget class
*
*/
class WP_Widget_My_Custom_Recent_Posts extends WP_Widget {
function WP_Widget_My_Custom_Recent_Posts() {
$widget_ops = array( 'classname' => 'widget_my_custom_recent_entries', 'description' => __( "The most recent posts on your site" ) );
$this->WP_Widget( 'my-custom-recent-posts', __( 'My Custom Recent Posts' ), $widget_ops );
$this->alt_option_name = 'widget_my_custom_recent_entries';
add_action( 'save_post', array( &$this, 'flush_widget_cache' ));
add_action( 'deleted_post', array( &$this, 'flush_widget_cache' ));
add_action( 'switch_theme', array( &$this, 'flush_widget_cache' ));
}
function widget( $args, $instance ) {
$cache = wp_cache_get( 'widget_my_custom_recent_posts', 'widget' );
if ( !is_array( $cache ))
$cache = array();
if ( isset( $cache[$args[ 'widget_id' ]] )) {
echo $cache[$args[ 'widget_id' ]];
return;
}
ob_start();
extract( $args );
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'My Custom Recent Posts' ) : $instance['title'], $instance, $this->id_base );
$phead = $instance['phead'];
$ptype = $instance['ptype'];
if ( !$number = (int) $instance['number'] )
$number = 10;
else if ( $number < 1 )
$number = 1;
else if ( $number > 15 )
$number = 15;
$beforetitle = '<'.$phead.'>';
$aftertitle = '</'.$phead.'>';
$r = new WP_Query( array( 'showposts' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'ignore_sticky_posts' => true, 'post_type' => $ptype ));
if( $r->have_posts() ) : ?>
<?php echo $before_widget; ?>
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
<ul>
<?php while( $r->have_posts() ) : $r->the_post(); ?>
<li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr( get_the_title() ? get_the_title() : get_the_ID() ); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php echo $after_widget; ?>
<?php
// Reset the global $the_post as this query will have stomped on it
wp_reset_postdata();
endif;
$cache[$args[ 'widget_id' ]] = ob_get_flush();
wp_cache_set( 'widget_my_custom_recent_posts', $cache, 'widget' );
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['phead'] = $new_instance['phead'];
$instance['ptype'] = $new_instance['phead'];
$instance['number'] = (int) $new_instance['number'];
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset( $alloptions['widget_my_custom_recent_entries'] ))
delete_option( 'widget_my_custom_recent_entries' );
return $instance;
}
function flush_widget_cache() {
wp_cache_delete( 'widget_my_custom_recent_posts', 'widget' );
}
function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$phead = isset( $instance['phead'] ) ? esc_attr( $instance['phead'] ) : '';
$ptype = isset( $instance['ptype'] ) ? esc_attr( $instance['ptype'] ) : '';
if ( !isset( $instance['number'] ) || !$number = (int) $instance['number'] )
$number = 5;
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
<p>
<label for="phead"><?php echo __( 'Widget Heading Format' ); ?><br />
<select name="phead">
<option value="h2" <?php if ($phead == 'h2') { echo 'selected="selected"'; } ?>>H2 - &lt;h2&gt;</option>
<option value="h3" <?php if ($phead == 'h3') { echo 'selected="selected"'; } ?>>H3 - &lt;h3&gt;</option>
<option value="h4" <?php if ($phead == 'h4') { echo 'selected="selected"'; } ?>>H4 - &lt;h4&gt;</option>
<option value="strong" <?php if ($phead == 'strong') { echo 'selected="selected"'; } ?>>Bold - &lt;strong&gt;</option>
</select>
</label>
</p>
<p>
<label for="latest-cpt-ptype">
<select name="latest-cpt-ptype">
<option value=""> - <?php echo __( 'Select Post Type' ); ?> - </option>
<?php $args = array( 'public' => true );
$post_types = get_post_types( $args, 'names' );
foreach ( (array) $post_types as $post_type ) { ?>
<option value="<?php echo $post_type; ?>" <?php if( $options['ptype'] == $post_type) { echo 'selected="selected"'; } ?>><?php echo $post_type;?></option>
<?php } ?>
</select>
</label>
</p>
<p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?>
<input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></label></p>
<?php
}
}
?>
<?php
/**
* Plugin Name: Latest CPT Posts Widget
* Plugin URI: http://new2wp.com/pro/latest-custom-post-type-posts-sidebar-widget
* Description: A New2WP sidebar Widget for displaying the latest posts of any post type including custom types. Control the widget title formatting and the number of posts to display. Plus the widget is completely localized for other languages.
* Version: 1.6.0
* Author: Jared Williams
* Author URI: http://new2wp.com
* Tags: custom post types, post types, latest posts, sidebar widget, plugin
* License: GPL
*/
/** CHANGELOG **
- 2/2/2011 =
Rewritten to extend the WP_Widget class in WordPress
*/
class WP_Widget_Recent_Custom_Posts extends WP_Widget {
function WP_Widget_Recent_Posts() {
$widget_ops = array('classname' => 'widget_recent_custom_posts', 'description' => __( "The most recent posts of a custom post type") );
$this->WP_Widget('recent-custom-posts', __('Recent Custom Posts'), $widget_ops);
$this->alt_option_name = 'widget_recent_entries';
add_action( 'save_post', array(&$this, 'flush_widget_cache') );
add_action( 'deleted_post', array(&$this, 'flush_widget_cache') );
add_action( 'switch_theme', array(&$this, 'flush_widget_cache') );
}
function widget($args, $instance) {
$cache = wp_cache_get('widget_recent_custom_posts', 'widget');
if ( !is_array($cache) )
$cache = array();
if ( isset($cache[$args['widget_id']]) ) {
echo $cache[$args['widget_id']];
return;
}
ob_start();
extract($args);
$title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Custom Posts') : $instance['title'], $instance, $this->id_base);
$phead = $instance['phead'] ? $instance['phead'] : 'h2';
$ptype = $instance['ptype'] ? $instance['ptype'] : 'post';
if ( ! $number = absint( $instance['number'] ) )
$number = 10;
$beforetitle = '<' . $phead . '>';
$aftertitle = '</' . $phead . '>';
$r = new WP_Query(array( 'post_type' => $ptype, 'posts_per_page' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'ignore_sticky_posts' => true));
if ($r->have_posts()) :
echo $before_widget;
if ( $title ) echo $beforetitle . $title . $aftertitle; ?>
<ul>
<?php while ($r->have_posts()) : $r->the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a>
</li>
<?php endwhile; ?>
</ul>
<?php
echo $after_widget;
// Reset the global $the_post as this query will have stomped on it
wp_reset_postdata();
endif;
/* Print link to custom post_type archive page if one exists. */
if ( function_exists( 'get_post_type_archive_link' )) {
$url = '';
$label = __( 'entries' );
$post_type = get_post_type_object( $ptype );
if ( isset( $post_type->name )) {
$url = get_post_type_archive_link( $post_type->name );
}
if ( isset( $post_type->labels->name )) {
$label = $post_type->labels->name;
}
if ( !empty( $url )) {
print '<p class="latest_cpt_icon"><a href="' . esc_url( $url ) . '" rel="bookmark">' . sprintf( esc_html__( 'View all %1$s &rarr;' ), $label ) . '</a></p>';
}
}
// echo widget closing tag
echo $after_widget;
$cache[$args['widget_id']] = ob_get_flush();
wp_cache_set('widget_recent_custom_posts', $cache, 'widget');
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['phead'] = strip_tags( $new_instance['phead'] );
$instance['ptype'] = strip_tags( $new_instance['ptype'] );
$instance['number'] = (int) $new_instance['number'];
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset( $alloptions['widget_recent_entries'] ))
delete_option('widget_recent_entries');
return $instance;
}
function flush_widget_cache() {
wp_cache_delete( 'widget_recent_posts', 'widget' );
}
function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$head = isset( $instance['head'] ) ? esc_attr( $instance['head'] ) : '';
$ptype = isset( $instance['ptype'] ) ? esc_attr( $instance['ptype'] ) : '';
$number = isset( $instance['number']) ? absint( $instance['number'] ) : 5;
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('phead'); ?>"><?php echo __( 'Title Heading Format' ); ?></label><br />
<select id="<?php echo $this->get_field_id('phead'); ?>" name="<?php echo $this->get_field_id('phead'); ?>">
<option value="h2" <?php if($phead =='h2'){echo 'selected="selected"';}?>>H2 - &lt;h2&gt;</option>
<option value="h3" <?php if($phead == 'h3'){echo 'selected="selected"';}?>>H3 - &lt;h3&gt;</option>
<option value="h4" <?php if($phead == 'h4'){echo 'selected="selected"';}?>>H4 - &lt;h4&gt;</option>
<option value="strong" <?php if ($phead == 'strong') { echo 'selected="selected"'; } ?>>Bold - &lt;strong&gt;</option>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('ptype'); ?>"></label>
<select id="<?php echo $this->get_field_id('ptype'); ?>" name="<?php echo $this->get_field_name('ptype'); ?>">
<option value=""> - <?php echo __( 'Select Post Type' ); ?> - </option>
<?php $args = array( 'public' => true );
$post_types = get_post_types( $args, 'names' );
foreach ( (array) $post_types as $post_type ) { ?>
<option value="<?php echo $post_type; ?>" <?php if( $options['ptype'] == $post_type) { echo 'selected="selected"'; } ?>><?php echo $post_type;?></option>
<?php } ?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label>
<input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" />
</p>
<?php
}
} ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment