Skip to content

Instantly share code, notes, and snippets.

@frosso
Created January 28, 2021 22:47
Show Gist options
  • Save frosso/71d195bdf33be4e4a8f4225f761c2533 to your computer and use it in GitHub Desktop.
Save frosso/71d195bdf33be4e4a8f4225f761c2533 to your computer and use it in GitHub Desktop.
[PHP/WordPress/WooCommerce] Display category description on pagination
<?php
// the `woocommerce_taxonomy_archive_description` and the `woocommerce_product_archive_description` functions do not display the category's description on pagination
// this code removes them from WooCommerce and rewrites them to show the description also on new pages
// Before: page 1 (https://d.pr/i/E2iHoD) & page 2 (https://d.pr/i/RiiCOy)
// After: page 1 (https://d.pr/i/E2iHoD) & page 2 (https://d.pr/i/DyGEVQ)
remove_action( 'woocommerce_archive_description', 'woocommerce_taxonomy_archive_description', 10 );
remove_action( 'woocommerce_archive_description', 'woocommerce_product_archive_description', 10 );
add_action( 'woocommerce_archive_description', function () {
if ( is_product_taxonomy() ) {
$term = get_queried_object();
if ( $term && ! empty( $term->description ) ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<div class="term-description">' . wc_format_content( $term->description ) . '</div>';
}
}
} );
add_action( 'woocommerce_archive_description', function () {
// Don't display the description on search results page.
if ( is_search() ) {
return;
}
if ( is_post_type_archive( 'product' ) ) {
$shop_page = get_post( wc_get_page_id( 'shop' ) );
if ( $shop_page ) {
$description = wc_format_content( $shop_page->post_content );
if ( $description ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<div class="page-description">' . $description . '</div>';
}
}
}
} );
@frosso
Copy link
Author

frosso commented Jan 28, 2021

Description

Before:
On page 1 the description is present:
Screen Shot 2021-01-28 at 4 42 33 PM
But on page 2 it isn't:
Screen Shot 2021-01-28 at 4 42 41 PM

After:
On page 1 the description is present:
Screen Shot 2021-01-28 at 4 42 33 PM
And it is also present on page 2:
Screen Shot 2021-01-28 at 4 42 52 PM

How to use it

Use WordPress' guidelines to alter your theme's functions or write a new plugin.
As an alternative, just go to your theme's functions.php file and add these lines (without the initial <?php tag)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment