Add php code inside the function add_shortcode in functions.php WordPress

From what I see you will need to do the following. In functions.php add:

function my_novinki( $atts ) 
{
   $images = get_field('img_novinki'); 

   if( $images ) {

    echo '<div id="carousel" class="flexslider">';
    echo '<ul class="slides gallery">';

      foreach( $images as $image ):
            echo '<li>';
            echo '<a href="'. $image["url"] .'"><img src="'.$image["sizes"]["thumbnail"] .'" alt="'. $image["alt"] .'" /></a>';
            echo '</li>';
      endforeach;

   echo '</ul>';
   echo '</div>'; 
  }

}

add_shortcode( 'my_novinki', 'my_novinki');

Then this can be called using [my_novinki] or if in a php template using <?php echo do_shortcode['my_novinki'];

Leave a Comment