Dragon54
Dragon54

Reputation: 313

Echo mixed code

I'm trying to echo a mixed string containing html code and PHP code but every possible thing I tried didn't work out.

Basically I want to do is looping the following code for 3 times. Giving me 3 divs with number incrementation in the name every time.

This code is going to be used in a wordpress template.

My normal code without echoing is the following:

<div id="gallery <?php $c; ?>">
  <?php query_posts('post_type=portfolio'); ?>
  <?php if(have_posts()) : ?>
    <?php while(have_posts()) : the_post(); ?>
      <?php if ( has_post_thumbnail() ) : ?>
        <a href="<?php the_permalink(); ?>" 
           title="<?php the_title_attribute(); ?>" >
           <?php the_post_thumbnail(); ?>
        </a>
      <?php endif; ?>
      <h1><?php the_title(); ?></h1>
      <a href="<?php the_permalink(); ?>" 
         title="<?php the_title_attribute(); ?>" > 
         <h2>View Project</h2>
      </a>
      <?php the_content() ?>
    <?php endwhile; ?>
  <?php endif; ?>
</div>

I'm fairly new to PHP so I don't know how to echo this the proper way. My for-loop has already been setup.

I hope you guys can help me out.

Kind regards Dragon54

Upvotes: 0

Views: 130

Answers (3)

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174957

You need to echo it:

<div id="gallery_<?php echo $c; ?>">

Also, spaces in IDs are not allowed, I switched it to an underscore.

Upvotes: 0

user1454661
user1454661

Reputation:

Learn concatenation PLEASE, it's a big must when programming PHP/HTML! Then you can simply put that block inside a foreach or a loop of some kind, depending how is your news system designed.

<?php
echo '<div id="gallery_'.$c.'">';
query_posts('post_type=portfolio');

while(have_posts()) {
    the_post();
    if ( has_post_thumbnail() ) { 
        echo '<a href="'.the_permalink().'" title="'.the_title_attribute();.'" '.the_post_thumbnail().'</a>';
    }
}

echo '.<h1>'.the_title().'</h1>
       <a href="'.the_permalink().'" title="'.the_title_attribute().'">link</a>
      </div>';
?>

Upvotes: 0

gherkins
gherkins

Reputation: 14973

something like:

for($i=1; $<=3; $++){
 echo '<div id="gallery gallery_'.$i.'">' . get_the_permalink() . '</div>';
}

should do the trick (see http://php.net/manual/de/language.operators.string.php)

But be aware that some of the Wordpress helper functions directly print/echo stuff themselves. Mostly you'll find something that returns the value instead

see http://codex.wordpress.org/Function_Reference/get_permalink (returns) vs http://codex.wordpress.org/Function_Reference/the_permalink (prints)

Upvotes: 1

Related Questions