WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] PHP in static page to run LOOP (3 posts)

  1. jpanizzoli
    Member
    Posted 7 years ago #

    I've spent all night going through the WordPress DOCS and support forums trying to get this to work...

    I've set a static page as the "Front Page Display" to act as my home page. I want to add a LOOP to show the most recent posts within the home page content, but can't add PHP without a PLUGIN. I've tried using Exec-PHP and RunPHP to do this.

    When I run some PHP test code through, everything seems to work fine. But when I add the PHP code for adding a loop-- I get an error everytime (referencing the PLUGIN file)! This is the code I added for a mini-loop:

    <?php
    $how_many=5; //How many posts do you want to show
    require_once("wp-config.php"); // Change this for your path to wp-config.php file ?>
    
    <ol id="whats-new">
    <?
    $news=$wpdb->get_results("SELECT <code>ID</code>,<code>post_title</code> FROM $wpdb->posts
    WHERE <code>post_status</code>= \"publish\" ORDER BY 'ID' DESC LIMIT ".$how_many);
    foreach($news as $np){
    printf ("<li><a href=\"index.php?p=%s\">%s</a></li>", $np->ID,$np->post_title);
    }?>
    </ol>

    Am I missing something here? Obviously, the PLUGINS are working because small snippets of PHP work fine. Just can't get a loop to work!

    Any suggestions???

  2. jpanizzoli
    Member
    Posted 7 years ago #

    No one?

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

    Why are you trying to put PHP in the post body anyway? That's weird. I know about ExecPHP and such. I don't care for them much. And the reason your loop isn't working is because ExecPHP and those sort of things were designed for small bits of PHP code inclusion, not to hold an entire Loop. I could be more technical about it, but you probably don't care. ;)

    Anyway, since you're putting this on a static Page to begin with, put your code into a Page Template and then set that Page to use that Template.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags