0

I have a homepage that is mixing two post-types : post and events

My aim is to print the details of each event in order to differentiate the content.

This is the method that I used to mix posts and events inside the Loop :

// The Query
        $args = array(
           'post_type' => array( 'post', 'event', 'book' )
            );
    
        $query = new WP_Query( $args );

In the Loop, I am printing the title, the excerpt and I wonder if it is possible then to introduce a condition (inside the Loop) to verify of this is an event and then retrieve some details about it. It looks exactly like this:

// The Loop
        
        if ( $query->have_posts() ) : ?>

        <?php if ( is_home() && ! is_front_page() ) : ?>
            <header>
                <h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
            </header>
        <?php endif; ?>

        <?php
        // Start the loop.
        while ( $query->have_posts() ) :
            $query->the_post();

            /*
             * Instead of publishing all the content, on home page we only get the title, the thumbnail and an excerpt
             */
        the_title( '<h1 class="entry-title">', '</h1>' );
        the_excerpt() ;
        
        // Test if this is an event and then print details
        if (is_singular('event') ) {
            eo_get_template_part( 'event-meta', 'event-single' );
        }

            // End the loop.
        endwhile;

I am not really sure that I am using the best approach on this. I have seen that there is also another way to mix contents by modifying the global query in functions.php

Someone here can please tell me how can accomplish this ?

3
  • is_singular and other conditionals always refer to the current global query, not custom queries. It will only return true if you're on the page of an event post, regardless of the type of the current post in the loop
    – Tom J Nowell
    Nov 7, 2022 at 17:06
  • @TomJNowell thank you for your answer ! I know how to use two different loops to print different contents for different post types, but in this case, what I want is to do a single list of actualities. Do you know if it is possible to print different informations inside only one Loop ?
    – Gadget
    Nov 7, 2022 at 17:55
  • I'm trying a new approach with this kind of condition : if ( 'event' == get_post_type() )
    – Gadget
    Nov 7, 2022 at 19:26

1 Answer 1

0

Of course, you can use conditions like this inside the loop.

if ( 'event' == get_post_type( get_the_ID() ) ) {
  // do something
}

Full code would be

// The Loop
    
    if ( $query->have_posts() ) : ?>

    <?php if ( is_home() && ! is_front_page() ) : ?>
        <header>
            <h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
        </header>
    <?php endif; ?>

    <?php
    // Start the loop.
    while ( $query->have_posts() ) :
        $query->the_post();

        /*
         * Instead of publishing all the content, on home page we only get the title, the thumbnail and an excerpt
         */
    the_title( '<h1 class="entry-title">', '</h1>' );
    the_excerpt() ;
    
    // Test if this is an event and then print details
    if (is_singular('event') ) {
        eo_get_template_part( 'event-meta', 'event-single' );
    }
    if ( 'event' == get_post_type( get_the_ID() ) ) {
      // do something
    }

        // End the loop.
    endwhile;
3
  • Hello @Rajan ! Thank you very much for your answer. I adjust a little bit the code in order to introduce if [it is an event] / else [this is a post].For some reason, I have two errors : 1. Event details are called but empty. 2. The loop is stoping after the first event.
    – Gadget
    Nov 8, 2022 at 17:47
  • Definitely the condition that you sent me is working well, but there is something wrong with the template-part that I am calling (event-meta-event-organiser). I will be looking for this on next days ! Thank you very much for your help !!!
    – Gadget
    Nov 8, 2022 at 18:23
  • Hello @Gadget, Thank you for your response. Nov 9, 2022 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.