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,

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

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(); ?>
Share Improve this question edited Jul 6, 2019 at 17:15 ElCodes asked Jul 6, 2019 at 13:29 ElCodesElCodes 11 bronze badge 0
Add a comment  | 

1 Answer 1

Reset to default 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).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745340833a4623318.html

相关推荐

  • 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,

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信