WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom Loop - Get Post Format (4 posts)

  1. mak1wp
    Member
    Posted 11 months ago #

    Hi,

    I've created a custom loop template for the purpose of displaying the posts in various grid layouts. But I'm not sure how to specify the Post Format ie standard, quote, link, image.

    The standard post loop (index.php) which works to pull in the post format looks like this..

    <?php if ( have_posts() ) : ?>
    
    				<?php while ( have_posts() ) : the_post(); ?>
    
    					<?php get_template_part( 'content', get_post_format() ); ?>
    
    				<?php endwhile; ?>
    
    			<?php else : ?>
    				<?php get_template_part( 'content', 'none' ); ?>
    			<?php endif; ?>

    The custom one I've put together works like this...

    <div class="six columns">
    
    			<?php if ( have_posts() ) : ?>
    
                    <?php while ( have_posts() ) : the_post(); ?>
    
                    	<a href="<?php the_permalink() ?>" >   <?php the_post_thumbnail('the-hub-image', array('class' => 'alignleft')); ?></a>
    
                        <h2 class="postTitle"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>

    ... and goes on to specify the other features/layout I need.

    Is there any way to include the get_post_format in this custom layout loop in order to load the various different layout styles for post formats which aren't marked as 'standard' post?

    Many thanks in advance to any replies!

  2. alchymyth
    The Sweeper & Moderator
    Posted 11 months ago #

    you could try to use get_template_part() together with a selection of various templates as in the default code;

    or use a number of 'if/elseif/else' structures;

    example:

    <?php if ( have_posts() ) : ?>
    
                    <?php while ( have_posts() ) : the_post(); ?>
    
    <?php if( get_post_format() == 'quote' ) : ?>
    ANY OUTPUT FOR QUOTE POSTS
    <?php elseif( get_post_format() == 'link' ) : ?>
    ANY OUTPUT FOR LINK POSTS
    <?php else : ?>
    ANY OUTPUT FOR STANDARD POSTS
    <?php endif; ?>
    
    <?php endwhile;
    endif; //closing of the loop ?>
  3. mak1wp
    Member
    Posted 11 months ago #

    Amazing, thanks so much for your help @alchymyth that worked an absolute charm!

  4. Audee
    Member
    Posted 3 months ago #

    @alchymyth big thanks to you!

    The 'if/elseif/else' structures have saved me from major headache to figure out how to keep the post format icon showing up for different layout of categories.

    I couldn't do more with the content.php since my category-custom.php has a very different layout than the category.php

Reply

You must log in to post.

About this Topic