Generate Shortcodes by Taxonomy

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.

Read More

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

Related posts

Leave a Reply

2 comments

  1. 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.