Instantly share code, notes, and snippets.
Created
January 20, 2015 16:00
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
-
Save samikeijonen/65217891c2fb5f11b5fe to your computer and use it in GitHub Desktop.
Get post from same taxonomy.
This file contains hidden or 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 | |
| /** | |
| * Stoma Related Post Widget class. | |
| * | |
| * @since 1.0.0 | |
| */ | |
| class Stomap_Related_Post_Widget extends WP_Widget { | |
| /** | |
| * Set up the widget's unique name, ID, class, description, and other options. | |
| * | |
| * @since 1.0.0 | |
| */ | |
| function __construct() { | |
| /* Set up the widget options. */ | |
| $widget_options = array( | |
| 'classname' => 'stomap-related-post', | |
| 'description' => esc_html__( 'Display related professionals by category.', 'stomap' ) | |
| ); | |
| /* Set up the widget control options. */ | |
| $control_options = array( | |
| 'width' => 200, | |
| 'height' => 350, | |
| 'id_base' => 'stomap-related-post' | |
| ); | |
| /* Create the widget. */ | |
| $this->WP_Widget( | |
| 'stomap-related-post', // $this->id_base | |
| __( 'Related professionals', 'stomap' ), // $this->name | |
| $widget_options, // $this->widget_options | |
| $control_options // $this->control_options | |
| ); | |
| } | |
| /** | |
| * Outputs the widget based on the arguments input through the widget controls. | |
| * | |
| * @since 1.0.0 | |
| */ | |
| function widget( $args, $instance ) { | |
| extract( $args ); | |
| /* If we are not in singular team-member page, get out of here. */ | |
| if ( !is_singular( 'team-member' ) ) { | |
| return false; | |
| } | |
| /* Get id of the 'page'. */ | |
| $id = get_queried_object_id(); | |
| /* Id to array. */ | |
| $id_array = array( $id ); | |
| /* Get team-member-category. */ | |
| $terms = get_the_terms( $id, 'team-member-category' ); | |
| /* If page doesn't have any categories attached to it, get out of here. */ | |
| if( empty( $terms ) ) { | |
| return false; | |
| } | |
| /* Get taxonomy IDs. */ | |
| $term_id = array(); | |
| foreach ( $terms as $term ) { | |
| $term_id[] = $term->slug; | |
| $term_title = __( 'Other', 'stomap' ) . ' ' . $term->name; | |
| } | |
| /* Open the before widget HTML. */ | |
| echo $before_widget; | |
| /* Output the term title. */ | |
| //if ( $instance['title'] ) | |
| echo $before_title . apply_filters( 'widget_title', $term_title, $instance, $this->id_base ) . $after_title; | |
| /* Set custom query to show related posts from same category. */ | |
| $stomap_posts_args = apply_filters( 'stomap_plugin_related_post_arguments', array( | |
| 'orderby' => 'rand', | |
| 'post_type' => 'team-member', | |
| 'tax_query' => array( | |
| array( | |
| 'taxonomy' => 'team-member-category', | |
| 'field' => 'slug', | |
| 'terms' => $term_id, | |
| ), | |
| ), | |
| 'post__not_in' => $id_array, // Don't show current page | |
| 'posts_per_page' => 5 | |
| ) ); | |
| $stomap_posts = new WP_Query( $stomap_posts_args ); | |
| ?> | |
| <?php if ( $stomap_posts->have_posts() ) : ?> | |
| <?php while ( $stomap_posts->have_posts() ) : $stomap_posts->the_post(); ?> | |
| <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> | |
| <header class="entry-header"> | |
| <?php if ( has_post_thumbnail() ) the_post_thumbnail( 'kuorinka-large', array( 'class' => 'thumbnail-large' ) ); ?> | |
| <?php the_title( sprintf( '<strong><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></strong>' ); ?> | |
| </header><!-- .entry-header --> | |
| <div class="entry-summary" <?php hybrid_attr( 'entry-summary' ); ?>> | |
| <?php the_excerpt(); ?> | |
| </div><!-- .entry-summary --> | |
| </article><!-- .entry --> | |
| <?php endwhile; ?> | |
| <?php endif; wp_reset_postdata(); // reset query. | |
| /* Close the after widget HTML. */ | |
| echo $after_widget; | |
| } | |
| /** | |
| * Updates the widget control options for the particular instance of the widget. | |
| * | |
| * @since 1.0.0 | |
| */ | |
| function update( $new_instance, $old_instance ) { | |
| $instance = $old_instance; | |
| /* Set the instance to the new instance. */ | |
| $instance = $new_instance; | |
| /* Strip tags from elements that don't need them. */ | |
| $instance['title'] = strip_tags( $new_instance['title'] ); | |
| return $instance; | |
| } | |
| /** | |
| * Displays the widget control options in the Widgets admin screen. | |
| * | |
| * @since 1.0.0 | |
| */ | |
| function form( $instance ) { | |
| /* Set up the defaults. */ | |
| $defaults = apply_filters( 'stomap_plugin_widget_defaults', array( | |
| 'title' => __( 'Related professionals', 'stomap' ) | |
| ) ); | |
| $instance = wp_parse_args( (array) $instance, $defaults ); | |
| ?> | |
| <p> | |
| <?php echo __( 'Related professionals', 'stomap' ); ?> | |
| </p> | |
| <div style="clear:both;"> </div> | |
| <?php | |
| } | |
| } | |
| /** | |
| * Register Widgets | |
| * | |
| * @since 1.0.0 | |
| */ | |
| function stomap_plugin_register_widgets() { | |
| register_widget( 'Stomap_Related_Post_Widget' ); | |
| } | |
| add_action( 'widgets_init', 'stomap_plugin_register_widgets' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment