Showing posts with label Shortcode. Show all posts
Showing posts with label Shortcode. Show all posts

Tuesday, March 28, 2023

Create shortcode use anywhere in WordPress

WordPress short-codes act as shortcuts that allow you to embed elements into a post or page quickly. These usually consist of a single line of code within square brackets, such as [yourshortcode], for example. This code will display a predetermined feature on the front end of your site.


Here is the shortcode.

<?php 

// testimonials slider

add_shortcode( 'list-posts-testimonial', 'testimonial_listing_shortcode' );

function testimonial_listing_shortcode( $atts ) {

  ob_start();

   $query = new WP_Query( array(

        'post_type' => 'testimonial',

        'posts_per_page' => -1,

        'order' => 'ASC',

        'orderby' => 'date',

        'menu_order'=>'ASC',

    ) );

if ( $query->have_posts() ) { 

?>

<div class="testimonial-slider-sec">

    <div class="owl-carousel owl-theme testimonial-slider">

        <?php while ( $query->have_posts() ) : $query->the_post(); ?>

        <div class="testimonial-items">

            <div class="testimonial-box">

                <div class="testimonial-img">

                 <img src="<?php echo get_the_post_thumbnail_url();  ?>" alt="testimonial-image">

            </div>

                <div class="testimonial-content"> 

                <h4><?php the_title(); ?></h4>

                    <?php the_field('designation'); ?>

                </div>

            </div>

        </div>

        <?php endwhile; ?>

        <?php wp_reset_query(); ?>

    </div>

    </div>

<?php 

    $myvariable = ob_get_clean();

        return $myvariable;

    } 

}


This shortcode can be used any where in WordPress pages.

[
list-posts-testimonial]

It can be modified as per requirement.


For Professional website contact WEB CODE ADDICT

View this post on Instagram A post shared by WebCodeAddict (@webcodeaddicted)