Getting different sets of
posts in your theme
Roman Rus
@RomSocial
www.websitesupgrade.com
The Main Loop
The loop:
while (have_posts()) :
the_post();
the_permalink();
the_post_thumbnail();
the_content();
the_excer...
query_posts()
The loop:
while (have_posts()) :
the_post();
...
endwhile;
Second time same loop:
rewind_posts();
while (hav...
get_posts()
Most commented posts:
$args = array(
'numberposts' => 4,
'orderby' => ‘comment_count',
'order' => 'DESC');
$th...
Most commented posts
Featured posts
Most recent posts from
selected categories, then
second most recent
posts, etc.
Most r...
get_posts( $args);
get_posts parameters:
$args = array(
'numberposts‘ => 1,
'posts_per_page' => 5,
'offset' => 0,
'categor...
How it is done?

Most read posts

Most commented posts

Featured posts

Most recent post, second
most recent post, etc.
fr...
Sub-categories listing?
Plugin: Categories Images
z_taxonomy_image_url();

$args = array(
'type' => 'post',

Plugin: Taxon...
Thumbnails
Plugin: Categories Images
Plugin: Regenerate Thumbnails
Plugin: Thumbnail Upscale
In functions.php add differen...
Thank you for your attention!
Questions?
Roman Rus
@RomSocial
www.websitesupgrade.com
Upcoming SlideShare
Loading in …5
×

Wordpress multiple loops

1,101 views
845 views

Published on

Presentation from Ottawa Wordpress Meetup - multiple wordpress loops.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,101
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Wordpress multiple loops

  1. 1. Getting different sets of posts in your theme Roman Rus @RomSocial www.websitesupgrade.com
  2. 2. The Main Loop The loop: while (have_posts()) : the_post(); the_permalink(); the_post_thumbnail(); the_content(); the_excerpt(); the_time(); the_author(); comments_template( ); endwhile;
  3. 3. query_posts() The loop: while (have_posts()) : the_post(); ... endwhile; Second time same loop: rewind_posts(); while (have_posts()) : the_post(); ... endwhile; Run a different loop: query_posts( 'category_name=cat2& posts_per_page=10'); while (have_posts()) : the_post(); ... endwhile; wp_reset_query();
  4. 4. get_posts() Most commented posts: $args = array( 'numberposts' => 4, 'orderby' => ‘comment_count', 'order' => 'DESC'); $theposts = get_posts( $args); If in functions.php etc.: global $post; foreach($theposts as $post) : setup_postdata($post); the_permalink(); the_post_thumbnail(); the_title(); ... endforeach; wp_reset_postdata();
  5. 5. Most commented posts Featured posts Most recent posts from selected categories, then second most recent posts, etc. Most read posts Most shared on social media posts List of subcategories with images
  6. 6. get_posts( $args); get_posts parameters: $args = array( 'numberposts‘ => 1, 'posts_per_page' => 5, 'offset' => 0, 'category' => '', 'orderby' => '', 'order' => 'DESC', 'include' => '', 'exclude' => '', 'meta_key' => '', 'meta_value' => '', 'post_type' => 'post', 'post_mime_type' => '', 'post_parent' => '', 'post_status' => 'publish', 'suppress_filters' => true ); $theposts = get_posts( $args); ORDERBY options: 'none' 'ID' 'author' 'title' 'date' 'modified' 'parent' 'rand' 'comment_count' 'menu_order' 'meta_value' // 'meta_key‘ 'meta_value_num' // 'meta_key‘ 'meta_query' => array( array( 'key' => 'featured', 'value' => 'yes', )); 'tax_query' => array( array( 'taxonomy' => 'genre', 'field' => 'slug', 'terms' => 'jazz' ) )
  7. 7. How it is done? Most read posts Most commented posts Featured posts Most recent post, second most recent post, etc. from individual categories Plugin: Meta Box Define custom field, ++ on single post views or Plugin: WP-PostViews Most shared on social media posts Plugin: Most Shared Posts Name of meta tag 'meta_key' => '_msp_total_shares' Sort by 'comment_count' or 'date' (default) use 'offset', 'category', 'numberposts' Sort by 'date' (default) with filter 'meta_key' => '_fp', 'meta_value' => '1', Sort by 'meta_value' or 'meta_value_num'
  8. 8. Sub-categories listing? Plugin: Categories Images z_taxonomy_image_url(); $args = array( 'type' => 'post', Plugin: Taxonomy TinyMCE (not working?) 'child_of' => 0, Plugin: WP Category Permalink (for SEO) 'parent' => '', 'orderby' => 'name', $categories= get_categories($args); foreach ($categories as $category) : $imgurl = z_taxonomy_image_url( $category->term_id); get_category_link( $category->term_id); echo( $category->slug); echo( $category->cat_name;); endforeach; 'order' => 'ASC', 'hide_empty' => 1, 'hierarchical' => 1, 'exclude' => '', 'include' => '', 'number' => '', 'taxonomy' => 'category', 'pad_counts' => false );
  9. 9. Thumbnails Plugin: Categories Images Plugin: Regenerate Thumbnails Plugin: Thumbnail Upscale In functions.php add different sizes for thumbnails: add_image_size(‘n_toprow', 164, 105, true); add_image_size(‘n_mainblog', 300, 260, true); .. In the loop etc. add: the_post_thumbnail( ' n_toprow' ); Bad hack for categories: $imgurl = z_taxonomy_image_url($category->term_id); $imgurlmod = str_replace ( '.jpg' , '-300x260.jpg' , $imgurl );
  10. 10. Thank you for your attention! Questions? Roman Rus @RomSocial www.websitesupgrade.com

×