Last active
August 29, 2015 14:01
Testimonial Widget for question on WordPress Stack Exchange
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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