Wordpress multiple loops

10
Getting different sets of posts in your theme Roman Rus @ RomSocial www.websitesupgrade.com

description

Presentation from Ottawa Wordpress Meetup - multiple wordpress loops.

Transcript of Wordpress multiple loops

Page 1: Wordpress multiple loops

Getting different sets of posts in your theme

Roman Rus @RomSocialwww.websitesupgrade.com

Page 2: Wordpress multiple loops

The loop:while (have_posts()) : the_post(); the_permalink(); the_post_thumbnail(); the_content(); the_excerpt(); the_time(); the_author(); comments_template( );endwhile;

The Main Loop

Page 3: Wordpress multiple loops

wp_reset_query();

The loop:while (have_posts()) : the_post(); ...endwhile;

query_posts()

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;

Page 4: Wordpress multiple loops

foreach($theposts as $post) : setup_postdata($post); the_permalink(); the_post_thumbnail(); the_title(); ...endforeach;

wp_reset_postdata();

Most commented posts:$args = array( 'numberposts' => 4, 'orderby' => ‘comment_count', 'order' => 'DESC');$theposts = get_posts( $args);

get_posts()

If in functions.php etc.: global $post;

Page 5: Wordpress multiple loops

Most commented posts

Most recent posts from selected categories, then

second most recent posts, etc.

Featured posts

Most read posts

Most shared on social media posts

List of subcategories with images

Page 6: Wordpress multiple loops

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);

get_posts( $args);

'meta_query' => array( array( 'key' => 'featured', 'value' => 'yes', ));

ORDERBY options:

'none' 'ID' 'author' 'title' 'date' 'modified' 'parent''rand''comment_count''menu_order' 'meta_value' // 'meta_key‘ 'meta_value_num'  // 'meta_key‘

'tax_query' => array( array( 'taxonomy' => 'genre', 'field' => 'slug', 'terms' => 'jazz' ) )

Page 7: Wordpress multiple loops

Most commented posts

Most recent post, second most recent post, etc.

from individual categories

Featured posts

Most read posts

Most shared on social media posts

How it is done?

Sort by 'comment_count'or 'date' (default)

use'offset',

'category', 'numberposts'

Sort by 'meta_value' or

'meta_value_num'

Plugin: Meta Box

Define custom field, ++ on single post views

or Plugin: WP-PostViews

Plugin: Most Shared PostsName of meta tag

'meta_key' => '_msp_total_shares'

Sort by 'date' (default)

with filter'meta_key' => '_fp',

'meta_value' => '1',

Page 8: Wordpress multiple loops

Sub-categories listing?

$args = array(

'type' => 'post',

'child_of' => 0,

'parent' => '',

'orderby' => 'name',

'order' => 'ASC',

'hide_empty' => 1,

'hierarchical' => 1,

'exclude' => '',

'include' => '',

'number' => '',

'taxonomy' =>

'category',

'pad_counts' => false );

$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;

Plugin: Categories Images

z_taxonomy_image_url();

Plugin: Taxonomy TinyMCE (not working?)

Plugin: WP Category Permalink (for SEO)

Page 9: Wordpress multiple loops

Thumbnails

In functions.php add different sizes for thumbnails:

add_image_size(‘n_toprow', 164, 105, true);add_image_size(‘n_mainblog', 300, 260, true);..

Plugin: Categories ImagesPlugin: Regenerate Thumbnails

Plugin: Thumbnail Upscale

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 );

Page 10: Wordpress multiple loops

Thank you for your attention!

Questions?

Roman Rus @RomSocialwww.websitesupgrade.com