Skip to content

Instantly share code, notes, and snippets.

@mattclements
Last active August 29, 2015 14:07
Show Gist options
  • Save mattclements/3e4cdd0cc7626a363ba7 to your computer and use it in GitHub Desktop.
Save mattclements/3e4cdd0cc7626a363ba7 to your computer and use it in GitHub Desktop.
Testimonial Custom Post Type
<?php
/**
* http://generatewp.com/post-type/
*/
//Add Testimonial Posts
if ( ! function_exists('testimonial_post_type') ) {
// Register Custom Post Type
function testimonial_post_type() {
$labels = array(
'name' => 'Testimonials',
'singular_name' => 'Testimonial',
'menu_name' => 'Testimonial',
'parent_item_colon' => 'Parent Testimonial:',
'all_items' => 'All Testimonials',
'view_item' => 'View Testimonial',
'add_new_item' => 'Add New Testimonial',
'add_new' => 'Add New',
'edit_item' => 'Edit Testimonial',
'update_item' => 'Update Testimonial',
'search_items' => 'Search Testimonials',
'not_found' => 'Not found',
'not_found_in_trash' => 'Not found in Trash',
);
$args = array(
'label' => 'testimonial',
'description' => 'Testimonial\'s by Customers',
'labels' => $labels,
'supports' => array( 'editor', 'title' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => false,
'show_in_admin_bar' => false,
'menu_position' => 5,
'can_export' => false,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => true,
'rewrite' => false,
'capability_type' => 'page',
);
register_post_type( 'testimonial', $args );
}
// Hook into the 'init' action
add_action( 'init', 'testimonial_post_type', 0 );
}
<?php
class RandomTestimonialWidget extends WP_Widget
{
function RandomTestimonialWidget()
{
$widget_ops = array('classname' => 'RandomTestimonialWidget', 'description' => 'Random Testimonial Widget' );
$this->WP_Widget('RandomTestimonialWidget', 'Random Testimonial Widget', $widget_ops);
}
function widget($args, $instance)
{
$posts_array = get_posts(array(
'post_type' => 'testimonial',
'numberposts' => 1,
'orderby' => 'rand',
));
if($posts_array && !empty($posts_array) && !empty($posts_array[0]))
{
$post = $posts_array[0];
$html = '<p class="testimonial-content">'.$post->post_content.'</p>';
$html .= '<h4 class="testimonial-by">'.$post->post_title.'</h4>';
echo $args['before_widget'];
echo $html;
echo $args['after_widget'];
return;
}
return;
}
}
function register_random_testimonial_widget() {
return register_widget("RandomTestimonialWidget");
}
add_action('widgets_init', 'register_random_testimonial_widget');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment