php - Which function do I need in my wordpress theme? query_posts(), get_posts(), get_post() -
i working on wordpress theme , homepage consists of 3 parts (see attached picture):
- 4 featured pages section (red)
- 3 featured posts section 'news' post category (orange)
- 3 featured posts section 'blog' post category (blue)
the 3th section displayed on pages of website sidebar.
i want know if using correct way query data in wordpress, needed data/content page title, page thumbnail, post title etc...
i read not recommended use query_post() wondering correct way is. please don't @ html, question php. it important know code work, don't have error's.
my code data use in 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 criteria.' ); echo '</p>'; } ?> </div> <?php wp_reset_query(); ?>
my code data use in 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 data 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>'; }
it's best avoid using query_posts()
. there's warning on codex page: http://codex.wordpress.org/function_reference/query_posts
so given solution?
new instances of wp_query. works way you've been doing it.
$section1 = new wp_query( array( 'posts_per_page' => 4, 'cat' => 3, ) );
you can find out more possible arguments here: http://codex.wordpress.org/class_reference/wp_query
then replacing current loop do:
if ( $section1->have_posts() ) : while ( $section1->have_posts() ) : $section1->the_post();
i named section 1 wise use more appropriate name. $header_posts
example.
create 3 instances 3 areas.
to take step further build home.php / index.php / front-page.php rather setting homepage static page. modify main query (area marked 2) using pre_get_posts
filter that's outside scope of question.
Comments
Post a Comment