SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Downloaden Sie, um offline zu lesen
Anatomy of the Loop:
     The Code
      Drew Jaynes

                    @DrewAPicture
The default Loop
Default Loop Breakdown:



•  The is the start of the loop. Here's what we're
  basically saying:
•  If we have some posts,
•  Then WHILE we have those posts
•  Loop through the posts and display them
   one after the other
Default Loop Breakdown:




•  It's called the Loop, because it loops through
   the content until there's none left to display.
•  endwhile marks the end of the loop
•  The else basically shows an error if no content is found
     at the beginning of the loop
•    We close off our if statement with endif
Default Loop: All together now
•  The loop starts with a question:
•  Do we have posts?
•  If we have posts, then while we have those
  posts, loop through and display them one at
  a time.

• If we don't have posts, skip the loop and
display an error. At the end of our loop,
complete our question with an endif.
Loop with a count
Loop with a count
•  Adding a count allows you to segment your
  loop into multiple pieces.

•  For example:
•  Using a count would allow you to display the
  FIRST post in a loop differently than the rest

•  Let's look at a breakdown of the code then
  some visual examples of loops with counts
Loop with a count: Breakdown


•  Ask if you have posts
•  Introduce the count variable and give it a value of 0
•  Note: The count variable you use doesn't have to be
  'count'. You can use any variable name you like.
Loop with a count: Breakdown



•  Just as with a normal loop, WHILE you have posts,
  output the posts one by one.


•  At this point, we also need to increment our count
  variable every time the loop comes around. This is
  accomplished with $count++
Loop with a count: Breakdown




• At this point, we're inside the loop, but we want to
leverage our count to do something. In this example we first
test if we're at count 1, and if so, do something. If NOT, do
something else.


•  Let me show you an example >>
Loop with a count: Visual
•  Many magazine / news-based themes use
 counts to highlight the latest post in a loop.
Loop with a count: Breakdown
•  This is the loop and count code we were looking at
  before. I've added some HTML5 markup. You can see
  we're displaying the first post in the loop SEPARATELY
  from the rest of the posts.
Loop with a count: Breakdown
•  Closing off a counted loop is done exactly the
     same way as with a regular loop.
•  The endwhile marks the point where we've looped
     through all of our content
•    The else displays an error if we never had any posts
•    The endif closes the loop.
Loop with a count: All together
•  First we ask our question: Do we have posts? if we
     have posts. Then we initialize our count variable and
     give it a value of 0
•    While we have posts, we say we want to loop through
     them one after the other and at the same time,
     increment our count every time we loop through.
•    If we're at #1 in the count, we can style the FIRST post
     differently, otherwise (else) style the other posts the
     same.
•    End our loop, end our question.
Loop + a count + a Page Header
Loop + a count + a Page Header
•  At this point, we probably don't need to do a
  full breakdown of the parts of the loop. We're
  only going to be focusing on the first section
  for this example.
Loop + a count + a Page Header
•  In the previous section, we added a counting function to
  the first part of the loop.
•  We checked for posts then initialized a count variable
•  WHILE we had those posts, we looped through them
   one by one, all the while incrementing our count
   variable each time.
•  Then we used the count variable to manipulate how our
   content was displayed.


•  So now we want to add a Page Header. The easiest way
  to think about this is by examining exactly which
  questions we're asking in our code and where.
Loop + a count + a Page Header
•  It's important to differentiate between the IF and WHILE
     sections of the loop.
•    The first part of the loop ONLY asks if we have posts.
     But we're not in the loop yet, we're just asking. This is
     where we set our count variable and display a page
     header. Display is contingent on having posts
•    Once we get into the WHILE section, we're dealing with
     the loop and content there will be looped over and
     repeated.
Manipulating default loops
•  In the examples we've covered so far, we've
  assumed WordPress has automagically
  supplied us with query results that we're just
  displaying.

•  In the next few slides, we'll be exploring how
  to manipulate the query results to get the
  kinds of posts we want.
query_posts();
•  At its base, we have the ability to modify the
  query being fed to a default loop using a
  function called query_posts()

• query_posts() utilizes parameters outlined in
the WP_Query class found at this link

• These parameters allow us to dial down a
query to precisely the type of posts we're
looking to display
query_posts(): An example
If you take a look at the modified loop below, you'll
notice the placement of the query_posts() function is
right before our loop code begins.
query_posts(): An example




•  You can see we've specified two parameters
 via our query_posts() call. We want to display
 5 posts per page and only posts in the 'main'
 category
query_posts(): An example
•  So that's pretty cool right? But there's a
  problem.

•  Using query_posts() essentially hijacks the
  query we're supplied with so depending on
  which page this loop displays on, it might
  give you unintended results.

•  Let me give you some examples >>
query_posts(): Be mindful of magic
•  Many pages display default queries through
     default loops:
•  On category archives, only posts from those categories
     are displayed
•    On author archives, only posts from that author are
     displayed


•  The magic lies in $query_string
•  Any page that has a pre-formed loop gets its
     parameter(s) via this $query_string. So what is it?
$query_string: A primer
•  Many pages in the WP Template Hierarchy
  display posts based on the $query_string

•  Some example query strings:
•  Category archive: category_name=category-slug
•  Page: pagename=page-slug
•  Author archive: author_name=username
•  And so on
•  You've probably noticed by now that the syntax of query
  strings are strikingly similar to the WP_Query class
  parameters. That's because they are.
query_posts() & $query_string
• So now that you understand that query_posts() by itself
will hijack your query and that you'll need $query_string to
avoid that, what's your solution?
•   Concatenation. You need to combine the query string
    parameter with your custom parameters inside of
    query_posts(). Something like this:




That's it. So let's recap >>
query_posts(): A Recap
•  Adding query_posts() before your loop by itself will
   hijack the query being supplied to your loop
•  The $query_string is a parameter WordPress magically
   supplies based on the Template Hierarchy
•  Using $query_string as one of your query_posts()
  parameters will allow you to maintain the default query
  while spicing it up with your own parameters.
Helpful Links
•  In the Codex:
•  The Loop page
•  query_posts() page (including $query_string)
•  The WP Query class
•  WP Query class parameters
•  Template Hierarchy page

Weitere ähnliche Inhalte

Ähnlich wie Anatomy of the WordPress Loop

Ähnlich wie Anatomy of the WordPress Loop (20)

Init() Lesson 3
Init() Lesson 3Init() Lesson 3
Init() Lesson 3
 
powerpoint 2-7.pptx
powerpoint 2-7.pptxpowerpoint 2-7.pptx
powerpoint 2-7.pptx
 
Testing gone-right
Testing gone-rightTesting gone-right
Testing gone-right
 
Init() day2
Init() day2Init() day2
Init() day2
 
Init() Lesson 2
Init() Lesson 2Init() Lesson 2
Init() Lesson 2
 
CPP13 - Object Orientation
CPP13 - Object OrientationCPP13 - Object Orientation
CPP13 - Object Orientation
 
Java Building Blocks
Java Building BlocksJava Building Blocks
Java Building Blocks
 
Tips for writing a paper
Tips for writing a paperTips for writing a paper
Tips for writing a paper
 
module 2.pptx
module 2.pptxmodule 2.pptx
module 2.pptx
 
ACM init() Day 2
ACM init() Day 2ACM init() Day 2
ACM init() Day 2
 
Theming Search Results - How to Make Your Search Results Rock
Theming Search Results - How to Make Your Search Results RockTheming Search Results - How to Make Your Search Results Rock
Theming Search Results - How to Make Your Search Results Rock
 
Gettingintotheloop 100123225021-phpapp01
Gettingintotheloop 100123225021-phpapp01Gettingintotheloop 100123225021-phpapp01
Gettingintotheloop 100123225021-phpapp01
 
Php tips and tricks by omar bin sulaiman
Php tips and tricks by omar bin sulaimanPhp tips and tricks by omar bin sulaiman
Php tips and tricks by omar bin sulaiman
 
Tuning Your Engine
Tuning Your EngineTuning Your Engine
Tuning Your Engine
 
Streamlining Your Template Structures When Building Themes
Streamlining Your Template Structures When Building ThemesStreamlining Your Template Structures When Building Themes
Streamlining Your Template Structures When Building Themes
 
JavaScript Interview Questions Part - 1.pdf
JavaScript Interview Questions Part - 1.pdfJavaScript Interview Questions Part - 1.pdf
JavaScript Interview Questions Part - 1.pdf
 
Autoboxing And Unboxing In Java
Autoboxing And Unboxing In JavaAutoboxing And Unboxing In Java
Autoboxing And Unboxing In Java
 
JSLink for ITPros - SharePoint Saturday Jersey
JSLink for ITPros - SharePoint Saturday JerseyJSLink for ITPros - SharePoint Saturday Jersey
JSLink for ITPros - SharePoint Saturday Jersey
 
Testing web APIs
Testing web APIsTesting web APIs
Testing web APIs
 
Code Like Pythonista
Code Like PythonistaCode Like Pythonista
Code Like Pythonista
 

Mehr von DrewAPicture

WordPress Development in a Modern PHP World
WordPress Development in a Modern PHP WorldWordPress Development in a Modern PHP World
WordPress Development in a Modern PHP WorldDrewAPicture
 
WordPress Development in a Modern PHP World
WordPress Development in a Modern PHP WorldWordPress Development in a Modern PHP World
WordPress Development in a Modern PHP WorldDrewAPicture
 
Getting Creative with WordPress Queries, Again
Getting Creative with WordPress Queries, AgainGetting Creative with WordPress Queries, Again
Getting Creative with WordPress Queries, AgainDrewAPicture
 
How to Win Friends and Influence WordPress Core
How to Win Friends and Influence WordPress CoreHow to Win Friends and Influence WordPress Core
How to Win Friends and Influence WordPress CoreDrewAPicture
 
It Takes a Village to Make WordPress
It Takes a Village to Make WordPressIt Takes a Village to Make WordPress
It Takes a Village to Make WordPressDrewAPicture
 
Getting Creative with WordPress Queries
Getting Creative with WordPress QueriesGetting Creative with WordPress Queries
Getting Creative with WordPress QueriesDrewAPicture
 
Setting Up WordPress: A NUX Case Study
Setting Up WordPress: A NUX Case StudySetting Up WordPress: A NUX Case Study
Setting Up WordPress: A NUX Case StudyDrewAPicture
 
Core Docs: Sentencing WordPress to 11-years-to-life
Core Docs: Sentencing WordPress to 11-years-to-lifeCore Docs: Sentencing WordPress to 11-years-to-life
Core Docs: Sentencing WordPress to 11-years-to-lifeDrewAPicture
 
Putting the (docs) Cart Before the (standards) Horse
Putting the (docs) Cart Before the (standards) HorsePutting the (docs) Cart Before the (standards) Horse
Putting the (docs) Cart Before the (standards) HorseDrewAPicture
 
There's a Filter For That
There's a Filter For ThatThere's a Filter For That
There's a Filter For ThatDrewAPicture
 
Customizer-ing Theme Options: A Visual Playground
Customizer-ing Theme Options: A Visual PlaygroundCustomizer-ing Theme Options: A Visual Playground
Customizer-ing Theme Options: A Visual PlaygroundDrewAPicture
 

Mehr von DrewAPicture (11)

WordPress Development in a Modern PHP World
WordPress Development in a Modern PHP WorldWordPress Development in a Modern PHP World
WordPress Development in a Modern PHP World
 
WordPress Development in a Modern PHP World
WordPress Development in a Modern PHP WorldWordPress Development in a Modern PHP World
WordPress Development in a Modern PHP World
 
Getting Creative with WordPress Queries, Again
Getting Creative with WordPress Queries, AgainGetting Creative with WordPress Queries, Again
Getting Creative with WordPress Queries, Again
 
How to Win Friends and Influence WordPress Core
How to Win Friends and Influence WordPress CoreHow to Win Friends and Influence WordPress Core
How to Win Friends and Influence WordPress Core
 
It Takes a Village to Make WordPress
It Takes a Village to Make WordPressIt Takes a Village to Make WordPress
It Takes a Village to Make WordPress
 
Getting Creative with WordPress Queries
Getting Creative with WordPress QueriesGetting Creative with WordPress Queries
Getting Creative with WordPress Queries
 
Setting Up WordPress: A NUX Case Study
Setting Up WordPress: A NUX Case StudySetting Up WordPress: A NUX Case Study
Setting Up WordPress: A NUX Case Study
 
Core Docs: Sentencing WordPress to 11-years-to-life
Core Docs: Sentencing WordPress to 11-years-to-lifeCore Docs: Sentencing WordPress to 11-years-to-life
Core Docs: Sentencing WordPress to 11-years-to-life
 
Putting the (docs) Cart Before the (standards) Horse
Putting the (docs) Cart Before the (standards) HorsePutting the (docs) Cart Before the (standards) Horse
Putting the (docs) Cart Before the (standards) Horse
 
There's a Filter For That
There's a Filter For ThatThere's a Filter For That
There's a Filter For That
 
Customizer-ing Theme Options: A Visual Playground
Customizer-ing Theme Options: A Visual PlaygroundCustomizer-ing Theme Options: A Visual Playground
Customizer-ing Theme Options: A Visual Playground
 

Kürzlich hochgeladen

ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 

Kürzlich hochgeladen (20)

ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 

Anatomy of the WordPress Loop

  • 1. Anatomy of the Loop: The Code Drew Jaynes @DrewAPicture
  • 3. Default Loop Breakdown: •  The is the start of the loop. Here's what we're basically saying: •  If we have some posts, •  Then WHILE we have those posts •  Loop through the posts and display them one after the other
  • 4. Default Loop Breakdown: •  It's called the Loop, because it loops through the content until there's none left to display. •  endwhile marks the end of the loop •  The else basically shows an error if no content is found at the beginning of the loop •  We close off our if statement with endif
  • 5. Default Loop: All together now •  The loop starts with a question: •  Do we have posts? •  If we have posts, then while we have those posts, loop through and display them one at a time. • If we don't have posts, skip the loop and display an error. At the end of our loop, complete our question with an endif.
  • 6. Loop with a count
  • 7. Loop with a count •  Adding a count allows you to segment your loop into multiple pieces. •  For example: •  Using a count would allow you to display the FIRST post in a loop differently than the rest •  Let's look at a breakdown of the code then some visual examples of loops with counts
  • 8. Loop with a count: Breakdown •  Ask if you have posts •  Introduce the count variable and give it a value of 0 •  Note: The count variable you use doesn't have to be 'count'. You can use any variable name you like.
  • 9. Loop with a count: Breakdown •  Just as with a normal loop, WHILE you have posts, output the posts one by one. •  At this point, we also need to increment our count variable every time the loop comes around. This is accomplished with $count++
  • 10. Loop with a count: Breakdown • At this point, we're inside the loop, but we want to leverage our count to do something. In this example we first test if we're at count 1, and if so, do something. If NOT, do something else. •  Let me show you an example >>
  • 11. Loop with a count: Visual •  Many magazine / news-based themes use counts to highlight the latest post in a loop.
  • 12. Loop with a count: Breakdown •  This is the loop and count code we were looking at before. I've added some HTML5 markup. You can see we're displaying the first post in the loop SEPARATELY from the rest of the posts.
  • 13. Loop with a count: Breakdown •  Closing off a counted loop is done exactly the same way as with a regular loop. •  The endwhile marks the point where we've looped through all of our content •  The else displays an error if we never had any posts •  The endif closes the loop.
  • 14. Loop with a count: All together •  First we ask our question: Do we have posts? if we have posts. Then we initialize our count variable and give it a value of 0 •  While we have posts, we say we want to loop through them one after the other and at the same time, increment our count every time we loop through. •  If we're at #1 in the count, we can style the FIRST post differently, otherwise (else) style the other posts the same. •  End our loop, end our question.
  • 15. Loop + a count + a Page Header
  • 16. Loop + a count + a Page Header •  At this point, we probably don't need to do a full breakdown of the parts of the loop. We're only going to be focusing on the first section for this example.
  • 17. Loop + a count + a Page Header •  In the previous section, we added a counting function to the first part of the loop. •  We checked for posts then initialized a count variable •  WHILE we had those posts, we looped through them one by one, all the while incrementing our count variable each time. •  Then we used the count variable to manipulate how our content was displayed. •  So now we want to add a Page Header. The easiest way to think about this is by examining exactly which questions we're asking in our code and where.
  • 18. Loop + a count + a Page Header •  It's important to differentiate between the IF and WHILE sections of the loop. •  The first part of the loop ONLY asks if we have posts. But we're not in the loop yet, we're just asking. This is where we set our count variable and display a page header. Display is contingent on having posts •  Once we get into the WHILE section, we're dealing with the loop and content there will be looped over and repeated.
  • 19. Manipulating default loops •  In the examples we've covered so far, we've assumed WordPress has automagically supplied us with query results that we're just displaying. •  In the next few slides, we'll be exploring how to manipulate the query results to get the kinds of posts we want.
  • 20. query_posts(); •  At its base, we have the ability to modify the query being fed to a default loop using a function called query_posts() • query_posts() utilizes parameters outlined in the WP_Query class found at this link • These parameters allow us to dial down a query to precisely the type of posts we're looking to display
  • 21. query_posts(): An example If you take a look at the modified loop below, you'll notice the placement of the query_posts() function is right before our loop code begins.
  • 22. query_posts(): An example •  You can see we've specified two parameters via our query_posts() call. We want to display 5 posts per page and only posts in the 'main' category
  • 23. query_posts(): An example •  So that's pretty cool right? But there's a problem. •  Using query_posts() essentially hijacks the query we're supplied with so depending on which page this loop displays on, it might give you unintended results. •  Let me give you some examples >>
  • 24. query_posts(): Be mindful of magic •  Many pages display default queries through default loops: •  On category archives, only posts from those categories are displayed •  On author archives, only posts from that author are displayed •  The magic lies in $query_string •  Any page that has a pre-formed loop gets its parameter(s) via this $query_string. So what is it?
  • 25. $query_string: A primer •  Many pages in the WP Template Hierarchy display posts based on the $query_string •  Some example query strings: •  Category archive: category_name=category-slug •  Page: pagename=page-slug •  Author archive: author_name=username •  And so on •  You've probably noticed by now that the syntax of query strings are strikingly similar to the WP_Query class parameters. That's because they are.
  • 26. query_posts() & $query_string • So now that you understand that query_posts() by itself will hijack your query and that you'll need $query_string to avoid that, what's your solution? •  Concatenation. You need to combine the query string parameter with your custom parameters inside of query_posts(). Something like this: That's it. So let's recap >>
  • 27. query_posts(): A Recap •  Adding query_posts() before your loop by itself will hijack the query being supplied to your loop •  The $query_string is a parameter WordPress magically supplies based on the Template Hierarchy •  Using $query_string as one of your query_posts() parameters will allow you to maintain the default query while spicing it up with your own parameters.
  • 28. Helpful Links •  In the Codex: •  The Loop page •  query_posts() page (including $query_string) •  The WP Query class •  WP Query class parameters •  Template Hierarchy page