0

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?

----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(); ?>
0

You have the right idea.

You can start dividing this up into individual templates in order to prevent repeating yourself or having multiple templates that do the same thing.

You should use get_template_part() to pull in different templates as you need them. This allows you to branch out your templates further without WordPress dictating how it should be done.

In your case, you may have a template for your generic loop that calls a template for individual items in a post list context that calls a template for each different type of item (your custom post type).

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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