Site logo

[Resolved] Custom Post Types – Archive title & Masonry and aleatory

Home Forums Support [Resolved] Custom Post Types – Archive title & Masonry and aleatory

Home Forums Support Custom Post Types – Archive title & Masonry and aleatory

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #675822
    Jorge

    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

    #675922
    Tom
    Lead Developer
    Lead Developer

    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 🙂

    #676619
    Jorge

    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

    #676686
    Jorge

    Ready done! there was no closing on line 7 );
    of the code to randomize
    Thanks, I owe you a beer!
    Jorge

    #676815
    Tom
    Lead Developer
    Lead Developer

    Ah, sorry about that! Glad it’s working 🙂

    #692315
    Jorge

    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

    #692674
    Tom
    Lead Developer
    Lead Developer

    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?

    #692723
    Jorge

    No Tom, I do not have templates attached to my child theme, I think they use the archive.php of the original theme.
    Jorge

    #692981
    Tom
    Lead Developer
    Lead Developer

    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.

    #693184
    Jorge

    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

    #693764
    Tom
    Lead Developer
    Lead Developer

    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

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.