Skip to content

Instantly share code, notes, and snippets.

@s-a-s-k-i-a
Last active July 9, 2019 10:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-a-s-k-i-a/4d3ee987a31f16cd6865d09d6c6f2191 to your computer and use it in GitHub Desktop.
Save s-a-s-k-i-a/4d3ee987a31f16cd6865d09d6c6f2191 to your computer and use it in GitHub Desktop.
WordPress: List Category Posts feat. Yoast SEO while using custom category.php with catlist shortcode
/*
* @author Saskia Lund
* @authorurl https://www.saskialund.de/
*
* When Yoast SEO and List Category Posts are activated alongside each other,
* there is an issue regarding canonicals and rel=next and rel=prev urls displayed in head html.
* List Category Posts is making use of its own pagination methods, while Yoast SEO is being WP codex compliant
* and is making use of WP pagination methods.
*
* Preparations
* Set up a custom category.php in your theme files and add the catlist shortcode to it, using
* echo do_shortcode('[catlist name="' . get_the_archive_title() . '" template=category numberposts=24 excerpt=yes excerpt_size=30 thumbnail=yes thumbnail_size=thumbnail date=yes posts_morelink_class="get_more_link" posts_morelink="Read more" pagination=yes]');
*
* To solve the incorrect output of Yoast SEO in this environment, while using
* a custom category.php file which contains echo do_shortcode('[catlist ....]'); to display the plugin's pagination,
* please add this code to your theme's functions.php file:
*/
add_filter( 'wpseo_next_rel_link', 'sl_change_wpseo_next' );
/* When using catlist shortcode in category.php, the general WP pagination is no longer relevant. Also we will never see
rel=prev in head html, because List Category Posts works with parameter URLs instead of WP permalink settings.
That is why we can only use this next filter, as it is being output in any case, while Yoast prev filter gets never output. */
function sl_change_wpseo_next( $link ) {
if ( is_category() && class_exists( 'ListCategoryPosts' ) ) {
$obj_id = get_queried_object_id();
$current_url = get_term_link( $obj_id );
if ( isset( $_GET['lcp_page0'] ) ) {
$category = get_category( $obj_id );
$totalPosts = $category->category_count;
$totalPages = (int)$totalPosts/24; /* Please change this number (24) to the value you set for numberposts=24 in catlist shortcode attribute */
$totalPages = ceil($totalPages);
$next_link = '';
$prev_link = '';
$currentpageNumber = $_GET['lcp_page0'];
$prevpageNumber = (int)$currentpageNumber - 1;
$nextpageNumber = (int)$currentpageNumber + 1;
if( $totalPages == $currentpageNumber ){
$next_link = '';
if( $currentpageNumber != 2 ){
$prev_link = $current_url.'?lcp_page0='.$prevpageNumber.'#lcp_instance_0';
} else {
$prev_link = $current_url;
}
} else {
if( $currentpageNumber == 1 ){
$next_link = $current_url.'?lcp_page0='.$nextpageNumber.'#lcp_instance_0';
$prev_link = '';
} else {
$next_link = $current_url.'?lcp_page0='.$nextpageNumber.'#lcp_instance_0';
$prev_link = $current_url.'?lcp_page0='.$prevpageNumber.'#lcp_instance_0';
}
}
} else {
// Fallback behaviour goes here
$next_link = $current_url.'?lcp_page0=2#lcp_instance_0';
$prev_link = '';
}
$next_link_out = '<link rel="next" href="'. $next_link .'" />';
$prev_link_out = '<link rel="prev" href="'. $prev_link .'" />';
if ( empty( $next_link ) ){
$next_link_out = '';
}
if ( empty( $prev_link ) ){
$prev_link_out = '';
}
$link = $prev_link_out . $next_link_out . PHP_EOL;
}
return $link;
}
function yoast_seo_canonical_change_sl_category( $canonical ) {
if( class_exists( 'ListCategoryPosts' ) ){
if ( !is_category() ) {
return $canonical;
} else {
$obj_id = get_queried_object_id();
$current_url = get_term_link( $obj_id );
if (isset($_GET['lcp_page0'])) {
$currentpageNumber = $_GET['lcp_page0'];
if( $currentpageNumber == 1 ){
$canonical = $current_url;
} else {
$canonical = $current_url.'?lcp_page0='.$currentpageNumber.'#lcp_instance_0';
}
} else {
$canonical = $current_url;
}
return $canonical;
}
} else {
return $canonical;
}
}
add_filter( 'wpseo_canonical', 'yoast_seo_canonical_change_sl_category', 10, 1 );
/* Any questions? Please feel free to ask. */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment