Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Check this code:

function slide_group() {                    
        register_taxonomy('Group', 'slides', array(
           'hierarchical' => true /*visualizza come le categorie*/, 'label' => 'Group',
           'query_var' => true, 'rewrite' => true));}
     add_action('init', 'slide_group', 0);


function square_slider_template() {

            // Query Arguments
            $args = array(
                        'post_type' => 'slides',
                        'posts_per_page'    => 5
                    );  

            // The Query
            $the_query = new WP_Query( $args );

            // Check if the Query returns any posts
            if ( $the_query->have_posts() ) {

            // Start the Slider ?>
            <div class="flexslider">
                <ul class="slides">

                    <?php       
                    // The Loop
                    while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                        <li>

                        <?php // Check if there's a Slide URL given and if so let's a link to it
                        if ( get_post_meta( get_the_id(), 'square_slideurl', true) != '' ) { ?>
                            <a href="<?php echo esc_url( get_post_meta( get_the_id(), 'square_slideurl', true ) ); ?>">
                        <?php }

                        // The Slide's Image
                        echo the_post_thumbnail();

                        // Close off the Slide's Link if there is one
                        if ( get_post_meta( get_the_id(), 'square_slideurl', true) != '' ) { ?>
                            </a>
                        <?php } ?>

                        </li>
                    <?php endwhile; ?>

                </ul><!-- .slides -->
            </div><!-- .flexslider -->

            <?php }

            // Reset Post Data
            wp_reset_postdata();
        }

    // Slider Shortcode

        function square_slider_shortcode() {
            ob_start();
            square_slider_template();
            $slider = ob_get_clean();
            return $slider;
        }
        add_shortcode( 'slider', 'square_slider_shortcode' );

This code is creating a slider based on "slider" custom post type using the thumbnail image. I created a custom taxonomy too.
What I would do is create a "foreach" loop based on the taxonomy-term that create a shortcode with the taxonomy-term name ( [design], [develop] etc.. OR [slider type=design], [slider type=develop] ) that contain only the posts of the taxonomy_term.
For example, [design] contains/displays only the post with design taxonomy, [develop] contains/displays only the post with develop taxonomy etc.


EDIT/UPDATE #2

I'm at this step:

    function square_slider_shortcode( $atts = array(), $content = '' )
{
    $atts = shortcode_atts( array(
                        'type'      => '00', // default type
                    ), $atts, 'square_slider' );

    // Sanitize input:
    $pid = sanitize_title( $atts['type'] );

    // Output
    return square_slider_template( $pid );    
}

add_shortcode( 'slider', 'square_slider_shortcode' );

function square_slider_template( $pid = '' )
{
$args = array(
    'post_type'       => 'slides',
    'p'           => $pid,


);  ?>

<?php
    // The Query
    $query = new WP_Query( $args );

    // Check if the Query returns any posts
    if ( $query->have_posts() ) {

    // Start the Slider ?>
    <div class="flexslider">
        <ul class="slides">

            <?php       
            // The Loop
            while ( $query->have_posts() ) : $query->the_post();
                for ($i = 1; $i <= 10; $i++):
                    $num_slide="slide_" . $i;
                    $slide = get_field($num_slide);
                    ?>                                      
                    <?php if (!empty($slide)): ?><li><img src="<?php echo $slide; ?>"></li>
                    <?php endif; ?>
            <?php endfor; ?> 
            <?php endwhile; ?>

        </ul><!-- .slides -->
    </div><!-- .flexslider -->

    <?php }

    // Reset Post Data
    wp_reset_postdata();
}

I solved in this way, I create a loop with the slides and I'll create the slider. It's not a smart way or the proper way, but it works :)
UPDATE: everything works but the slider appear always at the top of the page, independently of the position inside the editor.
I tried to put it inside another shortcode like [one_third][slider type="98"][/one_third][one_third_last]casual words[/one_third_last] but it appear outside the div tag like you can see in the image enter image description here

share|improve this question
    
why not just a single shortcode that you pass the term name to as an attribute? –  Milo Mar 28 at 0:30
1  
Regarding the update, you should rename $type to $pid for clarity. The square_slider_template function should return something or you can use the output buffer to catch it's output. Also consider using posts_per_page to control the number of slides. You should then try to feed the get_field() function with the corresponding post id: get_field($field_name, $post_id ), to see if that makes any difference. –  birgire Apr 2 at 8:19

2 Answers 2

You want to define a potentially huge number of different shortcodes with the same shortcode callback?

Why don't you define a single shortcode, with a term attribute? For example

[sc term="london"]

ps:

I think your problem lies in the $tax_term->name part, which could be a string like City of London and that's not a valid shortcode name. Try $tax_term->slug instead, but I don't think it's a good strategy!

Another problem is that you are defining a function inside the foreach loop. That should give you an error like: Fatal error: Cannot redeclare examples_shortcode() ....

You should consider using WP_DEBUG in your developement. Here's a good starting point.

Update:

You could use for example:

function square_slider_shortcode( $atts = array(), $content = '' )
{
    $atts = shortcode_atts( array(
                        'type'      => 'sport', // default type
                        'nr'        => 5,       // default number of slides
                    ), $atts, 'square_slider' );

    // Sanitize input:
    $type = sanitize_title( $atts['type'] );
    $nr   = (int) $atts['nr'];

    // Output
    return square_slider_template( $type, $nr );    
}

add_shortcode( 'slider', 'square_slider_shortcode' );

where:

function square_slider_template( $type = '', $nr = 5 )
{
    // Query Arguments
    $args = array(
        'post_type'       => 'slides',
        'posts_per_page'  => $nr,
        'tax_query'       => array(
             array(
                'taxonomy' => 'slides',
                'field'    => 'slug',
                'terms'    => $type,
             ),
        ),      
    );  

    // The Query
    $the_query = new WP_Query( $args );

    // ... etc ...

    return $html;
}

Then your shortcode syntax would be:

[slider type="sport" nr="5"]

where you now can change the term (type) and the number of slides (nr) to your needs.

share|improve this answer
    
I added more info to explain better what I would do :) btw, what do you mean like "attribute"? I'm still a noob in php :) –  dborghez Mar 28 at 7:06
2  
The shortcode attributes make it more flexible. If you define your shortcode to handle a attribute called for example type, then you can then use it like [slider type="sport"] or [slider type="news"]. –  birgire Mar 28 at 8:48
    
but does it works with taxonomies? (look above, I added my entire code ) –  dborghez Mar 28 at 14:06
    
I updated the answer with a demo that you can extend further. –  birgire Mar 28 at 14:37
    
@dborghez Did you manage to use this for your slides? –  birgire Mar 31 at 14:31

It seems you need a seft-closing shortcode.
http://codex.wordpress.org/Shortcode_API will be a great read to get started.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.