SlideShare a Scribd company logo
1 of 10
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_excerpt();
the_time();
the_author();
comments_template( );
endwhile;
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();
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();
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
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' ) )
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'
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 );
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 );
Thank you for your attention!
Questions?
Roman Rus
@RomSocial
www.websitesupgrade.com

More Related Content

What's hot

Web2py Code Lab
Web2py Code LabWeb2py Code Lab
Web2py Code Lab
Colin Su
 
Creatively creating custom post types! word sesh2
Creatively creating custom post types!  word sesh2Creatively creating custom post types!  word sesh2
Creatively creating custom post types! word sesh2
techvoltz
 
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
ichikaway
 
Rails' Next Top Model
Rails' Next Top ModelRails' Next Top Model
Rails' Next Top Model
Adam Keys
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
smueller_sandsmedia
 
前端MVC 豆瓣说
前端MVC 豆瓣说前端MVC 豆瓣说
前端MVC 豆瓣说
Ting Lv
 

What's hot (20)

[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018
 
You Don't Know Query (WordCamp Netherlands 2012)
You Don't Know Query (WordCamp Netherlands 2012)You Don't Know Query (WordCamp Netherlands 2012)
You Don't Know Query (WordCamp Netherlands 2012)
 
Web2py Code Lab
Web2py Code LabWeb2py Code Lab
Web2py Code Lab
 
Difference between mysql_fetch_array and mysql_fetch_assoc in PHP
Difference between mysql_fetch_array and mysql_fetch_assoc in PHPDifference between mysql_fetch_array and mysql_fetch_assoc in PHP
Difference between mysql_fetch_array and mysql_fetch_assoc in PHP
 
Creatively creating custom post types! word sesh2
Creatively creating custom post types!  word sesh2Creatively creating custom post types!  word sesh2
Creatively creating custom post types! word sesh2
 
Drupal vs WordPress
Drupal vs WordPressDrupal vs WordPress
Drupal vs WordPress
 
Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3
 
Views Style Plugins
Views Style PluginsViews Style Plugins
Views Style Plugins
 
Python Templating Engine - Intro to Jinja
Python Templating Engine - Intro to JinjaPython Templating Engine - Intro to Jinja
Python Templating Engine - Intro to Jinja
 
Drupal 8 Sample Module
Drupal 8 Sample ModuleDrupal 8 Sample Module
Drupal 8 Sample Module
 
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
 
Drupal 8: Routing & More
Drupal 8: Routing & MoreDrupal 8: Routing & More
Drupal 8: Routing & More
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
 
[Bucharest] Your intents are dirty, droid!
[Bucharest] Your intents are dirty, droid![Bucharest] Your intents are dirty, droid!
[Bucharest] Your intents are dirty, droid!
 
Separation of concerns - DPC12
Separation of concerns - DPC12Separation of concerns - DPC12
Separation of concerns - DPC12
 
Rails' Next Top Model
Rails' Next Top ModelRails' Next Top Model
Rails' Next Top Model
 
Getting Creative with WordPress Queries
Getting Creative with WordPress QueriesGetting Creative with WordPress Queries
Getting Creative with WordPress Queries
 
Caching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment CachingCaching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment Caching
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
 
前端MVC 豆瓣说
前端MVC 豆瓣说前端MVC 豆瓣说
前端MVC 豆瓣说
 

Similar to Wordpress multiple loops

Building a Portfolio With Custom Post Types
Building a Portfolio With Custom Post TypesBuilding a Portfolio With Custom Post Types
Building a Portfolio With Custom Post Types
Alex Blackie
 
MTDDC 2010.2.5 Tokyo - Brand new API
MTDDC 2010.2.5 Tokyo - Brand new APIMTDDC 2010.2.5 Tokyo - Brand new API
MTDDC 2010.2.5 Tokyo - Brand new API
Six Apart KK
 
Gail villanueva add muscle to your wordpress site
Gail villanueva   add muscle to your wordpress siteGail villanueva   add muscle to your wordpress site
Gail villanueva add muscle to your wordpress site
references
 
You don’t know query - WordCamp UK Edinburgh 2012
You don’t know query - WordCamp UK Edinburgh 2012You don’t know query - WordCamp UK Edinburgh 2012
You don’t know query - WordCamp UK Edinburgh 2012
l3rady
 
Extending WordPress. Making use of Custom Post Types
Extending WordPress. Making use of Custom Post TypesExtending WordPress. Making use of Custom Post Types
Extending WordPress. Making use of Custom Post Types
Utsav Singh Rathour
 
Stepping Into Custom Post Types
Stepping Into Custom Post TypesStepping Into Custom Post Types
Stepping Into Custom Post Types
K.Adam White
 
Pyconie 2012
Pyconie 2012Pyconie 2012
Pyconie 2012
Yaqi Zhao
 

Similar to Wordpress multiple loops (20)

Mastering Custom Post Types - WordCamp Atlanta 2012
Mastering Custom Post Types - WordCamp Atlanta 2012Mastering Custom Post Types - WordCamp Atlanta 2012
Mastering Custom Post Types - WordCamp Atlanta 2012
 
Building a Portfolio With Custom Post Types
Building a Portfolio With Custom Post TypesBuilding a Portfolio With Custom Post Types
Building a Portfolio With Custom Post Types
 
MTDDC 2010.2.5 Tokyo - Brand new API
MTDDC 2010.2.5 Tokyo - Brand new APIMTDDC 2010.2.5 Tokyo - Brand new API
MTDDC 2010.2.5 Tokyo - Brand new API
 
Building Potent WordPress Websites
Building Potent WordPress WebsitesBuilding Potent WordPress Websites
Building Potent WordPress Websites
 
Dig Deeper into WordPress - WD Meetup Cairo
Dig Deeper into WordPress - WD Meetup CairoDig Deeper into WordPress - WD Meetup Cairo
Dig Deeper into WordPress - WD Meetup Cairo
 
Gail villanueva add muscle to your wordpress site
Gail villanueva   add muscle to your wordpress siteGail villanueva   add muscle to your wordpress site
Gail villanueva add muscle to your wordpress site
 
WordPress Cuztom Helper
WordPress Cuztom HelperWordPress Cuztom Helper
WordPress Cuztom Helper
 
Can WordPress really do that? A case study of vierderduer.no
Can WordPress really do that? A case study of vierderduer.noCan WordPress really do that? A case study of vierderduer.no
Can WordPress really do that? A case study of vierderduer.no
 
Custom content types & custom taxonomies in wordpress
Custom content types & custom taxonomies in wordpressCustom content types & custom taxonomies in wordpress
Custom content types & custom taxonomies in wordpress
 
Custom Post Types and Meta Fields
Custom Post Types and Meta FieldsCustom Post Types and Meta Fields
Custom Post Types and Meta Fields
 
You don’t know query - WordCamp UK Edinburgh 2012
You don’t know query - WordCamp UK Edinburgh 2012You don’t know query - WordCamp UK Edinburgh 2012
You don’t know query - WordCamp UK Edinburgh 2012
 
Digging into WordPress custom fields - WordCamp Brno 2017
Digging into WordPress custom fields - WordCamp Brno 2017Digging into WordPress custom fields - WordCamp Brno 2017
Digging into WordPress custom fields - WordCamp Brno 2017
 
WP_Query, pre_get_posts, and eliminating query_posts()
WP_Query, pre_get_posts, and eliminating query_posts()WP_Query, pre_get_posts, and eliminating query_posts()
WP_Query, pre_get_posts, and eliminating query_posts()
 
Extending WordPress. Making use of Custom Post Types
Extending WordPress. Making use of Custom Post TypesExtending WordPress. Making use of Custom Post Types
Extending WordPress. Making use of Custom Post Types
 
Intermediate SQL with Ecto - LoneStar ElixirConf 2018
Intermediate SQL with Ecto - LoneStar ElixirConf 2018Intermediate SQL with Ecto - LoneStar ElixirConf 2018
Intermediate SQL with Ecto - LoneStar ElixirConf 2018
 
Stepping Into Custom Post Types
Stepping Into Custom Post TypesStepping Into Custom Post Types
Stepping Into Custom Post Types
 
WordPress London 16 May 2012 - You don’t know query
WordPress London 16 May 2012 - You don’t know queryWordPress London 16 May 2012 - You don’t know query
WordPress London 16 May 2012 - You don’t know query
 
WordPress as an application framework
WordPress as an application frameworkWordPress as an application framework
WordPress as an application framework
 
Pyconie 2012
Pyconie 2012Pyconie 2012
Pyconie 2012
 
php2.pptx
php2.pptxphp2.pptx
php2.pptx
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Recently uploaded (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

Wordpress multiple loops

  • 1. Getting different sets of posts in your theme Roman Rus @RomSocial www.websitesupgrade.com
  • 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. 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. 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. 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. 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. 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. 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. 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. Thank you for your attention! Questions? Roman Rus @RomSocial www.websitesupgrade.com