I’m aware it is probably a return problem. So I divided the content up, one in a function called thelist
and the other being an actual function returning it. The code follows the question.
The actual shortcode works, except the contents appear at the top before the rest of the content. I thought the now_include_post
return would fix it, however it does not. Can anybody help?
function thelist() {
if (have_posts()) : while (have_posts()) : the_post();
?>
<div id="post-<?php the_ID(); ?>" <?php post_class('thumb'); ?>>
<a href="<?php the_permalink() ?>" class="thumb-link">
<?php
if (!post_password_required()) {
if (has_post_thumbnail()) {
the_post_thumbnail();
}
} else {
?>
<img src="<?php bloginfo('template_url') ?>/img/locked.png" />
<?php } ?>
</a>
<h2>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
</h2>
</div>
<?php /* end post */ ?>
<?php
endwhile;
endif;
wp_reset_query();
}
?>
<?php
function now_include_post($atts) {
$thepostid = intval($atts[id]);
query_posts("p=$thepostid");
$output .= thelist();
return $output;
}
You want to return all the text rather than outputting it then and there when you are escaping PHP.
At the start of your thelist() function start an output buffer with
Then at the end close this buffer and return its contents with
That will return the content rather than echo it straight away, which is what you want to do in the case of a WP shortcode
PHP information on Output Buffering Functions