kronoszx
5/16/2017 - 3:38 PM

Get Custom Post by Taxonomy

Get Custom Post by Taxonomy



function shortcode_show_minibanners( $atts ) {
    global $wp_query,
        $post;

    $atts = shortcode_atts( array(
        'tipo' => ''
    ), $atts );

    $loop = new WP_Query( array(
        'posts_per_page'    => 200,
        'post_type'         => 'minibanners',
        'orderby'           => 'title',
        'order'             => 'ASC',
        'tax_query'         => array( array(
            'taxonomy'  => 'tipo',
            'field'     => 'slug',
            'terms'     => array( sanitize_title( $atts['tipo'] ) )
        ) )
    ) );

    if( ! $loop->have_posts() ) {
        return false;
    }
    echo '<div class="minibanners">';
    while( $loop->have_posts() ) {
        $loop->the_post();
        $url    = get_post_meta(get_the_ID(), "url", true);
        $image  = get_post_meta(get_the_ID(), "imagen", true);
        $target = get_post_meta(get_the_ID(), "target", true);
        echo '<a href="'.$url.'" target="'.$target.'">'.wp_get_attachment_image($image, 'full','').'</a>';
    }
    echo '</div>';

    wp_reset_postdata();
}
add_shortcode( 'minibanners', 'shortcode_show_minibanners' );