0

Below is my code, as i try to add html / css tags my page errors. I am using wordpress

<?php
    // check if the repeater field has rows of data
    if( have_rows('lyrics') ):
    // loop through the rows of data
        while ( have_rows('lyrics') ) : the_row();
            // display a sub field value
            the_sub_field('lyric_title'); 

            // display a sub field value
            the_sub_field('lyrics_text');
        endwhile;
    else :
        // no rows found
    endif;
?>

I cannot figure out to:

  1. how to add a div around each repeat for styling
  2. Add a h3 to the_sub_field('lyric_title');
  3. Add a p with a class to the_sub_field('lyrics_text');

3 Answers 3

1

Its because HTML does not go into PHP. You have two options.

1) Break up the php tags

<?php if( have_rows('lyrics') ): ?>
  <?php while ( have_rows('lyrics') ) : the_row(); ?>  
    <div class="">
      <?php  the_sub_field('lyric_title');  ?>  
      <?php the_sub_field('lyrics_text'); ?>  
    </div>
  <?php endwhile; ?>  
<?php else : ?>  
  <div class="">
    Text
  </div>
<?php endif; ?>  

2) Echo the HTML

<?php
if( have_rows('lyrics') ) :
  while ( have_rows('lyrics') ) : the_row();
    echo "<div class=''>";
     the_sub_field('lyric_title'); 
     the_sub_field('lyrics_text');
    echo "</div>";
  endwhile;
else :
  echo "<div class=''>";
  echo "Text";
  echo "</div>";
endif;
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

<?php

// check if the repeater field has rows of data
if( have_rows('lyrics') ):

// loop through the rows of data
  while ( have_rows('lyrics') ) : 
?>
<div>
  <?php the_row(); ?>

  // display a sub field value
  <h3> 
    <?php the_sub_field('lyric_title'); ?>
  </h3>
// display a sub field value
  <p class="class">
    the_sub_field('lyrics_text');
  </p>
</div>
endwhile;

else :

// no rows found

endif;

?>

You can very easly combine PHP with HTML within your Wordpress template like this. Just be sure to keep it readable.

Comments

0

inside PHP markers, use echo command to print HTML:

echo '<div class="enclosed">';
while ( have_rows('lyrics') ) : the_row();
echo '</div>';

or you can close php code, write tag as is and open php code again:

//PHP code before
?>
<div class="enclosed">
<?php
while ( have_rows('lyrics') ) : the_row();
?>
</div>
<?php
//PHP code continues here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.