-4

I tried a lot but failed to create shortcode of this PHP snippet:

<ul>
<?php
$my_query = new WP_Query('post_status=future&order=DESC&showposts=10');
if ($my_query->have_posts()) {
    while ($my_query->have_posts()) : $my_query->the_post();

        $do_not_duplicate = $post->ID; ?>

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

    <?php endwhile;
}
?>
</ul>

Can you tell me how to do that & also explain how to put PHP codes into shortcodes. Don't need to explain the basic as I know how to create a shortcode.

  • 2
    Where is your attempt to create a shortcode? There are any number of Q/As on this site that should answer this question. For example, this one. – s_ha_dum Feb 24 '14 at 20:20
0

This should do it..

   function future_posts_function($atts){
    extract(shortcode_atts(array(
        'poststatus' => 'future',
        'order'         => 'DESC',
        'showposts' => 10,
    ), $atts));

    $return_string = '<ul>';
    query_posts(array('post_status' => $poststatus, 'order' => $order, 'showposts' => $showposts));
    if (have_posts()) :
        while (have_posts()) : the_post();
            $return_string .= '<li>'.get_the_title().'</li>';
        endwhile;
    endif;
    $return_string .= '</ul>';

    wp_reset_query();
    return $return_string;
}
add_shortcode('future_posts', 'future_posts_function');
  • It's showing the published posts. However, the code I posted is showing future posts when I used it as a page template. – Hardeep Asrani Feb 25 '14 at 0:32
  • Try... <pre><?php $the_query = new WP_Query( 'post_status=future' ); ?> <?php if ( $the_query->have_posts() ) : ?></pre> – Shoebox Feb 26 '14 at 2:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.