Archived topic
Custom Post Types - Archive title & Masonry and aleatory
10 replies · Started by Jorge on September 12, 2018
Hello, I have created a Custom Post Type (propiedades) for a real estate website.
I have also created archives-propiedades.php placed in child theme and I want the title "Archives: propiedades" not to be visible and to be able to place another new title, but I do not know where to change it.
I also want to show archives-propiedades.php with a random order so that all the "propiedades" have equal probability of being up on each page load that is in masonry style.
Could you please tell me from where or how can I modify these things?
Thank you very much in advance
Jorge
Hi there,
For the title, do this:
add_filter( 'get_the_archive_title', function( $title ) {
if ( is_post_type_archive( 'propiedades' ) ) {
return 'Your title here';
}
return $title;
} );
For the random order, try this:
add_filter('pre_get_posts', function( $query ) {
if ( ! is_admin() && $query->is_main_query() && is_post_type_archive( 'propiedades' ) ) {
$query->set('orderby', 'rand');
}
return $query;
} );
Let me know if this helps or not :)
Thank you very much Tom
The title change went OK
The randomized unfortunately did not work, returns HTTP ERROR 500
Could you try to check it? please
Jorge
Ready done! there was no closing on line 7 );
of the code to randomize
Thanks, I owe you a beer!
Jorge
Ah, sorry about that! Glad it's working :)
Template Archive with all post types together
Hello, excuse this reopening but it is to complete. I have several Post Types, and I want to put them all together in an "archive" type template, I know how I show type by type, but I do not know how to show them all together type1, type2, etc and in random masonry format.
Thank you so much
Jorge
GP Premium 1.7.2
Hi there,
So you want one archive template that displays multiple post types? Do you have a custom archive template added to your child theme?
No Tom, I do not have templates attached to my child theme, I think they use the archive.php of the original theme.
Jorge
So you'd likely need to create a custom template file along with a custom query that calls multiple post types.
get_posts() is the function you would use to query the multiple post types.
I understand, could you advise me? which template.php should I take as a model to introduce this function get_post () for displaying all the post (I guess it can be archive.php, but I'm not sure)
My knowledge of php are very few.
Thank so much. Jorge
It depends which archive you're trying to target.
All archives would be archive.php. Others would be named differently: https://developer.wordpress.org/themes/basics/template-hierarchy/#custom-post-types
The examples here might help: https://wordpress.stackexchange.com/questions/256428/how-to-show-posts-from-multiple-post-types-in-a-single-loop-and-display-them-se