Tell us what you are trying to do?
I use the pro Generatepress theme (with GP Premium plugin). This theme allows the display of the post archive page as a two-column masonry grid. I've created a few custom post types and I want them to follow the same archive layout as posts.
Is there any documentation that you are following?
I'm using the code from this page: hidden link
I added the following code via Toolset's Custom Code Snippets feature:
//////////////
add_filter( 'generate_blog_masonry','qa_faqs_masonry' );
function qa_faqs_masonry( $columns ) {
if ( is_post_type_archive( 'FAQ' ) ) {
return true;
}
return $masonry;
}
////////////
My CPT Name: FAQ
My CPT slug: qa_faqs
Is there a similar example that we can see?
This is the masonry layout for normal posts:
hidden link
Any time an FAQ post type appears in the archive list, it messes up the layout.
What is the link to your site?
This is the archive for my FAQ cpt:
hidden link
The posts should be displayed in a two column masonry grid. What am I missing?
Hi,
Thank you for contacting us and I'd be happy to assist.
I've noticed a couple of points about the snippet that you've shared:
1. In the function's name (line# 2), it is using the "$columns" parameter, but at the end (line# 7), it is returning the "$masonry" variable, which is not defined anywhere. They both should be the same variables.
2. Within the "is_post_type_archive( )", the post type's slug should be used: is_post_type_archive( 'qa_faqs' ).
( ref: https://developer.wordpress.org/reference/functions/is_post_type_archive/ )
3. The conditional check for the "is_post_type_archive( 'qa_faqs' )" would only apply to the FAQ post type's archive page, which is at:
{yourwebsite.com}/qa_faqs/
The page that you're trying to target is the category archive page for the term "FAQ":
{yourwebsite.com}/category/faq/
To target that archive page, you'll need to use "is_category( )" conditional check:
https://developer.wordpress.org/themes/basics/conditional-tags/#a-category-page
Example of the snippet that should work for both those archive pages:
add_filter( 'generate_blog_masonry','qa_faqs_masonry' );
function qa_faqs_masonry( $masonry ) {
if ( (is_post_type_archive( 'qa_faqs' )) || (is_category( 'faq' )) ) {
return 'true';
}
return $masonry;
}
regards,
Waqar
Thanks Waqar. That works.
I've added it as a custom code snippet in Toolset. Which setting should I use?
Run mode: "Always" or "On demand"
Run context: "Front-end / WordPress admin / AJAX calls"
Thanks for the update and glad I could help.
For "Run mode", you can select "Run always" and for "Run context", you can select "Front-end".
( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ )