Archived topic
Modifying category pages possibility
13 replies · Started by William on April 4, 2021
Hi there,
With a category page such as this, I was wondering if the following is possible in Generate Press?
1) Changing the title for each post to something unique, such as a custom field or the Breadcrumb title with Yoast:

As this will help to remove a lot of 'By ______' in the title that isn't the best for SEO of UX
2) Increase the number of posts that appear per paginated page on category pages
3) Add filters (Ajax if possible) at the top of the pages that can sort the posts such as alphabetically, date published, or based on another custom field?
Many thanks,
Will
Hi there,
As for #1:
I'm not sure I understand what you mean.
Are you trying to dynamically change the breadcrumb title value by taking something from a custom field?
If that's the case, it really depends on the breadcrumbs code you're using. If you're using a plugin for your breadcrumbs, you should ask the plugin's developer/support on best practices on how to do it.
As for #2:
You need to change the post per page for them.
Try this PHP snippet:
add_action( 'pre_get_posts', function( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_category('your-category-slug') ) {
$query->set( 'posts_per_page', 9 );
}
} );
Change your-category-slug to the category slug you want to increase the post displayed and change the 9 to the number of posts you want to appear.
If you want to apply it on multiple categories, you can modify the code into something like this:
add_action( 'pre_get_posts', function( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_category( array('your-category-slug-1','your-category-slug-2','your-category-slug-3') ) ) {
$query->set( 'posts_per_page', 9 );
}
} );
Or if you want it to apply to ALL category archives, simply leave the is_category( ) empty.
Example:
add_action( 'pre_get_posts', function( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_category() ) {
$query->set( 'posts_per_page', 9 );
}
} );
As for #3:
You'll need a plugin that does this exact feature. This isn't a default GP feature, unfortunately.
Thanks for that! For #1 it's basically I don't want the post title to appear for the archvies pages of posts - instead, I would rather the displayed title on these pages be something else, such as my own shortcode or the 'Breadcrumb title' that yoast use.
Basically, if an article is 'Still I Rise by Maya Angelou', I want that post to appear as 'Still I Rise' on the category page, because the category page is Maya Angelou (doesn't need repeating again)
Interesting, do you want to keep the "By xx" on the single post?
Not 100% sure it will work, but you could try this:
add_filter( 'the_title', function( $title ) {
if ( ! is_archive() ) {
return $title;
}
return strpos( $title, "by" ) ? substr( $title, 0, strpos( $title, "by" ) ) : $title;
} );
That works a treat! The only issue is that for some poems 'by' is in the poem title.
Take the post title 'Stopping by Woods on a Snowy Evening by Robert Frost'. The above code causes the post title to appear as the below:

Is there a way so that it only removed the last 'by ____' instead of what appears above (so the poem always appears)?
Hi there,
Let's try modifying Tom's code a bit.
Try this:
add_filter( 'the_title', function( $title ) {
if ( ! is_archive() ) {
return $title;
}
return strrpos( $title, "by" ) ? substr( $title, 0, strrpos( $title, "by" ) ) : $title;
} );
Ah that's amazing thank you!
Is it possible to only show these for categories archives and no other categories?
Not sure if this works as you want but try this out.
add_filter( 'the_title', function( $title ) {
if ( ! is_archive() ) {
return $title;
}
if ( is_category() ) {
return strrpos( $title, "by" ) ? substr( $title, 0, strrpos( $title, "by" ) ) : $title;
}
} );
That's great thank you!
No problem. Glad to be of any help. :D
Hi there,
Thanks for this code, but it appears on author archives the posts disappear completely when the above code is implemented. Is there a way for the code to be modified so the post title is kept in tact for author pages?
Kind regards,
What happens if you try this?
add_filter( 'the_title', function( $title ) {
if ( is_category() ) {
$title = strrpos( $title, "by" ) ? substr( $title, 0, strrpos( $title, "by" ) ) : $title;
}
return $title;
} );
Let us know.
Perfect :) thank you!
Nice one. Glad it works for you. No problem. :D