I am working on a wordpress theme and my homepage consists of 3 parts (see attached picture):
- 4 featured pages section (red)
- 3 featured posts section with 'news' as post category (orange)
- 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;">»</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>';
}