0

I'm trying to automate one of our posting aspects. We post a lot of lyrics and have a system of meta boxes which we use to input various pieces of information about each set of lyrics we post. One aspect that is a little annoying is having to search & replace <li></li>tags to the start and end of each new line of lyrics. I was hoping to have PHP echo this automatically but my code is not working at the moment. Here is my untouched code which outputs a verse of lyrics (we use wpautopso that line breaks are respected):

<?php if ( get_post_meta($post->ID, '_cmb_lyrics_1', true) ) : ?>
<?php $lyrics_1 = get_post_meta($post->ID, '_cmb_lyrics_1', true) ?>
<ul>
<?php echo wpautop( $lyrics_1) ; ?>
</ul>
<?php endif; ?> 

And here is my attempt at outputting <li>tags, which didn't work. :(

<?php if ( get_post_meta($post->ID, '_cmb_lyrics_1', true) ) : ?>
<?php $lyrics_1 = get_post_meta($post->ID, '_cmb_lyrics_1', true) ?>
<ul>
<?php 
echo "<li>\n"; wpautop( $lyrics_1) ; "</li>\n"; ?>
</ul>
<?php endif; ?>

Any help appreciated. Thanks.

3
  • what exactly is in $lyrics_1? is this multiple lines?
    – Michael
    Feb 14, 2013 at 11:59
  • See my answer to the identical question wordpress.stackexchange.com/a/84815/11761
    – Max Yudin
    Feb 14, 2013 at 12:44
  • @MaxYudin. I could not get that to work - returned error unexpected { .
    – JMB
    Feb 14, 2013 at 21:19

1 Answer 1

0

Try:

<li>
    <?php echo wpautop( $lyrics_1);?>
</li>

instead of this line:

<?php echo "<li>\n"; wpautop( $lyrics_1) ; "</li>\n"; ?>

edit: thanks @Toscho for the syntax edit, I made a hasty error in my earlier revision ;-)

5
  • That doesn't work unfortunately - it was the first thing I tried. It returns like this: <ul> <li><p>Hi<br/> Ok<br/> Let's go<br/> Fo sho</p> </li> </ul> It doesn't apply the <li>to each line, rather before and after each block of lyrics.
    – JMB
    Feb 14, 2013 at 11:39
  • do you want to have each p block within li tag, like: <li><p>...</p></li> ?
    – birgire
    Feb 14, 2013 at 11:58
  • if so you can try: <?php echo str_replace(array('<p>','</p>'),array('<li><p>','</p></li>'),wpautop( $lyrics_1));?>
    – birgire
    Feb 14, 2013 at 12:08
  • This won't work unfortunately. wpautop adds <br /> tags to mark the line end. I changed your search value </p>for <br />and tried \nin place of <p>but to no avail. I got the text returned as one long <li>item!
    – JMB
    Feb 14, 2013 at 21:23
  • so you want to change: <ul> <p>Hi<br/> Ok<br/> Let's go<br/> Fo sho</p> </ul> into this: <ul> <li>Hi</li> <li>Ok</li> <li>Let's go</li> <li>Fo sho </li> </ul> ?
    – birgire
    Feb 15, 2013 at 17:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.