Title: Combining php and html
Last modified: August 21, 2016

---

# Combining php and html

 *  Resolved [dinkelgodis](https://wordpress.org/support/users/dinkelgodis/)
 * (@dinkelgodis)
 * [13 years ago](https://wordpress.org/support/topic/combining-php-and-html/)
 * 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/](http://wordpress.org/extend/plugins/shortcodes-ui/)

Viewing 1 replies (of 1 total)

 *  Plugin Author [Bainternet](https://wordpress.org/support/users/bainternet/)
 * (@bainternet)
 * [13 years ago](https://wordpress.org/support/topic/combining-php-and-html/#post-3792247)
 * 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;
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Combining php and html’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/shortcodes-ui_dbeef6.svg)
 * [ShortCodes UI](https://wordpress.org/plugins/shortcodes-ui/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/shortcodes-ui/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/shortcodes-ui/)
 * [Active Topics](https://wordpress.org/support/plugin/shortcodes-ui/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/shortcodes-ui/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/shortcodes-ui/reviews/)

## Tags

 * [html](https://wordpress.org/support/topic-tag/html/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * 1 reply
 * 2 participants
 * Last reply from: [Bainternet](https://wordpress.org/support/users/bainternet/)
 * Last activity: [13 years ago](https://wordpress.org/support/topic/combining-php-and-html/#post-3792247)
 * Status: resolved