• Hi all,

    I’ve created a new page (not a template) and called it “home”, on this page I have a few floats and I need to put the following code into a specific float:

    <h4>Latest Blog Posts</h4>
    		<?php $postslist = get_posts('numberposts=5&order=DSC&orderby=date'); foreach ($postslist as $post) : setup_postdata($post);?>
    		<h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
     		<small>
    			<?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago'; ?>
    			<span>?</span>
    			<a href="<?php the_permalink() ?>#comments" title="View comments about <?php the_title(); ?>"><?php comments_number('No Comments', '1 Comment', '% Comments' );?></a>
    		</small>
    		<br />
    		<br />
     		<?php endforeach; ?>

    The problem I’m having is that the wysiwyg is breaking the code. How can I call this into the page without it breaking?

    It’s really important that it’s on the page and not the template.

    Cheers

Viewing 8 replies - 1 through 8 (of 8 total)
  • put code in through the html editor, not the wysiwyg

    also, php code is not supported in posts/pages normally, there are plugins available which allow php to be inserted. just search the plugin repo for php code

    Thread Starter rob_the_villain

    (@rob_the_villain)

    Thanks for replying.

    I’ve tried through the html editor and it still breaks it. Shall have a look for a plugin.

    Thanks

    Thread Starter rob_the_villain

    (@rob_the_villain)

    I’ve got a plugin that executes the code, just need some adjustments to it.

    <h4>Latest Blog Posts</h4>
    		<?php $postslist = get_posts('numberposts=5&order=DSC&orderby=date'); foreach ($postslist as $post) : setup_postdata($post);?>
    		<h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
     		<small>
    			<?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago'; ?>
    			<span>?</span>
    			<a href="<?php the_permalink() ?>#comments" title="View comments about <?php the_title(); ?>"><?php comments_number('No Comments', '1 Comment', '% Comments' );?></a>
    		</small>
    		<br />
    		<br />
     		<?php endforeach; ?>

    The problem I have is that instead of picking up the post titles, links to posts etc etc it’s just picking up the homepage info. So each title says “home” rather than the posts title and each one just reloads the home page. Can anyone help?

    why not just make a template? Or insert the code into a template? If I may ask?

    trying to put a loop in a post pretty much puts a loop in the middle of your loop…. I don’t know how that’s going to work out…

    You may end up with some kind of looping loop time space continuum thing, which sounds bad…..

    Thread Starter rob_the_villain

    (@rob_the_villain)

    Haha, yep, not really thinking it through, I’ll just get it to grab a template and hopefully it’ll act as I want it. Thanks

    Thread Starter rob_the_villain

    (@rob_the_villain)

    Oh dear, that didn’t go well!

    I just put into the page to get another template (thanks to the php plugin), I then put this code into the new template:

    <?php
    /**
     * @package WordPress
     * @subpackage Default_Theme
     */
    ?>
    
    <h4>Latest Blog Posts</h4>
    <?php $postslist = get_posts('numberposts=5&order=DSC&orderby=date'); foreach ($postslist as $post) : setup_postdata($post);?>
    <h5><a href="<?php the_permalink(); ?>">
      <?php the_title(); ?>
      </a></h5>
    <small> <?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago'; ?> <span>?</span> <a href="<?php the_permalink() ?>#comments" title="View comments about <?php the_title(); ?>">
    <?php comments_number('No Comments', '1 Comment', '% Comments' );?>
    </a> </small> <br />
    <br />
    <?php endforeach; ?>

    I’m getting this error:

    Fatal error: Call to undefined function get_latestposts() in /home/metahousemedia/metahousemedia.com/wp-content/plugins/php-execution-plugin/includes/class.php_execution.php(273) : eval()'d code on line 8

    Thread Starter rob_the_villain

    (@rob_the_villain)

    Solved with putting the following code directly into the wysiwyg using the php plugin:

    <?php
     global $post;
     $myposts = get_posts('numberposts=5');
     foreach($myposts as $post) :
       setup_postdata($post);
     ?>
     <h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
    <small><?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago'; ?> <span>?</span> <a href="<?php the_permalink(); ?>#comments" title="View comments about <?php the_title(); ?>"><?php comments_number('No Comments', '1 Comment', '% Comments' );?></a></small>
    <br />
    <br />
     <?php endforeach; ?>

    Cheers for the help Rev.

    Sure thing, glad you got what you were after!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Put php into a page (ie not a template) through wysiwyg’ is closed to new replies.