WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to chance shorcode position (5 posts)

  1. jepser
    Member
    Posted 3 years ago #

    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() ?>">&raquo;</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?

  2. elfin
    Moderator
    Posted 3 years ago #

    don't echo, but return what you want printed to the screen.

  3. jepser
    Member
    Posted 3 years ago #

    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() ?>">&raquo;</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!

  4. jepser
    Member
    Posted 3 years ago #

    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() .'">&raquo;</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');
  5. elfin
    Moderator
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic