• Resolved Bodhipaksa

    (@haecceity)


    Hi,

    I’m experimenting with runPHP so that I can include dynamically-updated lists of links on a new site.

    If I run the following code, all is well:

    <div class="box">
    <ul><?php get_archives('postbypost','10','custom','<li>','</li>'); ?></ul>
    </div>

    But if I try to run this I get an error message:

    <div class="box">
    <ul>
    <?php $archive = get_posts('numberposts=20&cat=1'); ?>
    <?php foreach($archive as $post) : ?>
    <li><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
    </div>

    The message is:

    Parse error: parse error, unexpected $ in /htdocs/test/wp-content/plugins/runPHP/runPHP.php(359) : eval()’d code on line 1
    Parse error: parse error, unexpected T_ENDFOREACH in /htdocs/test/wp-content/plugins/runPHP/runPHP.php(359) : eval()’d code on line 1

    Anyone have any ideas why this should be?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    It has to do with how runPHP does things. It doesn’t simply run your whole post as if it was php (although that would be simpler…). Instead, it pulls each section of stuff inside <?php…?> tags and runs it separately.

    The upshot here is that a) you don’t have global scope and b) each section of <?php…?> code is separate from all others.

    Try this code instead:
    <div class="box">
    <ul>
    <?php
    $archive = get_posts('numberposts=20&cat=1');
    foreach($archive as $post):
    echo '<li><a href="';
    the_permalink();
    echo '" rel="bookmark">';
    the_title();
    echo '</a></li>';
    endforeach;
    ?>
    </ul>
    </div>

    The key is to keep all the loop and related variables inside the same <?php…?> block. Otherwise they will be in different scopes and thus not work.

    Thread Starter Bodhipaksa

    (@haecceity)

    Thanks.

    No error messages now, but the links that appear all have the title and permalink of the page on which they’re showing up, rather than the posts in category 1.

    Any more thoughts?

    Thread Starter Bodhipaksa

    (@haecceity)

    Resolved! On further digging around the forum I found a link to the Customizable Post Listings plugin at https://www.coffee2code.com/archives/2004/08/27/plugin-customizable-post-listings/

    All I then had to do was to put a simple line of code in the page and up comes a list of posts. As the name implies the plugin is in fact highly customizable.

    I appreciate your suggestion though. It was good to learn a bit more about how php works.

    Hi,
    I am having the same difficulties as Haecceity had.

    I understand what needs to be done (put the entire php coding into one php”block”), but I don’t have a clue on how to do that ??

    Basically I would like to put the following code into one so that runPHP sees it as 1 block:

    <?php query_posts('category_name=whatson&showposts=7'); ?>
    <?php while (have_posts()) : the_post(); ?>
    
    <div class="post" id="post-<?php the_ID(); ?>">
    
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?> &raquo;</a></h2>
    
    <p class="postinfo">By <?php the_author_posts_link(); ?> on <?php the_time('M j, Y') ?> in <?php the_category(', ') ?> | <?php comments_popup_link('0 Comments', '1 Comment', '% Comments'); ?><?php edit_post_link('Edit', ' | ', ''); ?>
    
    <div class="entry">
    						<?php the_content(); ?>
    </div>
    </div>   
    
    <?php endwhile; endif; ?>

    (not sure if the last <?php endwhile; endif; ?> has to be included or not…)

    I hope someone can help me with this or point me into the right direction, that would be awesome!

    Thanks,
    Piet

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘RunPHP — getting error sometimes’ is closed to new replies.