wordpress - rewrite function to not use echo php for use with short_code -


i having bit of trouble getting shortcode place html of function in correct place on wordpress. after research have found when using shortcodes cannot have echo used in function. have been doing best write function without echo's cant seem work. have little actual experience php language , giving best shot.

the top code block functioning code echo used throughout, , second rewritten code without echo not functioning. have idea rewrite going wrong? or possibly have better work around rewriting function?

first:

function na_get_gallery_image_urls( $post_id ) {  $post = get_post($post_id);  // make sure post has gallery in if( ! has_shortcode( $post->post_content, 'gallery' ) )     return;  //initiate counter able keep track of images $count_img = 0;    // retrieve galleries of post $gallery = get_post_gallery_images( $post );   // loop through each image in first gallery foreach( $gallery $image ) {   // below here main containers , first large image; stuff want output 1 time.  if($count_img == 0) { ?>     <!-- whole gallery container (inludes thumbnails) -->    <div id="instant-gallery">       <!-- main display area -->      <div id="ig-main">           <!-- set parameters image display. -->          <?php           $default_attr = "ig-hero";          ?>           <!-- display first image attachment large image in main gallery area -->          <img src="<?php print $image; ?>" id="<?php print $default_attr; ?>">           <a href="<?php print $image; ?>" download>download</a>       <!-- close main display area -->      </div>       <!-- open thumbnail navigation -->      <ul id="ig-thumbs">    <!-- end block of stuff first image  -->    <?php } ?>    <!-- now, each of thumbnail images, label li id of        appropriate thumbnail number -->   <li id="ig-thumb-<?php print $count_img+1; ?>">    <?php if ($count_img==0) {     // if first thumbnail, add class of 'selected' highlighted    $thumb_attr = "thumb selected";     } else {     // other thumbnails, add basic class of 'thumb'    $thumb_attr = "thumb";     } ?>    <!-- output thumbnail-sized version of image has attributes defined above -->  <img src="<?php print $image; ?>" class="<?php print $thumb_attr; ?>">   </li>   <!-- increment counter can keep track of thumbnail on -->  <?php $count_img = $count_img + 1; } ?>   <!-- close thumbnail navigation list -->  </ul>   <!-- close entire gallery -->   </div>    <?php     }     function instant_gallery() {    global $post;    $args = array(     'post_parent'    => '$post->id',            // current post     'post_type'      => 'attachment',       // post attachments     'post_mime_type' => 'image',            // grab images     'order'          => 'asc',              // list in ascending order     'orderby'        => 'menu_order',       // list them in menu order     'numberposts'    => -1,                 // show attachments     'post_status'    => null,               // post status   );   // retrieve items match our query; in case, images attached current post. na_get_gallery_image_urls($args[0]);   }    // create shortcode  add_shortcode('instant_gallery', 'instant_gallery');    ?> 

last:

  function na_get_gallery_image_urls( $post_id ) {    $post = get_post($post_id);         // make sure post has gallery in   if( ! has_shortcode( $post->post_content, 'gallery' ) )     return;    // initiate counter able keep track of images   $count_img = 0;      // retrieve galleries of post   $gallery = get_post_gallery_images( $post );      // loop through each image in first gallery   foreach( $gallery $image ) {     // below here main containers , first large image; stuff want output 1 time.   if($count_img == 0) {      //<!-- whole gallery container (inludes thumbnails) -->    $hero_img = '<div id="instant-gallery">';       //<!-- main display area -->      $hero_img .= '<div id="ig-main">';           //<!-- set parameters image display. -->           $default_attr = "ig-hero";           //<!-- display first image attachment large image in main gallery area -->          $hero_img .= '<img src="' . $image . '" id="' . $default_attr . '">';           $hero_img .= '<a href="' . $image . '" download>download</a>';           //close main display area           $hero_img .= '</div>';                  //<!-- open thumbnail navigation -->         $hero_img .= '<ul id="ig-thumbs">';          //<!-- end block of stuff first image  -->        }         //<!-- now, each of thumbnail images, label li id of appropriate thumbnail number -->       $thumbs = '<li id="ig-thumb-' . ($count_img + 1) . '">';    if ($count_img==0) {      // if first thumbnail, add class of 'selected' highlighted    $thumb_attr = "thumb selected";    } else {     // other thumbnails, add basic class of 'thumb'    $thumb_attr = "thumb";    }     //<!-- output thumbnail-sized version of image has attributes defined above -->   $thumbs .= '<img src="' . $image . '" class="' . $thumb_attr . '">' .  '</li>';     //<!-- increment counter can keep track of thumbnail on -->   $count_img = $count_img + 1;   }    //<!-- close thumbnail navigation list -->  $fin = '</ul>';   // <!-- close entire gallery --> $fin .= '</div>';  $content = "$hero_img" . "$thumbs" . "$fin";  return $content;  }    function instant_gallery() {    global $post;    $args = array(     'post_parent'    => '$post->id',            // current post     'post_type'      => 'attachment',       // post attachments     'post_mime_type' => 'image',            // grab images     'order'          => 'asc',              // list in ascending order     'orderby'        => 'menu_order',       // list them in menu order     'numberposts'    => -1,                 // show attachments     'post_status'    => null,               // post status   );      // retrieve items match our query; in case, images attached         current post. na_get_gallery_image_urls($post->id);  }  // create shortcode add_shortcode('instant_gallery', 'instant_gallery');  ?> 

hi try below code changing parameters works.

function testgallery_shortcode( $atts , $content = null ) {     extract( shortcode_atts(     array(     'title' =>'test gallery',     'items' =>3,     ), $atts )     );      $args = array(             'post_type'      => 'testgallery',             'posts_per_page' => $items,             'post_status'    => 'publish',     );      $test_gallery = new wp_query( $args );      $html = '';          global $post;         if ( $test_gallery->have_posts() ) :while ( $test_gallery->have_posts() ) : $test_gallery->the_post();         $html.='<div class="col-sm-4 col-xs-12">';         $html.='<figure class="post">';         $html.='<div class="image-wrap">';         $html.='<a class="zoomtoplay" alt="gallery_image" href="'. esc_url( get_permalink() ).'"></a>';         if (has_post_thumbnail() ) {             $imgarray = wp_get_attachment_image_src( get_post_thumbnail_id($post->id),'full');             $imgurl = $imgarray[0];             $html.='<img src="'.$imgurl.'" class="img-responsive"/>';         }         $html.='<a href="'.esc_url( get_permalink() ).'"></div><h3>'.get_the_title().'</h3></a>';         $html.='</figure>';         $html.='</div>';          endwhile;         endif;         wp_reset_query();      return $html;  } add_shortcode( 'testgallery', 'testgallery_shortcode' ); 

-- can use [testgallery] shortcode in post , page.

this standard way of creating shortcode.

still if needed more details can visit link - https://www.sitepoint.com/custom-shortcodes-for-wordpress/


Comments