How to Fix Double Spaces and Formatting Issues with %%current_pagination%% in SEOPress Titles
Content
Users of the SEOPress – On-site SEO plugin sometimes encounter a formatting issue on the first page of archive listings (like categories or tags). The problem arises when using the %%current_pagination%% dynamic variable in title templates.
On paginated pages (page 2, 3, etc.), the title displays correctly. However, on page 1, where %%current_pagination%% outputs nothing, it can leave behind an unwanted double space before the separator (%%sep%%), making the title look unprofessional.
Why This Happens
The %%current_pagination%% variable is designed to be empty on the first page of an archive. When it's placed in a template like %%term_title%% %%current_pagination%% %%sep%% %%sitetitle%%, its empty value on page 1 results in two consecutive spaces between %%term_title%% and %%sep%%.
Solution: Clean Extra Spaces with a Code Snippet
The most effective solution is to add a small code snippet to your theme's functions.php file. This code will clean up any extra spaces caused by empty dynamic variables.
Step-by-Step Guide:
- Access your WordPress theme files, typically through your hosting provider's file manager or an FTP client.
- Locate your active theme's
functions.phpfile. It is highly recommended to use a child theme to prevent your changes from being overwritten during theme updates. - Open the
functions.phpfile for editing. - Paste the following code snippet at the very end of the file:
function sp_titles_title($html)
{
// This function removes any extra spaces from the title
$html = implode(' ', array_filter(explode(" ", $html)));
return $html;
}
add_filter('seopress_titles_title', 'sp_titles_title'); - Save the changes and upload the file back to your server if necessary.
This code works by filtering the final title output. It explodes the title string into an array using spaces, filters out any empty array elements (like our empty variable), and then implodes the array back into a string with single spaces. This ensures a clean title without double spaces on page 1.
This solution, shared by the SEOPress team in a support thread, is a reliable way to handle formatting issues caused by conditional dynamic variables.
Related Support Threads Support
-
Change month in title to numberhttps://wordpress.org/support/topic/change-month-in-title-to-number/
-
Current page number in title???https://wordpress.org/support/topic/current-page-number-in-title/
-
Problem with archive title templateshttps://wordpress.org/support/topic/problem-with-archive-title-templates/
-
Requesthttps://wordpress.org/support/topic/request-64/
-
Placeholder count – number of charactershttps://wordpress.org/support/topic/placeholder-count-number-of-characters/
-
paginated pages – self referencinghttps://wordpress.org/support/topic/paginated-pages-self-referencing/
-
Dynamic variable %%page%% ?https://wordpress.org/support/topic/dynamic-variable-page/