Archived topic
Archive.php elements order
13 replies · Started by AdB on August 13, 2020
Hi, I've a question regarding archive.php
I created an archive template for a custom taxonomy (Standort): everything's working fine.
Is there a way to redefine the order of the elements (meta, data, post title etc) for this archive?
for example,
post category/ies
post title
post data
post excerpt
I already check out this post https://generatepress.com/forums/topic/customize-mobile-featured-image-archives/ but I guess I need a couple of more hints to achieve that ;)
I mean, the title / data / excerpt are already in this order. For this archive, I need only to move the categories on top. It'd be great also to know how generally can I define the order of such elements.
thank you very much!
Davide
Hi Davide,
Right now it's not overly easy, but we have a solution on the horizon.
So basically we just need to move the categories to above the title for this specific archive? Not the single posts?
Let me know :)
Yes, exactly. Only moving the category for this archive, thank you!
(single post: I'll put together "something" customized with some hooks. I've also a question regarding the single post layout, I'll open another topic ;))
thanks
Davide
You could try this:
add_filter( 'generate_footer_entry_meta_items', function( $items ) {
if ( is_post_type_archive( 'your-cpt-name' ) ) {
return array_diff( $items, array( 'categories' ) );
}
return $items;
} );
add_action( 'generate_before_entry_title', function() {
if ( is_post_type_archive( 'your-cpt-name' ) ) {
$categories_list = get_the_category_list( ' | ' );
if ( $categories_list ) {
printf( '<div class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</div>',
esc_html_x( 'Categories', 'Used before category names.', 'generatepress' ),
$categories_list
);
}
}
} );
aaa great thanks!
only one thing, the code should be for a taxonomy archive, so I changed is_post_archive() with
is_tax()
add_filter( 'generate_footer_entry_meta_items', function( $items ) {
if ( is_tax( 'standort' ) ) {
return array_diff( $items, array( 'categories' ) );
}
return $items;
} );
add_action( 'generate_before_entry_title', function() {
if ( is_tax( 'standort' ) ) {
$categories_list = get_the_category_list( ' | ' );
if ( $categories_list ) {
printf( '<div class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</div>',
esc_html_x( 'Categories', 'Used before category names.', 'generatepress' ),
$categories_list
);
}
}
} );
seems to work fine. I was about to write a quite related question in another post, but I guess I'll continue here (summarized: how can I display the categories / tags / author of a custom post type in an archive page. Normally only the post title, excerpt and custom image are displayed. With the code above I noticed that the category is displayed also for the custom post type, so you "accidentally" anticipated me)
Awesome! So it's working for both needs now?
Thanks for posting your code :)
Hi Tom, I didn't test it with the custom post type archive.
With this "custom taxonomy archive page", it seems to work fine, but what if I'd like to do the same also with both the category archive and the tag archive? They're both taxonomy, so it should work ... but actually it doesn't ¯\_(ツ)_/¯
and also, since the code above displays the categories of the custom posts too (which is not happening within a standard archive page), there's a way to do the same also for the posts date? It should go here --> 'generate_after_entry_title'.
Thank you very much
Davide
You would have to expand your is_tax() to include those other taxonomies I think.
Doesn't the post date display after the title by default?
Hi Tom,
with the custom post types, the post metadata (tags, category, post date etc.) aren't displayed in the archive pages (post archive, meta archive etc.).
So I did some further research, I found this post of yours:
I add the piece of code to my custom posts and to the standard wp-post type.
Everything's fine, except that in archive, for the standard posts, the post date and the categories are shown twice.
I solved the post date "problem" with that
add_action( 'after_setup_theme', function() {
remove_action( 'generate_after_entry_title', 'generate_post_meta' );
} );
How can I also remove the standard categories? With something like: "remove_action( 'generate_after_entry_content', ....... " ?
thank you
Davide
Instead of that code, this filter might be worth checking out: https://docs.generatepress.com/article/generate_entry_meta_post_types/
Hi Tom, thanks.
the code from your last post works fine for the post-dates (& meta).
Doing so I'm back with the categories display problem (the code of this post is not working with the normal categories and taxonomy archives.
That's why I went on with my research and found your other post which is working perfectly except that the post date and post categories of the standard post are displayed twice.
For the post-date I solved it like indicated in my last post, regarding the double categories, I now tried with some css
.cat-links {display:none;}
it seems to work, but I'm not 100% sure about that, that's why I'm still looking for something like remove_action.
Does it make sense? (a bit confused I know ;))
thanks!
Davide
So the only issue now is the double categories?
Where are the second set of categories displaying?
Hi Tom,
exactly.
the second set of categories are actually the standard one, so on the archive page they're displayed after the excerpt.
thank you
Removing those is what this function should do:
add_filter( 'generate_footer_entry_meta_items', function( $items ) {
if ( is_tax( 'standort' ) ) {
return array_diff( $items, array( 'categories' ) );
}
return $items;
} );
You just need to make sure the condition is correct. If it's not working, what's the condition you're trying?