Skip to main content
added 1237 characters in body
Source Link

----EDIT-----

Cracked it. Needed to access the 'name' property of the WP_Post_Type Object. Fully working code for anyone who needs it.

<?php if ( $query2->have_posts() ) : ?>
    <?php while ( $query2->have_posts() ) : $query2->the_post();?> 
           <?php $postType = get_post_type_object(get_post_type()); ?>
            <?php if ($postType->name == 'al-comment' ) : ?>
            <article class = "comment">
                <h2> Comment type </h2><br/>
            </article>
            <?php elseif ($postType->name == 'al-blog' ) : ?>
            <article class = "blog">
                <h2> Blog type </h2><br/>
            </article>
            <?php elseif ($postType->name == 'al-link' ) : ?>
            <article class = "link">
                <h2> Link type </h2><br/>
            </article>
            <?php elseif ($postType->name == 'al-ytvid' ) : ?>
            <article class = "ytvid">
                <h2> YouTube Video type </h2><br/>
            </article>

                <?php else : ?>
                     Standard post type <br/>
<?php endif; ?> 
<?php endwhile; ?>
<?php else : ?>
<?php echo "Error: 404"; ?>
<?php endif ?>
<?php wp_reset_postdata(); ?>

----EDIT-----

Cracked it. Needed to access the 'name' property of the WP_Post_Type Object. Fully working code for anyone who needs it.

<?php if ( $query2->have_posts() ) : ?>
    <?php while ( $query2->have_posts() ) : $query2->the_post();?> 
           <?php $postType = get_post_type_object(get_post_type()); ?>
            <?php if ($postType->name == 'al-comment' ) : ?>
            <article class = "comment">
                <h2> Comment type </h2><br/>
            </article>
            <?php elseif ($postType->name == 'al-blog' ) : ?>
            <article class = "blog">
                <h2> Blog type </h2><br/>
            </article>
            <?php elseif ($postType->name == 'al-link' ) : ?>
            <article class = "link">
                <h2> Link type </h2><br/>
            </article>
            <?php elseif ($postType->name == 'al-ytvid' ) : ?>
            <article class = "ytvid">
                <h2> YouTube Video type </h2><br/>
            </article>

                <?php else : ?>
                     Standard post type <br/>
<?php endif; ?> 
<?php endwhile; ?>
<?php else : ?>
<?php echo "Error: 404"; ?>
<?php endif ?>
<?php wp_reset_postdata(); ?>
Source Link

How to create an IF statement in the Main Loop for Custom Post Types

Firstly, this is my first WP project and I'm learning as I go... I know this is probably an easy thing to achieve, but for some reason I'm having trouble getting it to work... so apologies if this is a noob question...

But I'm creating a social networking site that has different types of posts similar to Twitter. I have 5 custom post types for comment / status update / 'tweets', image posts, weblink posts, blog posts and video posts.

I created single-[post-type].php files, but this obviously only changed how they are outputted on single pages and doesn't change the HTML markup within the main loop.

So what I've been trying to do in my main loop is have a few else if statements... So 'If post-type = "comment"... then output this HTML'. 'Else if post-type="link" then use this HTML'...

I've tried a few ways... this was the most recent. Here's my current main WP loop code;

<?php if ( $query2->have_posts() ) : ?>
    <?php while ( $query2->have_posts() ) : $query2->the_post();?> 
           <?php $postType = get_post_type_object(get_post_type()); ?>
            <?php if (is_singular( 'comment' )) : ?>
                <h2> Custom Post Type Here </h2>

                <?php else : ?>
                        <article class ="post">
                            <a href="<?php the_permalink(); ?>">
                                <h3><?php the_title() ?></h3>
                                <a href="<?php the_permalink(); ?>">
                                <p class="meta">posted a blog on 24th May 2019 at 22:17</p>
                                <p><?php the_content(); ?></p>
                            </article>
                            </a>
                            <hr>
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
<?php echo "Error: 404"; ?>
<?php endif ?>

Can someone more experience let me know the right way to do this please?