1
votes

I am working on a wordpress theme and my homepage consists of 3 parts (see attached picture):

  1. 4 featured pages section (red)
  2. 3 featured posts section with 'news' as post category (orange)
  3. 3 featured posts section with 'blog' as post category (blue)

The 3th section is displayed on all pages of my website as a sidebar.

I want to know if I am using a correct way to query data in wordpress, to get the needed data/content like page title, page thumbnail, post title etc...

I read it is not recommended to use query_post() so I was wondering what the correct way is. Please don't look at the html, my question is just about the php. It is important to know that my code does work, so I don't have error's.

My code to get the data to use in the 1st section (red):

<div id="hiContainer">
                    <?php $i = 1;
                    query_posts( 'posts_per_page=4&cat=3' );
                    if ( have_posts() ) { 
                        while ( have_posts() ) {
                            the_post();
                            if ( $i < 4 ) { 
                                echo '<div class="headerItem">';
                            } 
                            else {
                                echo '<div id="hiLast">';
                            }
                            if ( has_post_thumbnail() ) {
                                the_post_thumbnail( 'hiThumb' ); 
                            }
                    ?>
                            <div class="hiTitle">
                                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                            </div>  
                    <?php   echo '</div>';
                            $i += 1;
                        }
                    }
                    else { 
                        echo '<p>';
                        _e( 'Sorry, no posts matched your criteria.' );
                        echo '</p>';
                    }
                    ?>                      
                </div>
                <?php wp_reset_query(); ?>

My code to get the data to use in the 2nd section (orange):

<div id="niContainer">
                <?php 
                query_posts( 'posts_per_page=3&cat=5' );
                if ( have_posts() ) { 
                    while( have_posts() ) {
                        the_post();             
                        echo '<div class="newsItem">';
                        if ( has_post_thumbnail() ) {
                            echo '<div class="niImage">';
                                the_post_thumbnail( 'niThumb' ); 
                            echo '</div>';
                        }
                ?>
                        <div class="niTitle">
                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                        </div>  
                        <div class="niExcerpt">
                            <p><?php echo get_the_excerpt(); ?></p>
                        </div>
                        <div class="niReadMore">
                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">Lees meer <span style="color:#c73f20;">&#187;</span></a>
                        </div>
                <?php   
                        echo '</div>';
                    }
                }
                ?>  
                </div>

My code to get data to use in 3th section (blue):

for ( $i = 1; $i < 4; $i++ ) {
                        $post_id = ot_get_option( 'post'.$i );
                        $post = get_post($post_id);
                        $author_id = $post->post_author;
                        $author_first_name = get_the_author_meta( 'first_name', $author_id );
                        $author_last_name = get_the_author_meta( 'last_name', $author_id );
                        echo '<div class="rsbPost">';
                                echo '<div class="rsbpThumb">'; 
                                    echo get_the_post_thumbnail( $post_id ,'rsbpThumb' );
                                echo '</div>';
                            echo '<div class="rsbpTitle">';
                                echo '<a href="'; 
                                echo get_permalink( $post_id ); 
                                echo '"title="'; 
                                echo $post->post_title;
                                echo '">'; 
                                echo $post->post_title;
                                echo'</a>';
                            echo '</div>';
                            echo '<div class="rsbpMeta">';
                                if ( !empty( $author_first_name ) || !empty( $author_last_name ) ) {
                                    echo $author_first_name . " " . $author_last_name;
                                }
                                else {
                                    echo 'Redactie';
                                }                               
                                echo ', ';
                                echo mysql2date('j F Y', $post->post_date);
                            echo '</div>';
                        echo '</div>';          
                }

enter image description here

1
Nice answer by Nathan and there's more to learn here: When should you use WP_Query vs query_posts() vs get_posts()? - brasofilo

1 Answers

3
votes

It's best to avoid using query_posts(). There's a warning about exactly that on the codex page: http://codex.wordpress.org/Function_Reference/query_posts

So given that what is the solution?

New instances of WP_Query. It works almost exactly the way you've been doing it.

$section1 = new WP_Query( array(
    'posts_per_page' => 4,
    'cat'            => 3,
) );

You can find out more about the possible arguments here: http://codex.wordpress.org/Class_Reference/WP_Query

Then replacing your current loop you would do:

if ( $section1->have_posts() ) : while ( $section1->have_posts() ) : $section1->the_post();

I named it section 1 but it would be wise to use a more appropriate name. $header_posts for example.

Create 3 instances for your three areas.

To take this a step further you could build this into home.php / index.php / front-page.php rather than setting the homepage to a static page. Then modify the main query (area marked as 2) using the pre_get_posts filter but that's outside the scope of your question.