WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] RunPHP -- getting error sometimes (5 posts)

  1. Bodhipaksa
    Member
    Posted 7 years ago #

    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?

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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.

  3. Bodhipaksa
    Member
    Posted 7 years ago #

    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?

  4. Bodhipaksa
    Member
    Posted 7 years ago #

    Resolved! On further digging around the forum I found a link to the Customizable Post Listings plugin at http://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.

  5. Piet
    Member
    Posted 7 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags