WordPress.org

Ready to get started?Download WordPress

Forums

ShortCodes UI
[resolved] Combining php and html (2 posts)

  1. dinkelgodis
    Member
    Posted 10 months ago #

    Hello! Great plugin!

    I want to combine php and html in my shortcode. This is what I have done:

    ?><div id="tjanster">
    <?php $i=1; ?>
    <?php query_posts('category_name=tjanster'); ?>
        <?php if (have_posts()) : while (have_posts()) : the_post();?>
        		<div class="tjanster_post tjanster_nr_<?php echo $i ?>">
                	<?php the_content(); ?>
                </div>
                <?php $i++; ?>
        <?php endwhile; endif; ?>
    </div>

    [Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum's parser.]

    And it works almost perfectly. The problem is that it goes through the loop twice. The first time it reads the html and the second time it doesnt. What can i do to solve this? :)
    Regards,
    Anna

    http://wordpress.org/extend/plugins/shortcodes-ui/

  2. Bainternet
    Member
    Plugin Author

    Posted 10 months ago #

    Not use query__posts for starters.

    Its meant to modify the main query which is clearly not the case here.
    And for a better solution you should use a code that returns php and not echo's it out so try this:

    $html = '<div id="tjanster">';
    $i=1;
    $qs = new get_posts(array('category_name'=> 'tjanster'));
    foreach ($qs as $q) {
    	$html .= '<div class="tjanster_post tjanster_nr_'. $i .'">'.$q->post_content.'</div>';
    	$i++;
    }
    $html .= '</div>';
    return $html;

Reply

You must log in to post.

About this Plugin

About this Topic