Troubleshooting get_posts() on Static Home Page
-
I have a site set up to function only on WordPress, but I am having a problem I have never come across before. I have my home page set to be a static page, while my posts are displayed as a portfolio. I also wanted to have the ability to display my posts that are not Portfolio Entries on the home page as an updates section.
After doing some reading through the Codex I decided that get_posts(); would be the most efficient way to do this. After developing and running my PHP and HTML I have run across my issue. Seen here: https://chetgproductions.com.
After the posts are displayed the template tags seem to become confused. Where I use the_title(); it displays the title of the page, not the title of that post, the same thing happens with the date.
Here is the coding I am using:
<?php $updates = get_posts('category=-1'); foreach($updates as $post) : setup_postdata($post); ?> <h2><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></h2> <small>Posted on <?php the_time('F jS, Y'); ?></small> <?php the_content(); ?> <?php endforeach; ?>
The coding is derived directly from the WP Codex, and it works exactly as it should, except these minor details. If any of you have some insight on how to address this problem please pass along your knowledge. It’s greatly appreciated.
*UPDATE*
I’ve done some more reading into the Codex, and have employed this code:
<?php $updates = get_posts('category=-1'); foreach($updates as $post) : setup_postdata($post); ?> <h3><?php echo $post->post_title; ?></h3> <small>Posted on <?php echo $post->post_date; ?></small> <?php the_content(); ?> <?php endforeach; ?>
Querying the table columns proved successful, however this is not how I would like to do this. For some reason the setup_postdata(); command just isn’t letting me access the post’s meta data as I would like to. I can live with this temporary fix for now, but would really appreciate the advice of anyone out there on this.
- The topic ‘Troubleshooting get_posts() on Static Home Page’ is closed to new replies.