Support » Plugins and Hacks » Display Previous Post Content hack

  • I have been working on a hack to display the previous content but somehow I am getting erros messages:
    Here is the code:

    <?php $prev_id = get_previous_post(TRUE); $prevpost = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID={$prev_id->ID}", 'ARRAY_A'); $prev_date = strtotime($prevpost['post_date']); ?>
    <h2><?php previous_post_link('%link') ?></h2>
    <small>Posted: <?php echo date('l, F jS, Y', $prev_date); ?></small>
    <?php echo $prevpost['post_content']; ?>

    Any Help is welcome and everybody will use it later.
    Thank you Guys!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Andrew Ozz


    Hmm, you are pulling the post directly from the db going around the api? Why not try this:
    $prevpost = get_posts('numberposts=1&offset=1');

    Hi azaozz,
    Thanks for the reply but I didn’t get the content or anything. It came out blank, Any ideas??

    Moderator Andrew Ozz


    get_posts returns an object, use echo $prevpost->post_content to get the content, echo $prevpost->post_title; to get the title, echo $prevpost->comment_count; to get the number of comments, etc.

    More here:

    I Don’t know if I am doing something wrong but
    I am not getting anything, here is the code after
    the changes:

    <?php $prev_id = get_previous_post(TRUE); $prevpost = get_posts('numberposts=1&offset=1'); $prev_date = strtotime($prevpost['post_date']); ?>
    			<h2><?php echo $prevpost->post_title ?></h2>
    			<small>Posted: <?php echo date('l, F jS, Y', $prev_date); ?></small><br />
    			<?php echo $prevpost->post_content ?>

    really thank you again.

    Moderator Andrew Ozz


    Yes, get_posts actually returns an array of objects. Also you don’t need the ID and the date parts. Try this:

    <?php $prevpost = get_posts('numberposts=1&offset=1'); ?>
      <h2><?php echo $prevpost[0]->post_title; ?></h2>
      <small>Posted: <?php echo date('l, F jS, Y', $prevpost[0]->post_date); ?></small><br />
      <?php echo $prevpost[0]->post_content; ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display Previous Post Content hack’ is closed to new replies.