WordPress.org

Ready to get started?Download WordPress

Forums

Display Previous Post Content hack (6 posts)

  1. studgate
    Member
    Posted 7 years ago #

    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!

  2. Andrew Ozz
    WordPress Dev
    Posted 7 years ago #

    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');

  3. studgate
    Member
    Posted 7 years ago #

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

  4. Andrew Ozz
    WordPress Dev
    Posted 7 years ago #

    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:
    http://codex.wordpress.org/Template_Tags/get_posts

  5. studgate
    Member
    Posted 7 years ago #

    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.

  6. Andrew Ozz
    WordPress Dev
    Posted 7 years ago #

    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; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic