简体   繁体   中英

wordpress - adding shortcode with tax_query value terms variable

I'm writing a shortcode function for WP, but I have an issue and I don't understand how can I call a variable directly in wordpress when i write the shortcode, like this [shortcode-name terms="news1"]

this is my code, for the moment i pass the terms='news1' in the query, but i would like transform this value in a variable so than i can call in shortcode like like this [shortcode-name terms="news1"].

After looking at various tuts, I need to leave the variable empty but I have not been able to get that part to work ...

Could someone please point me in the right direction?

function bxslider_shortcode() {
       $content = '<ul class="bxslider">';

       $loop = new WP_Query( array(
            'post_type' => 'bxslider',
            'posts_per_page' => -1,
            'post_status' => 'publish',
            'orderby' => 'date',
            'tax_query' => array(
                array(
                'taxonomy' => 'bxslider_category',
                'field'    => 'slug',
                'terms'    => 'new1',

                ),
            ),
            'order' => 'ASC'
        ));



         while ( $loop->have_posts() ) : $loop->the_post();

           $thumb_id = get_post_thumbnail_id();
           $thumb_url = wp_get_attachment_image_src($thumb_id,'full', true);
           $content .= '<li>';
           $content .= '<img src="';
           $content .= $thumb_url[0];
           $content .= '" alt="';
           $content .= get_the_title();
           $content .= '" /></li>';
         endwhile;

       $content .= '</ul>';
       echo $content;
     }

add_shortcode('bxslider', 'bxslider_shortcode');

So effectively you want to pass the supplied terms="SOMETHING" to the function defined in add_shortcode ?

The example from this page ( http://codex.wordpress.org/Shortcode_API ) is

// [bartag foo="foo-value"]
function bartag_func( $atts ) {
    $a = shortcode_atts( array(
        'foo' => 'something',
        'bar' => 'something else',
    ), $atts );

    return "foo = {$a['foo']}";
}
add_shortcode( 'bartag', 'bartag_func' );

So for you it might be

function bxslider_shortcode($atts) {
$content = '<ul class="bxslider">';

$loop = new WP_Query( array(
    'post_type' => 'bxslider',
    'posts_per_page' => -1,
    'post_status' => 'publish',
    'orderby' => 'date',
    'tax_query' => array(
        array(
            'taxonomy' => 'bxslider_category',
            'field'    => 'slug',
            'terms'    => $atts['terms'],
        ),
    ),
    'order' => 'ASC'
));

while ( $loop->have_posts() ) : $loop->the_post();

    $thumb_id = get_post_thumbnail_id();
    $thumb_url = wp_get_attachment_image_src($thumb_id,'full', true);
    $content .= '<li>';
    $content .= '<img src="';
    $content .= $thumb_url[0];
    $content .= '" alt="';
    $content .= get_the_title();
    $content .= '" /></li>';

endwhile;

$content .= '</ul>';
echo $content;
}

add_shortcode('bxslider', 'bxslider_shortcode');

Unless I've missed something?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM