SlideShare a Scribd company logo
Coolfields Consulting www.coolfields.co.uk
@coolfields
Simple Usability Improvements
for your WordPress Theme
Graham Armfield
graham.armfield@coolfields.co.uk
@coolfields
What I‟m Going to Cover
Suggested tweaks to improve usability
• Helping visitors find what they‟re looking for more easily.
Including
• Updates to theme files for search, blog pages, archive
pages, „landing‟ pages, „read more‟ links
• Using plugins to improve search results and pagination.
2
IMPROVING SEARCH
RESULTS DISPLAY
Giving visitors feedback about the
search they just did
3
Improving Search Results Display
Which is more informative?
4
TwentyEleven
Theme
Bespoke Theme
Improving Search Results Display
The code:
<h1>Search Results for: '<?php the_search_query(); ?>'</h1>
<p>You searched for
<strong>'<?php the_search_query(); ?>'</strong>
and the search returned
<?php echo $wp_query->found_posts; ?> items. Here are the
results:</p>
5
BETTER SEARCH RESULTS
Beyond the default WordPress Search
6
Better Search Results
WordPress Search Functionality
• Powerful search capability
• Built-in
• Dynamic
But, it does have limitations:
• Search results are ordered newest first – no assessment of
priority.
• There is no indication of the use of the search terms.
7
Better Search Results
Relevanssi plugin
http://wordpress.org/plugins/relevanssi/
• Results order based on relevance
• And/or search with multiple search words
• Fuzzy matching
• Customised excerpts to show use of search terms
• Minimum search word length
• Logging searches
• Compatible with custom post types
• Etc
8
Better Search Results
Relevanssi settings
9
Better Search Results
Relevanssi settings
10
Better Search Results
Customised excerpts highlight use of search terms
11
CUSTOMISING POST TYPE
DISPLAY
Because not all post types are the same
12
Customising post type display
Useful for:
• Archive page – by date, by category
• Search results page
Especially where you have many different post types – eg
Events, Articles, Reviews, etc
13
Customising post type display
14
This is an
article
This is an
event
Customising post type display
Let‟s create a function…
getPostTypeAtts($postType) {
switch ($postType) {
case 'page' :
$str = 'Page';
break;
case 'post' :
$str = 'News Item';
break;
case 'event' :
$str = 'Event';
break;
}
return array($str);
}
15
Customising post type display
In search.php, archive.php
<?php
while (have_posts()) : the_post();
$postTypeAtts = getPostTypeAtts(get_post_type());
$typeName = ' ('.$postTypeAtts[0].')';
?>
<h2><a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); echo $typeName; ?></a></h2>
16
Customising post type display
17
Client chose not to have post type for
pages.
Customising post type display
Further customisations:
• Showing a published date, author, etc
• Customising the Read More link
• Etc
18
Customising post type display
getPostTypeAtts($postType) {
switch ($postType) {
case 'post' :
$str = 'News Item';
$showDate = true;
$readMore = 'Read the full news item';
break;
case 'job' :
$str = 'Job';
$showDate = false;
$readMore = 'Read more about the job';
break;
...
}
return array($str, $showDate, $readMore);
}
19
Customising post type display
In search.php, archive.php
<?php
while (have_posts()) : the_post();
$postTypeAtts = getPostTypeAtts(get_post_type());
$typeName = ' ('.$postTypeAtts[0].')';
?>
<h2><a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); echo $typeName; ?></a></h2>
<?php if ($postTypeAtts[1]) { ?>
<p>Published: <?php the_time('M jS, Y'); </p> ?>
<?php }
the_excerpt(); ?>
<p><a href="<?php the_permalink() ?>"><?php the_title();
echo ': '. $postTypeAtts[2].' &raquo;' ?></a></p>
20
Customising post types
21
PRETTY PAGINATION
Helping visitors move around lists of
posts and search results.
22
Pretty pagination
We‟re all familiar with this:
Achieved using previous_posts_link() and next_posts_link()
This might be OK for site with not many posts, but what if you‟ve got
loads?
23
Pretty pagination
Might this work better?
Now your visitors have a much better idea of how many posts there
are. And they can quickly re-find one later.
24
Pretty pagination
Plugins available to do this:
• wp-pagenavi
• wp-paginate
But you do need to edit theme files to use them.
Because of customisation requirements I decided to build
my own functionality.
25
Pretty pagination
In WordPress, the common queries are paged by default – for
example:
• Blog index page
• Archive pages – categories, tags, etc
• Search results
The number of posts shown on each page is set in admin
settings. (But can be overridden by use of the
pre_get_posts hook.)
Custom queries using WP_Query are not paged by default but
paging can be induced by use of posts_per_page and
paged in query arguments.
26
Pretty pagination
Within each query there are two useful values that we can
use to help create the new style paging.
get_query_var('paged')
• Tells us which page of results we‟re on.
$wp_query -> max_num_pages
• Gives the total number of pages required to show all
results from the query.
• Works with default queries and with custom queries.
27
Pretty pagination
We‟ll need some code in the loop, and a function to
actually create the HTML for the new-style paging links.
Get the values at the start of the loop for use later
if (have_posts()) :
$paged = (get_query_var('paged')) ?
get_query_var('paged') : 1;
$pageMax = $wp_query -> max_num_pages;
while (have_posts()) : the_post();
28
Note that get_query_var('paged') is not set on first results
page so use this ternary operator to force value of $paged
to 1 if it‟s not set.
Pretty pagination
At the point you want the paging links to appear call a new
function:
<?php
endwhile; // endwhile for the loop or query
// If more than one page show links to others
if ($pageMax > 1) {
echo '<h3>Other Blog Entries</h3>';
echo doPaging($wp_query, $paged);
}
?>
<?php else : ?>
29
Pretty pagination
And the function – part 1:
function doPaging($myQuery, $paged) {
$str = '<ul class="paging">';
// Previous link? Yes if we're not on page 1
if ($paged > 1) {
$str .= '<li><a href="?paged='.($paged-1).'">&laquo;
Newer Posts</a></li>';
}
...
30
Pretty pagination
The doPaging function – part 2:
...
for($i=1; $i<=$myQuery->max_num_pages; $i++){
// Check if we're on that page
if ($paged==$i) {
// we are, so no link
$str .= '<li class="this-page"><span class=“srdr">Page
</span>'.$i.'</li>';
} else {
// put out the link
$str .= '<li><a href="?paged=' . $i.'"><span
class=“srdr">Page </span>'.$i.'</a></li>';
}
}
...
31
Pretty pagination
The doPaging function – part 3:
...
// Older posts link? Yes if we're not on last page
if ($paged < $myQuery->max_num_pages) {
$str .= '<li><a href="?paged='.($paged+1).'">Older
Posts &raquo;</a></li>';
}
$str .= '</ul>';
return $str;
}
32
Pretty pagination
Possible Enhancements:
• Customisation of text strings – eg “Next” and “Previous”
where custom post types or custom queries are used – the
results may not all be in chronological order.
• Amendments to function call to cater for paging on search
results – the required URLs for search results pages are in a
different format.
• Catering for sites with many, many pages of posts:
33
Summary
We‟ve seen just a few small enhancements that can be made to our
sites that can improve the visitors‟ experience.
There are others:
• Keeping the search query in the search box when showing results.
• Secondary navigation for child and sibling pages – not just relying on
dropdown menus.
34
Thanks for listening –
any (more) questions?
graham.armfield@coolfields.co.uk
@coolfields
35

More Related Content

What's hot (20)

PPT
Haml, Sass and Compass for Sane Web Development
jeremyw
 
KEY
How To Write a WordPress Plugin
Andy Stratton
 
DOC
20110820 header new style
AgentiadeturismInvenio
 
PDF
RSpec User Stories
rahoulb
 
PDF
Qpsmtpd
Fred Moyer
 
PPTX
WordPress 15th Meetup - Build a Theme
Fadi Nicolas Zahhar
 
PPT
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Mike Schinkel
 
PDF
Telling Stories With RSpec
rahoulb
 
PPTX
Webinar: AngularJS and the WordPress REST API
WP Engine UK
 
PDF
D7 theming what's new - London
Marek Sotak
 
PDF
HTML5 Essentials
Marc Grabanski
 
PPT
Performance Tips In Rails Development
qtlove
 
TXT
Seo hints
AbidKhan237
 
PDF
Enter the app era with ruby on rails
Matteo Collina
 
PPTX
Supercharged HTML & CSS
Max Kraszewski
 
PDF
i18n for Plugin and Theme Developers, WordCamp Milano 2016
Sergey Biryukov
 
PPT
Evolution of API With Blogging
Takatsugu Shigeta
 
ODP
PHP 102: Out with the Bad, In with the Good
Jeremy Kendall
 
PDF
HTML5 workshop, forms
Robert Nyman
 
PPT
Changing Template Engine
Takatsugu Shigeta
 
Haml, Sass and Compass for Sane Web Development
jeremyw
 
How To Write a WordPress Plugin
Andy Stratton
 
20110820 header new style
AgentiadeturismInvenio
 
RSpec User Stories
rahoulb
 
Qpsmtpd
Fred Moyer
 
WordPress 15th Meetup - Build a Theme
Fadi Nicolas Zahhar
 
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Mike Schinkel
 
Telling Stories With RSpec
rahoulb
 
Webinar: AngularJS and the WordPress REST API
WP Engine UK
 
D7 theming what's new - London
Marek Sotak
 
HTML5 Essentials
Marc Grabanski
 
Performance Tips In Rails Development
qtlove
 
Seo hints
AbidKhan237
 
Enter the app era with ruby on rails
Matteo Collina
 
Supercharged HTML & CSS
Max Kraszewski
 
i18n for Plugin and Theme Developers, WordCamp Milano 2016
Sergey Biryukov
 
Evolution of API With Blogging
Takatsugu Shigeta
 
PHP 102: Out with the Bad, In with the Good
Jeremy Kendall
 
HTML5 workshop, forms
Robert Nyman
 
Changing Template Engine
Takatsugu Shigeta
 

Similar to Simple Usability Tweaks for Your WordPress Theme (20)

PDF
Your Custom WordPress Admin Pages Suck
Anthony Montalbano
 
PPTX
WordPress Structure and Best Practices
markparolisi
 
PPT
WordPress for Libraries PreConference Workshop
Polly Farrington
 
PPTX
Optimizing Your WordPress Site
Phil Buckley
 
PDF
Making WordPress Your Own: Theme Customization & Creation
Mykl Roventine
 
PPTX
WordPress theme development from scratch : ICT MeetUp 2013 Nepal
Chandra Prakash Thapa
 
PPTX
WordPress Themes 101 - PSUWeb13 Workshop
Curtiss Grymala
 
PPTX
Most widely used WordPress tips and tricks of 2016
Reegan
 
PPTX
Introduction to Plugin Programming, WordCamp Miami 2011
David Carr
 
PPTX
NamesCon 2015 Wordpress Beginner Session
Bruce Marler
 
PPSX
WordPress Theme Design and Development Workshop - Day 2
Mizanur Rahaman Mizan
 
PPTX
WordPress Themes 101 - dotEduGuru Summit 2013
Curtiss Grymala
 
PPTX
Wordpress workflow for an agency world
Chris Lowe
 
PDF
SEO FOR WORDPRESS
DAVID RAUDALES
 
PPTX
The Way to Theme Enlightenment 2017
Amanda Giles
 
PPTX
WordPress SEO
Michelle Castillo
 
PPTX
Obscure Wordpress Functions That Are Actually Quite Useful
Graham Armfield
 
PDF
WordPress customizer: for themes and more
R-Cubed Design Forge
 
PPT
WordPress Theme Design - Rich Media Institute Workshop
Brendan Sera-Shriar
 
PDF
Why Hacking WordPress Search Isn't Some Big Scary Thing
Chris Reynolds
 
Your Custom WordPress Admin Pages Suck
Anthony Montalbano
 
WordPress Structure and Best Practices
markparolisi
 
WordPress for Libraries PreConference Workshop
Polly Farrington
 
Optimizing Your WordPress Site
Phil Buckley
 
Making WordPress Your Own: Theme Customization & Creation
Mykl Roventine
 
WordPress theme development from scratch : ICT MeetUp 2013 Nepal
Chandra Prakash Thapa
 
WordPress Themes 101 - PSUWeb13 Workshop
Curtiss Grymala
 
Most widely used WordPress tips and tricks of 2016
Reegan
 
Introduction to Plugin Programming, WordCamp Miami 2011
David Carr
 
NamesCon 2015 Wordpress Beginner Session
Bruce Marler
 
WordPress Theme Design and Development Workshop - Day 2
Mizanur Rahaman Mizan
 
WordPress Themes 101 - dotEduGuru Summit 2013
Curtiss Grymala
 
Wordpress workflow for an agency world
Chris Lowe
 
SEO FOR WORDPRESS
DAVID RAUDALES
 
The Way to Theme Enlightenment 2017
Amanda Giles
 
WordPress SEO
Michelle Castillo
 
Obscure Wordpress Functions That Are Actually Quite Useful
Graham Armfield
 
WordPress customizer: for themes and more
R-Cubed Design Forge
 
WordPress Theme Design - Rich Media Institute Workshop
Brendan Sera-Shriar
 
Why Hacking WordPress Search Isn't Some Big Scary Thing
Chris Reynolds
 
Ad

More from Graham Armfield (20)

PPTX
Useful Accessibility Tools Version 3 - Jul 2021
Graham Armfield
 
PPTX
So how do i know if my wordpress website is accessible - WordPress Accessibil...
Graham Armfield
 
PPTX
Useful Accessibility Tools - WordCamp Brighton
Graham Armfield
 
PPTX
Accessibility Hacks Version 2
Graham Armfield
 
PPTX
Accessibility Hacks version 2
Graham Armfield
 
PPTX
Useful Accessibility Tools - WP Pompey April 2019
Graham Armfield
 
PPTX
How to Build an Accessible WordPress Theme
Graham Armfield
 
PPTX
Accessibility Hacks Wordcamp Manchester October 2018
Graham Armfield
 
PPTX
Useful Accessibility Tools
Graham Armfield
 
PPTX
Assistive technology Demo WordCamp Bristol
Graham Armfield
 
PPTX
Designing for Accessibility - WordCamp London 2017
Graham Armfield
 
PPTX
Designing for Accessibility - Front End North - September 2016
Graham Armfield
 
PPTX
Themes Plugins and Accessibility - WordCamp London March 2015
Graham Armfield
 
PPTX
Can WordPress help make the web more accessible - eaccess15 - Feb 2015
Graham Armfield
 
PPTX
Themes, Plugins and Accessibility
Graham Armfield
 
PPTX
Wordpress and Web Accessibility Wordcamp UK 2014
Graham Armfield
 
PPTX
So What is This Thing Called WordPress?
Graham Armfield
 
PPTX
So How Do I Know if My Website is Accessible?
Graham Armfield
 
PPTX
Handling User Generated Content in WordPress
Graham Armfield
 
PPTX
So, How Do I Know if my WordPress Website is Accessible?
Graham Armfield
 
Useful Accessibility Tools Version 3 - Jul 2021
Graham Armfield
 
So how do i know if my wordpress website is accessible - WordPress Accessibil...
Graham Armfield
 
Useful Accessibility Tools - WordCamp Brighton
Graham Armfield
 
Accessibility Hacks Version 2
Graham Armfield
 
Accessibility Hacks version 2
Graham Armfield
 
Useful Accessibility Tools - WP Pompey April 2019
Graham Armfield
 
How to Build an Accessible WordPress Theme
Graham Armfield
 
Accessibility Hacks Wordcamp Manchester October 2018
Graham Armfield
 
Useful Accessibility Tools
Graham Armfield
 
Assistive technology Demo WordCamp Bristol
Graham Armfield
 
Designing for Accessibility - WordCamp London 2017
Graham Armfield
 
Designing for Accessibility - Front End North - September 2016
Graham Armfield
 
Themes Plugins and Accessibility - WordCamp London March 2015
Graham Armfield
 
Can WordPress help make the web more accessible - eaccess15 - Feb 2015
Graham Armfield
 
Themes, Plugins and Accessibility
Graham Armfield
 
Wordpress and Web Accessibility Wordcamp UK 2014
Graham Armfield
 
So What is This Thing Called WordPress?
Graham Armfield
 
So How Do I Know if My Website is Accessible?
Graham Armfield
 
Handling User Generated Content in WordPress
Graham Armfield
 
So, How Do I Know if my WordPress Website is Accessible?
Graham Armfield
 
Ad

Recently uploaded (20)

PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
PPTX
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 

Simple Usability Tweaks for Your WordPress Theme

  • 1. Coolfields Consulting www.coolfields.co.uk @coolfields Simple Usability Improvements for your WordPress Theme Graham Armfield [email protected] @coolfields
  • 2. What I‟m Going to Cover Suggested tweaks to improve usability • Helping visitors find what they‟re looking for more easily. Including • Updates to theme files for search, blog pages, archive pages, „landing‟ pages, „read more‟ links • Using plugins to improve search results and pagination. 2
  • 3. IMPROVING SEARCH RESULTS DISPLAY Giving visitors feedback about the search they just did 3
  • 4. Improving Search Results Display Which is more informative? 4 TwentyEleven Theme Bespoke Theme
  • 5. Improving Search Results Display The code: <h1>Search Results for: '<?php the_search_query(); ?>'</h1> <p>You searched for <strong>'<?php the_search_query(); ?>'</strong> and the search returned <?php echo $wp_query->found_posts; ?> items. Here are the results:</p> 5
  • 6. BETTER SEARCH RESULTS Beyond the default WordPress Search 6
  • 7. Better Search Results WordPress Search Functionality • Powerful search capability • Built-in • Dynamic But, it does have limitations: • Search results are ordered newest first – no assessment of priority. • There is no indication of the use of the search terms. 7
  • 8. Better Search Results Relevanssi plugin http://wordpress.org/plugins/relevanssi/ • Results order based on relevance • And/or search with multiple search words • Fuzzy matching • Customised excerpts to show use of search terms • Minimum search word length • Logging searches • Compatible with custom post types • Etc 8
  • 11. Better Search Results Customised excerpts highlight use of search terms 11
  • 12. CUSTOMISING POST TYPE DISPLAY Because not all post types are the same 12
  • 13. Customising post type display Useful for: • Archive page – by date, by category • Search results page Especially where you have many different post types – eg Events, Articles, Reviews, etc 13
  • 14. Customising post type display 14 This is an article This is an event
  • 15. Customising post type display Let‟s create a function… getPostTypeAtts($postType) { switch ($postType) { case 'page' : $str = 'Page'; break; case 'post' : $str = 'News Item'; break; case 'event' : $str = 'Event'; break; } return array($str); } 15
  • 16. Customising post type display In search.php, archive.php <?php while (have_posts()) : the_post(); $postTypeAtts = getPostTypeAtts(get_post_type()); $typeName = ' ('.$postTypeAtts[0].')'; ?> <h2><a href="<?php the_permalink() ?>" rel="bookmark"> <?php the_title(); echo $typeName; ?></a></h2> 16
  • 17. Customising post type display 17 Client chose not to have post type for pages.
  • 18. Customising post type display Further customisations: • Showing a published date, author, etc • Customising the Read More link • Etc 18
  • 19. Customising post type display getPostTypeAtts($postType) { switch ($postType) { case 'post' : $str = 'News Item'; $showDate = true; $readMore = 'Read the full news item'; break; case 'job' : $str = 'Job'; $showDate = false; $readMore = 'Read more about the job'; break; ... } return array($str, $showDate, $readMore); } 19
  • 20. Customising post type display In search.php, archive.php <?php while (have_posts()) : the_post(); $postTypeAtts = getPostTypeAtts(get_post_type()); $typeName = ' ('.$postTypeAtts[0].')'; ?> <h2><a href="<?php the_permalink() ?>" rel="bookmark"> <?php the_title(); echo $typeName; ?></a></h2> <?php if ($postTypeAtts[1]) { ?> <p>Published: <?php the_time('M jS, Y'); </p> ?> <?php } the_excerpt(); ?> <p><a href="<?php the_permalink() ?>"><?php the_title(); echo ': '. $postTypeAtts[2].' &raquo;' ?></a></p> 20
  • 22. PRETTY PAGINATION Helping visitors move around lists of posts and search results. 22
  • 23. Pretty pagination We‟re all familiar with this: Achieved using previous_posts_link() and next_posts_link() This might be OK for site with not many posts, but what if you‟ve got loads? 23
  • 24. Pretty pagination Might this work better? Now your visitors have a much better idea of how many posts there are. And they can quickly re-find one later. 24
  • 25. Pretty pagination Plugins available to do this: • wp-pagenavi • wp-paginate But you do need to edit theme files to use them. Because of customisation requirements I decided to build my own functionality. 25
  • 26. Pretty pagination In WordPress, the common queries are paged by default – for example: • Blog index page • Archive pages – categories, tags, etc • Search results The number of posts shown on each page is set in admin settings. (But can be overridden by use of the pre_get_posts hook.) Custom queries using WP_Query are not paged by default but paging can be induced by use of posts_per_page and paged in query arguments. 26
  • 27. Pretty pagination Within each query there are two useful values that we can use to help create the new style paging. get_query_var('paged') • Tells us which page of results we‟re on. $wp_query -> max_num_pages • Gives the total number of pages required to show all results from the query. • Works with default queries and with custom queries. 27
  • 28. Pretty pagination We‟ll need some code in the loop, and a function to actually create the HTML for the new-style paging links. Get the values at the start of the loop for use later if (have_posts()) : $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $pageMax = $wp_query -> max_num_pages; while (have_posts()) : the_post(); 28 Note that get_query_var('paged') is not set on first results page so use this ternary operator to force value of $paged to 1 if it‟s not set.
  • 29. Pretty pagination At the point you want the paging links to appear call a new function: <?php endwhile; // endwhile for the loop or query // If more than one page show links to others if ($pageMax > 1) { echo '<h3>Other Blog Entries</h3>'; echo doPaging($wp_query, $paged); } ?> <?php else : ?> 29
  • 30. Pretty pagination And the function – part 1: function doPaging($myQuery, $paged) { $str = '<ul class="paging">'; // Previous link? Yes if we're not on page 1 if ($paged > 1) { $str .= '<li><a href="?paged='.($paged-1).'">&laquo; Newer Posts</a></li>'; } ... 30
  • 31. Pretty pagination The doPaging function – part 2: ... for($i=1; $i<=$myQuery->max_num_pages; $i++){ // Check if we're on that page if ($paged==$i) { // we are, so no link $str .= '<li class="this-page"><span class=“srdr">Page </span>'.$i.'</li>'; } else { // put out the link $str .= '<li><a href="?paged=' . $i.'"><span class=“srdr">Page </span>'.$i.'</a></li>'; } } ... 31
  • 32. Pretty pagination The doPaging function – part 3: ... // Older posts link? Yes if we're not on last page if ($paged < $myQuery->max_num_pages) { $str .= '<li><a href="?paged='.($paged+1).'">Older Posts &raquo;</a></li>'; } $str .= '</ul>'; return $str; } 32
  • 33. Pretty pagination Possible Enhancements: • Customisation of text strings – eg “Next” and “Previous” where custom post types or custom queries are used – the results may not all be in chronological order. • Amendments to function call to cater for paging on search results – the required URLs for search results pages are in a different format. • Catering for sites with many, many pages of posts: 33
  • 34. Summary We‟ve seen just a few small enhancements that can be made to our sites that can improve the visitors‟ experience. There are others: • Keeping the search query in the search box when showing results. • Secondary navigation for child and sibling pages – not just relying on dropdown menus. 34
  • 35. Thanks for listening – any (more) questions? [email protected] @coolfields 35