WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Avoiding nested php code (10 posts)

  1. Kathy_P
    Member
    Posted 8 years ago #

    I am using the Customizable Post Listings plugin by Scott Reilly and running WordPress 1.5.2. The plugin works fine, but I am trying to write a tiny bit of code so that it will show up in my sidebar except on the main page. At the moment, my code looks like this:
    <?php if (!is_home()) { ?>
    <h2>Recent Posts</h2>
    <ul>
    <?php c2c_get_recent_posts(5, "<li>%post_URL%<br />%post_excerpt_short%</li>"); ?>
    </ul>
    <?php } ?>

    After it didn't work, I realized it was because I had php code nested inside other php code. The only reason this occurred to me is because someone on this forum corrected the same type of error for me on another occasion.

    If someone proficient in php and WordPress could suggest a way around this problem I'd be most grateful.

  2. jkoontz
    Member
    Posted 8 years ago #

    Don't close the php and just echo the HTML you're outputing.

  3. Kathy_P
    Member
    Posted 8 years ago #

    I know so little php that I had to look up the syntax for echo before I could try it out. Is this what you meant:

    <?php if (!is_home()) {
    echo <h2>Recent Posts</h2>
    echo

    <ul>
    c2c_get_recent_posts(5, "
    <li>%post_URL%%post_excerpt_short%</li>
    ");
    echo </ul>

    } ?>'

    If someone could check my syntax to let me know if I'm using echo correctly, and in general, if it's coded right, I'd feel better about sticking it in my sidebar. Thanks.

    Update: I am having trouble getting the code to show up properly because even though I encased the whole thing in backticks it interpreted the ul. And now there are breaks in there that I didn't put in. If you can't figure out what I meant, I will try again later. have to go to work now.

  4. TechGnome
    Moderator
    Posted 8 years ago #

    Hold the phone.... there's nothing wrong with the original code.... First off... it really isn't nested PHP code... it goes in & out of PHP mode... and does it like it should. I don't see a reason why it wouldn't work. It's the same kind of thing I do on my site.

    Which brings me to "how" do you know it's not working?

    -tg

  5. gregh
    Member
    Posted 8 years ago #

  6. Kathy_P
    Member
    Posted 8 years ago #

    To answer TechGnome:
    I first used this "trick" on my custom 404 page, where I wrapped it around my Google ad script, so it would run on every page except the 404, like this:
    <?php if (!is_404()) { ?>
    <ul>Google script</ul>
    <?php } ?>

    That worked. When I tried it on the customizable post listings plugin code (as above in first post), it didn't work. In other words, despite the code it showed the Recently Posted section of my sidebar on my home (index) page.

    Looking at the code, I saw that php code pulling in the recent posts was sandwiched between a php line that had a curly-brace and another php line that closed the curly brace. And I surmised, incorrectly or not, that I was nesting the php and it didn't like that.

    Now, using the link that gregh provided (and I did look in the manual, but I really didn't know where to look), let's try again:
    <?php if (!is_home()) :
    echo <h2>Recent Posts</h2>;
    echo <ul>;
    c2c_get_recent_posts(5, "<li>%post_URL%<br />%post_excerpt_short%</li>");
    echo </ul>;
    endif;
    ?>

    gregh, does that look right?

  7. Kathy_P
    Member
    Posted 8 years ago #

    Okay, I just went to try the above code in my sidebar. But just for kicks I tried it the first way, as in my original post, again. And just as TechGnome said, it did work this time. I don't know if my browser cache just didn't refresh or if I thought I saved my changes but actually hadn't, or if it was some other too-late-at-night goof. But it does work now.

    As for the second method of coding it, that didn't work. I got this error: "Parse error: parse error, unexpected '<', expecting ',' or ';' in /home/path-to-theme/sidebar.php on line 21" Line 21 is the line that says echo <h2>Recent Posts</h2>;

    Since the first method is working, I now consider this a learning exercise only. If anyone wants to troubleshoot the second method for the general education of me and anyone else who comes along, I promise to take notes and learn. But I no longer need to know the second method to accomplish my goal, so I will mark this resolved after a day or two.

  8. mniepert
    Member
    Posted 8 years ago #

    There is no nested PHP code in the entire post. "Nested" php will always give you an error message.

    Gregh just posted a link to an alternative syntax in php. That doesn't mean the "standard" if-else doesn't work.

    (btw: Your last code snippet is completely messed up.)

    Mathias

  9. Austin Matzko
    Member
    Posted 8 years ago #

    you need to include echo statements in quotation marks, like so:
    echo '<h2>Recent Posts</h2>';

  10. Kathy_P
    Member
    Posted 8 years ago #

    Thank you filosofo for pointing out the echo problem. Mathias, the c2c line broke in two and I don't know why. If there are other problems besides the echo syntax let it be a warning for those who might read this later. I wouldn't know how to fix it if I'm not told. I never claimed to even be a newb when it comes to php. What little I know I've gleaned from the Codex, from poring over templates coded by my betters and trying to intelligently use pieces from them, and, just recently, from the php online manual.

    I think it's great that so many people are willing to help explain things to those of us who are learning by trial and error. Thanks to everyone who responded to this thread.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags