- This topic has 18 replies, 3 voices, and was last updated 4 years, 8 months ago by
David.
-
AuthorPosts
-
July 19, 2021 at 3:54 pm #1861733
Ken
Hi, Recently I had my coach setup four custom posts types (CPT). So far the post look good when published.
Problem: When I go to a Category that was assigned to a CPT, instead of seeing the Category Post in 3-columns, I see only one long column. See attached screenshot.Is there some kind of magic coding I can do to fix this so that the Category Posts show up in 3-columns?
Appreciate any helpful suggestions.
link to Category URL
Link to Screenshot
Temporary login LinkJuly 19, 2021 at 8:27 pm #1861938Ken
UPDATE: I moved all the CPT to a new category see link below.
You can see each custom posts is in one column. there should be three columns. You can see three columns if you click on any other catogeries.I’m still researching an answer.
July 19, 2021 at 8:38 pm #1861950Ken
Hi again, apologies. I meant to tell you what my coach did. She sent me the following:
When I assign a custom post type to a category it removes the column layout for that category page. I followed the steps in this post to add columns code to the child theme’s functions.php file but it doesn’t seem to work: https://docs.generatepress.com/article/using-columns-in-the-blog/#adding-columns-to-your-custom-post-type. Maybe the code is supposed to be inside the custom post template instead? Can you check with support?
I hope this helps with the troubleshooting.
Please let me know if the code should be inside the custom post template, and if so can you kindly show me where I should put the code? You’ll only need to show me once and I can then do the same for the three other custom post types. Thank you.
July 19, 2021 at 8:46 pm #1861962Elvin
StaffCustomer SupportHi Ken,
You can sort this out by using few filter.
Check our documentation here – https://docs.generatepress.com/article/using-columns-in-the-blog/
July 19, 2021 at 8:50 pm #1861969Ken
Hi Elvin,
I guess you didn’t see my screenshot. I selected 3 columns but only when I added the custom posts to a category, then navigate to the category it would show all posts in one column. It should be three. No problem with regular non-custom posts, they show up in three columns.I’m not sure how to fix this.
July 19, 2021 at 8:52 pm #1861973Ken
If you’re referring to this:
Adding columns to your custom post type
By default, columns only apply to the post post type. However, you can add columns to your custom post type using the filter:add_filter( ‘generate_blog_columns’,’tu_portfolio_columns’ );
function tu_portfolio_columns( $columns ) {
if ( is_post_type_archive( ‘portfolio’ ) ) {
return true;
}return $columns;
}I’d like to know where I should place this filter? It sounds like I should add it to each of the four custom posts that I have–Is that correct?
If so,
Not sure where to place the code.July 19, 2021 at 8:58 pm #1861987Ken
Ahh, sorry, forgot to add the following;
If it is the case where I should add the filter to the custom post
Let’s try an example:
In the child theme there’s
single-one_product.php
and
content-one_product.phpQuestions:
1) which one (single or content) should I put the code in?
2) where in ‘that one (single or content)” should I paste the code?Apologies for all these posts. I appreciate your help, understanding and above all patience with me.
July 19, 2021 at 9:03 pm #1861990Ken
This is what I have in my ‘functions.php’ file:
<?php
/**
* GeneratePress child theme functions and definitions.
*
* Add your custom PHP in this file.
* Only edit this file if you have direct access to it on your server (to fix errors if they happen).
*/add_filter( ‘generate_blog_columns’,’tu_portfolio_columns’ );
function tu_portfolio_columns( $columns ) {
if ( is_post_type_archive( ‘list_post’,’one_product’,’ten_product’,’five_product’,’how_to’ ) ) {
return true;
}return $columns;
}add_filter( ‘generate_blog_masonry’,’tu_portfolio_masonry’ );
function tu_portfolio_masonry( $masonry ) {
if ( is_post_type_archive( ‘list_post’,’one_product’,’ten_product’,’five_product’,’how_to’ ) ) {
return ‘true’;
}return $masonry;
}function my_cptui_add_post_types_to_archives( $query ) {
// We do not want unintended consequences – also modified to include home page.
if ( is_admin() || ! $query->is_main_query() ) {
return;
}if ( is_category() || is_tag() || is_home() && empty( $query->query_vars[‘suppress_filters’] ) ) {
$cptui_post_types = cptui_get_post_type_slugs();$query->set(
‘post_type’,
array_merge(
array( ‘post’ ),
$cptui_post_types
)
);
}
}
add_filter( ‘pre_get_posts’, ‘my_cptui_add_post_types_to_archives’ );add_filter(‘widget_posts_args’, ‘widget_posts_args_add_custom_type’);
function widget_posts_args_add_custom_type($params) {
$params[‘post_type’] = array(‘post’,’one_product’,’ten_product’,’five_product’,’list_post’,’how_to’);
return $params;
}function my_cptui_add_post_types_to_rss( $query ) {
// We do not want unintended consequences.
if ( ! $query->is_feed() ) {
return;
}$cptui_post_types = cptui_get_post_type_slugs();
$query->set(
‘post_type’,
array_merge(
array( ‘post’ ),
$cptui_post_types
)
);
}
add_filter( ‘pre_get_posts’, ‘my_cptui_add_post_types_to_rss’ );July 19, 2021 at 9:06 pm #1861993Elvin
StaffCustomer SupportPerhaps I’m not understanding the structure clearly.
To clarify: You want custom post type archives to have 3 columns as well?
If so, then the
generate_blog_columnsfilter you’ve spotted is the way to go.You place that on a child themes functions.php or a code snippets plugin.
Here’s a modified example for 4 cpts.
add_filter( 'generate_blog_columns','tu_portfolio_columns' ); function tu_portfolio_columns( $columns ) { if ( is_post_type_archive( array('cpt1','cpt2','cpt3','cpt4') ) ) { return true; } return $columns; } add_filter( 'generate_blog_get_column_count','tu_search_column_count' ); function tu_search_column_count( $count ) { if ( is_post_type_archive( array('cpt1','cpt2','cpt3','cpt4') ) ) { return 33; } return $count; }You basically change cpt1, cpt2, cpt3, cpt4 with your cpt slugs. 😀
July 19, 2021 at 9:14 pm #1862002Ken
Ah, it’s not working. Here’s what I did:
<?php
/**
* GeneratePress child theme functions and definitions.
*
* Add your custom PHP in this file.
* Only edit this file if you have direct access to it on your server (to fix errors if they happen).
*/add_filter( ‘generate_blog_columns’,’tu_portfolio_columns’ );
function tu_portfolio_columns( $columns ) {
if ( is_post_type_archive( array(‘list_post’,’one_product’,’ten_product’,’five_product’,’how_to’ ) ) ){
return true;
}return $columns;
}add_filter( ‘generate_blog_get_column_count’,’tu_search_column_count’ );
function tu_search_column_count( $count ) {
if ( is_post_type_archive( array(‘list_post’,’one_product’,’ten_product’,’five_product’,’how_to’) ) ) {
return 33;
}return $count;
}
add_filter( ‘generate_blog_masonry’,’tu_portfolio_masonry’ );
function tu_portfolio_masonry( $masonry ) {
if ( is_post_type_archive( ‘list_post’,’one_product’,’ten_product’,’five_product’,’how_to’ ) ) {
return ‘true’;
}return $masonry;
}function my_cptui_add_post_types_to_archives( $query ) {
// We do not want unintended consequences – also modified to include home page.
if ( is_admin() || ! $query->is_main_query() ) {
return;
}if ( is_category() || is_tag() || is_home() && empty( $query->query_vars[‘suppress_filters’] ) ) {
$cptui_post_types = cptui_get_post_type_slugs();$query->set(
‘post_type’,
array_merge(
array( ‘post’ ),
$cptui_post_types
)
);
}
}
add_filter( ‘pre_get_posts’, ‘my_cptui_add_post_types_to_archives’ );add_filter(‘widget_posts_args’, ‘widget_posts_args_add_custom_type’);
function widget_posts_args_add_custom_type($params) {
$params[‘post_type’] = array(‘post’,’one_product’,’ten_product’,’five_product’,’list_post’,’how_to’);
return $params;
}function my_cptui_add_post_types_to_rss( $query ) {
// We do not want unintended consequences.
if ( ! $query->is_feed() ) {
return;
}$cptui_post_types = cptui_get_post_type_slugs();
$query->set(
‘post_type’,
array_merge(
array( ‘post’ ),
$cptui_post_types
)
);
}
add_filter( ‘pre_get_posts’, ‘my_cptui_add_post_types_to_rss’ );July 19, 2021 at 9:16 pm #1862003Ken
Forgot to confirm: yes, I would love to have 3-columns for custom post types.
July 19, 2021 at 11:44 pm #1862134Elvin
StaffCustomer SupportStrange.
Can you link me to a category archive page of a custom post type and a category archive page of a default page.
The archive linked on the private information text field doesn’t seem to be an archive for CPT category. (I’ve clicked a post on it and the post type was a default post.)
July 20, 2021 at 12:23 am #1862194Ken
I’ve attached the links below.
July 20, 2021 at 4:13 am #1862485David
StaffCustomer SupportHi there,
that condition checks for a custom post type archive, where it looks like you’re just displaying a CPT in a default archive.
Try this snippet instead:add_filter( 'generate_blog_columns', function( $columns ) { if ( in_array(get_post_type(), array('cpt-1','cpt-2','cpt-3')) && ! is_singular() ) { return true; } return $columns; } );Update the
array()to your CPT slugs.July 20, 2021 at 9:08 am #1863160Ken
Hi David, bummers, it didn’t work. Here’s how my Functions.php looks after adding the code you gave me.
I’ve attached a VDO cast. I may sound a bit slow, it’s 1am here in Japan.<?php
/**
* GeneratePress child theme functions and definitions.
*
* Add your custom PHP in this file.
* Only edit this file if you have direct access to it on your server (to fix errors if they happen).
*/add_filter( ‘generate_blog_columns’, function( $columns ) {
if ( in_array(get_post_type(), array(‘list_post’,’one_product’,’ten_product’,’five_product’,’how_to’)) && ! is_singular() ) {
return true;
}return $columns;
} );add_filter( ‘generate_blog_get_column_count’,’tu_search_column_count’ );
function tu_search_column_count( $count ) {
if ( is_post_type_archive( array(‘list_post’,’one_product’,’ten_product’,’five_product’,’how_to’) ) ) {
return 33;
}return $count;
}add_filter( ‘generate_blog_masonry’,’tu_portfolio_masonry’ );
function tu_portfolio_masonry( $masonry ) {
if ( is_post_type_archive( ‘list_post’,’one_product’,’ten_product’,’five_product’,’how_to’ ) ) {
return ‘true’;
}return $masonry;
}function my_cptui_add_post_types_to_archives( $query ) {
// We do not want unintended consequences – also modified to include home page.
if ( is_admin() || ! $query->is_main_query() ) {
return;
}if ( is_category() || is_tag() || is_home() && empty( $query->query_vars[‘suppress_filters’] ) ) {
$cptui_post_types = cptui_get_post_type_slugs();$query->set(
‘post_type’,
array_merge(
array( ‘post’ ),
$cptui_post_types
)
);
}
}
add_filter( ‘pre_get_posts’, ‘my_cptui_add_post_types_to_archives’ );add_filter(‘widget_posts_args’, ‘widget_posts_args_add_custom_type’);
function widget_posts_args_add_custom_type($params) {
$params[‘post_type’] = array(‘post’,’one_product’,’ten_product’,’five_product’,’list_post’,’how_to’);
return $params;
}function my_cptui_add_post_types_to_rss( $query ) {
// We do not want unintended consequences.
if ( ! $query->is_feed() ) {
return;
}$cptui_post_types = cptui_get_post_type_slugs();
$query->set(
‘post_type’,
array_merge(
array( ‘post’ ),
$cptui_post_types
)
);
}
add_filter( ‘pre_get_posts’, ‘my_cptui_add_post_types_to_rss’ ); -
AuthorPosts
- You must be logged in to reply to this topic.