Skip to content

Instantly share code, notes, and snippets.

@designbuildtest
Last active August 29, 2015 14:15
Show Gist options
  • Save designbuildtest/616125e8c7656c7d87c5 to your computer and use it in GitHub Desktop.
Save designbuildtest/616125e8c7656c7d87c5 to your computer and use it in GitHub Desktop.
Add Theme Support - Testimonials
add_theme_support( 'myplugin-testimonials' ); // functions.php
if ( function_exists('myplugin_testimonial_html') ) { myplugin_testimonial_html(); } // template file
// Order by MENU ORDER > ASC when displayed in an archive.
function rc_modify_query_get_design_projects( $query ) {
if ( ! is_admin() && $query->is_main_query() && is_post_type_archive( 'testimonial' ) ) {
$query->set('orderby', 'menu_order' );
$query->set('order', 'ASC' );
}
}
add_action( 'pre_get_posts', 'rc_modify_query_get_design_projects' );
if ( current_theme_supports( 'onehundred-testimonials' ) ) {
function myplugin_testimonial_page_id_meta_box() {
add_meta_box( 'myplugin_testimonial_page_id', __( 'Testimonial linked to this Page' ), 'myplugin_testimonial_page_id_meta_box_callback', 'page', 'normal', 'default' );
}
add_action( 'add_meta_boxes', 'myplugin_testimonial_page_id_meta_box' );
function myplugin_testimonial_page_id_meta_box_callback( $post ) {
wp_nonce_field( 'testimonial_page_id_meta_box', 'testimonial_page_id_meta_box_nonce' );
$value = get_post_meta( $post->ID, '_myplugin_testimonial_page_id', true );
$selected = isset( $value ) ? esc_attr( $value ) : '';
?>
<p class="description"><?php _e( 'Select a customer testimonial to associate with this page. Testimonials are very effective selling tools.' ); ?></p>
<p>
<?php
$args = array(
'name' => 'testimonial_page_id',
'selected' => $selected,
'sort_order' => 'ASC',
'sort_column' => 'menu_order',
'hierarchical' => 1,
'post_type' => 'testimonial'
);
wp_dropdown_pages( $args );
?>
</p>
<?php
}
function myplugin_save_testimonial_page_id_meta_box_data( $post_id ) {
if ( ! isset( $_POST['testimonial_page_id_meta_box_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['testimonial_page_id_meta_box_nonce'], 'testimonial_page_id_meta_box' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
if ( isset( $_POST['testimonial_page_id'] ) ) {
$value = $_POST['testimonial_page_id'];
if ( absint( $value ) ) {
update_post_meta( $post_id, '_myplugin_testimonial_page_id', $value );
}
}
}
add_action( 'save_post', 'myplugin_save_testimonial_page_id_meta_box_data' );
function myplugin_testimonial_html() {
if ( ! is_page() ) {
return;
}
global $post;
$testimonial_id = get_post_meta( $post->ID, '_myplugin_testimonial_page_id', true );
if ( $testimonial_id ) {
$args = array(
'p' => $testimonial_id,
'post_type' => 'testimonial',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<div class="container"><?php
while ( $query->have_posts() ) : $query->the_post(); ?>
<blockquote class="featured-quote"><?php the_excerpt(); ?> - <a><cite><?php the_title(); ?></cite></a></blockquote><?php
endwhile;
wp_reset_postdata(); ?>
</div><?php
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment