Site logo

Archived topic

Custom Post Type Archive in Columns

22 replies · Started by Johan on February 2, 2021

Viewing posts 1–15 of 23

Hello and thank you for your great product!
However, I have a problem:
I created a child theme a "Custom Post Type" and when I display it by archive the layout is in one column (see here: https://dev1.directsolutions.fr/offres/nos-formations / or https://dev1.directsolutions.fr/offres/nos-formations/securite-incendie/).
I would like it to display in 2 or three columns. I have tried several of the solutions given in these pages without it working.
This does not work:

add_filter ('generate_blog_columns', 'tu_formation_columns');
function tu_formation_columns ($ columns) {
if (is_post_type_archive ('training')) {
return true;
}
return $ columns;
}

Could you give me a solution?
Thank you
Johan Puisais

Hi there

can you confirm the Post Type you have set for the is_post_type_archive is correct - it should be formation

Hello and thank you David
Yes, I confirm it to you.
What is displayed here are the "custom post type" "formation" classified into categories

In the code you provided above - you have:

if (is_post_type_archive ('training')) {

This needs to be:

if (is_post_type_archive ('formation')) {

Yes I put "training" ... in fact I am French and during the translation I forgot to modify "formation" ;-) But yes my code is :

add_filter( 'generate_blog_columns','tu_formation_columns' );
function tu_formation_columns( $columns ) {
    if ( is_post_type_archive( 'formation' ) ) {
        return true;
    }
    return $columns;
}

In fact I have looked at all the forum answers that may apply to my problem here and have tested most of them. Without results...
Is it due to the version of my GeneratePress (I have the latest one)? Because the examples given do not seem to be the same as the original code I find in my files.

That code should work - how are you adding the code to your site?

Hello David,
Yes I added this code in the functions.php file of my child theme in "themes/generatepress_child"

Hi there,

What if you use nos-formations instead?

Let us know :)

Hello Tom,
I did it and I just do it again now. It doesn't change anything

For additional information and if this can help you, I also created an "archive-formation.php" which is not taken into account by the system while my "single-formation.php" works. It may not be related but ...

I suppose the first thing we need to do is figure out if our condition is right.

Does this output anything on your page?:

add_action( 'generate_before_header', function() {
    if ( is_post_type_archive( 'formation' ) ) {
        echo 'Hello world';
    }
} );

If not, it means the condition is wrong and needs to be tweaked.

Hello Tom,

I just inserted this code and nothing is displayed, no "Hello Word" :-(
The condition of must therefore not be the right one.

Instead of:

if ( is_post_type_archive( 'formation' ) ) {

try this in Tom's function:

if ( is_category( 'formation' ) ) {

After some tests I see that
add_action ('generate_before_header', function () {
if (is_category ()) {
echo 'Hello everyone';
}
});
works, as well as "is-archive ()"

"if ( is_category( 'formation' ) )" not works but "if ( is_category( ) )" Yes

And "if ( is_category( 'nos-formations' ) )" works only at "https://dev1.directsolutions.fr/offres/nos-formations/"

So this should work:

add_filter( 'generate_blog_columns','tu_formation_columns' );
function tu_formation_columns( $columns ) {
    if ( is_category( 'nos-formations' ) ) {
        return true;
    }
    return $columns;
}

Whatever archives does it need to be applied to?

This archived topic is closed to new replies.