• ChetG

    (@chetg)


    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.

Viewing 8 replies - 1 through 8 (of 8 total)
  • esmi

    (@esmi)

    Personally, I would have thought query_posts might be better. Unless, of course, you also want to display content from the Page itself.

    Thread Starter ChetG

    (@chetg)

    Yeah I am also displaying a separate set of information in the page as well as these posts. You can see how I have it set up at the link. I was also told that query_posts() was only usable on the posts page, and with the loop.

    Maybe I’ve been mislead.

    esmi

    (@esmi)

    You can use query_posts on any kind of page and with multiple loops – proving you remember to reset the query each time.

    https://codex.www.ads-software.com/The_Loop#Multiple_Loops

    Thread Starter ChetG

    (@chetg)

    Okay so I implemented:

    <?php query_posts('posts_per_page=3&cat=-1');?>

    While it does display the posts, it doesn’t offer any control in display design. It’s also a great fix to my problem, and maybe I can’t do what I’m wanting to do? Though that seems implausible.

    Thread Starter ChetG

    (@chetg)

    <?php query_posts('posts_per_page=3&cat=-1');?>
    <?php while (have_posts()) : the_post(); ?>
    <h3><a href="<?php the_permalink();?>"><?php the_title();?></a></h3>
      <?php endwhile;?>

    So that gives me the ability to edit the appearance the way I want, but it’s also displaying the query twice. Any advice on that?

    You can see the example here: Follow the Link

    esmi

    (@esmi)

    Try adding wp_reset_query(); after the first Loop.

    Thread Starter ChetG

    (@chetg)

    <?php query_posts('posts_per_page=3&cat=-1'); ?>
    <?php wp_reset_query(); ?>
    <?php while (have_posts()) : the_post(); ?>
    <h3><a href="<?php the_permalink();?>"><?php the_title();?></a></h3>
      <?php endwhile;?>

    That’s what the code looks like now. However now on the page it’s just relooping the page title, over, and over, and over on into perpetuity. This is the same thing that happened when I tried just using a loop to do this.

    Maybe we’re getting closer to the source of the problem.

    Thread Starter ChetG

    (@chetg)

    <?php query_posts('posts_per_page=3&cat=-1'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <h3><a href="<?php the_permalink();?>"><?php the_title();?></a></h3>
    <small><?php the_date();?></small>
    <?php the_content(); ?>
      <?php endwhile;?>
    <?php wp_reset_query(); ?>

    I changed my coding to this, which I think may have been what you intended for me to do in the first place. Thanks for your input and walking with me through this. I should have come to the WP Forum earlier than I did.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Troubleshooting get_posts() on Static Home Page’ is closed to new replies.