Forum Replies Created

Viewing 1 replies (of 1 total)
  • Hi,
    I was playing with something similar for my homepage, and ended up writing my own custom plugin which offered short codes.
    put something like the following into a php file, and add a folder and that file into the plugins section of your site.

    Then go into the wordpress admin section and enable your plugin.

    <?php
    /*
    * Plugin Name: My Custom ShortCodes
    * Description: Create your WordPress shortcode.
    * Version: 1.0
    * Author: John Porra
    * Author URI: https://www.pum.com.au
    */
    
    // Example 1 : WP Shortcode to display form on any page or post.
    function pp_product_slider( $atts = [] ){
    
        // normalize attribute keys, lowercase
        // this next part allows you to pass arguments to the short code. i.e.
        [product-slider product="pizza"]
    
        $atts = array_change_key_case((array)$atts, CASE_LOWER);
    
        $product_name =  $atts['product'];
       
        global $wpdb;
        
        $postids = $wpdb->get_col($wpdb->prepare("
        SELECT      ID
        FROM        $wpdb->posts
        WHERE       $wpdb->posts.post_title like '" . $product_name . " %'
        ORDER BY    $wpdb->posts.post_title", $product_name)); 
    
        if ($postids) {
            $args = [
                'post_type'      => 'post',
                'posts_per_page' => -1,
                'orderby'        => 'post_title',
                'order'          => 'asc',
                'post__in' => $postids
            ];
    
            $the_query = new WP_Query($args);
        
            // The Loop
            // we put the output into a string, rather than an echo so that it can go where we want it, not where it appears when it executes.
            if ( $the_query->have_posts() ) {
                $o= '';
                if ( $the_query->have_posts() ) {
                    //$the_query->the_post();
                    $id = $the_query->posts[0]->ID;
    
                    // Here's where you'd build your custom HTML                
                    $o = $o .  '<div class="container primacy-slider">';
                    $o = $o .       '<div class="row">';
                    $o = $o .           '<div class="col-md-2"><a href="' . get_permalink($id) . '">' . get_the_post_thumbnail($id, 'fashify-thumb-default') . '</a></div>';
                    $o = $o .           '<div class="col-md-7">';
                    $o = $o .               '<a href="' . get_permalink($id) . '"><h4>' . get_the_title($id) . '</h4></a>';
                    $o = $o .               '<p>' . get_the_excerpt($id) . '</p>';
                    $o = $o .           '</div>';
                    $o = $o .           '<div class="col-md-3">';
    
                    
                    $o = $o .           '</div>';
                    $o = $o .       '</div>';
                    $o = $o .   '</div>';
                }
                
                /* Restore original Post Data */
                wp_reset_postdata();
            } else {
                // no posts found
                $o = 'Nothing here.';
            }
        }
        return $o;
    }
    // change 'product-slider' to the name you want YOUR short code to have...
    add_shortcode('product-slider', 'pp_product_slider');
    ?>

    Then to use it, set up the slider to enable short codes, and add your short code in there.

    i.e. for the one above use
    [product-slider product="pizza"]
    and then your slide will be populated with the “pizza product”

    Hope this helps.

Viewing 1 replies (of 1 total)