How to chance shorcode position
-
http://develop.planetwize.com/our-focus/african-rift-valley/
I have a shortcode with the following code [news_events /]
<div class="inner-news"> <h2 class="main-title">News & Events</h2> <?php $newscat = get_cat_id('news'); $eventscat = get_cat_id('events'); ?> <?php $shortnne = new WP_Query('cat='. $newscat . ','. $eventscat .'&showposts=2'); while ($shortnne->have_posts()) : $shortnne->the_post(); ?> <div class="mini-post"> <h4><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h4> <p><?php custom_excerpt(10,false) ?><a href="<?php the_permalink() ?>">»</a></p> </div> <?php endwhile; wp_reset_postdata(); ?> <a href="#" class="see-more">See more</a> </div>
I want to show below the image but wherever I put the shorcode the div is created at the beginning, how to I fix that?
Viewing 4 replies - 1 through 4 (of 4 total)
-
don’t echo, but return what you want printed to the screen.
this is placed in the functions.php
function news_events_side() { ?> <div class="inner-news"> <h2 class="main-title">News & Events</h2> <?php $newscat = get_cat_id('news'); $eventscat = get_cat_id('events'); ?> <?php $shortnne = new WP_Query('cat='. $newscat . ','. $eventscat .'&showposts=2'); while ($shortnne->have_posts()) : $shortnne->the_post(); ?> <div class="mini-post"> <h4><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h4> <p><?php custom_excerpt(10,false) ?><a href="<?php the_permalink() ?>">»</a></p> </div> <?php endwhile; wp_reset_postdata(); ?> <a href="#" class="see-more">See more</a> </div> <?php } add_shortcode('news_events', 'news_events_side');
I don’t understand what do you mean with don’t echo, thanks!
Thanks! Here is the new code…
function news_events_side() { $newscat = get_cat_id('news'); $eventscat = get_cat_id('events'); $output = '<div class="inner-news">'; $output .= '<h2 class="main-title">News & Events</h2>'; $shortnne = new WP_Query('cat='. $newscat . ','. $eventscat .'&showposts=2'); while ($shortnne->have_posts()) : $shortnne->the_post(); $output .= '<div class="mini-post">'; $output .= '<h4><a href="'. get_permalink() .'">' . get_the_title() . '</a></h4>'; $output .= '<p>' . get_custom_excerpt(10) . '<a href="' . get_permalink() .'">»</a></p>'; $output .= '</div>'; endwhile; wp_reset_postdata(); $output .= '<a href="#" class="see-more">See more</a></div>'; return $output; } add_shortcode('news_events', 'news_events_side');
basically you are printing direct to the screen – if you do that then the output will appear before the content. To get the output to appear exactly where you place the shortcode you need to return a variable.
ahh you replied as I was posting, looks ok.
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘How to chance shorcode position’ is closed to new replies.