Skip to content

Instantly share code, notes, and snippets.

@aj-adl
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aj-adl/bb023ce8fb98c1efa8d6 to your computer and use it in GitHub Desktop.
Save aj-adl/bb023ce8fb98c1efa8d6 to your computer and use it in GitHub Desktop.
Testimonial Widget for question on WordPress Stack Exchange
<?php
// don't include opening php tag unless necessary
function testimonial_widget($args, $instance) {
global $post;
$current_page_id = $post->ID;
$current_page_id_string = strval($post->ID);
// This queries for post_type of testimonal with a
// meta-select value of the current page ID
// I think you may be tripping up because $post->ID is an integer
// but the meta-select value you are saving is a string, so I am
// going to try both here.
$query_args = array (
'post_type' => 'testimonial',
'meta_query' => array(
array(
'key' => 'meta-select',
'value' => array( $current_page_id, $current_page_id_string ),
'compare' => 'IN',
),
),
);
$t_q = new WP_Query( $query_args );
$title = apply_filters( 'widget_title', $instance['title'] );
do_action('before_widget');
$output = "<div id='testimonial-rotator' class='widget'>";
$output .= "<h3 class='widgettitle'>" . $instance['title'] . "</h3> <ul>";
if ( $t_q->have_posts() ) :
while ( $t_q->have_posts() ) : $t_q->the_post();
$output .= "<li id='testimonial-".the_ID()."'>";
$output .= "<blockquote>".the_excerpt()."</blockquote>";
$output .= "<p>".the_author()."</p>";
$output .= "</li>";
endwhile;
endif;
$output .= "</ul> </div>";
echo $output;
do_action('after_widget');
wp_reset_postdata();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment