How to show posts in homepage using WordPress?

I am pretty new in WordPress theme development and I have some doubt about this WP function used to show my post in the homepage:

    <?php
        if ( have_posts() ) :
            // Start the Loop.
            while ( have_posts() ) : the_post();

                /*
                 * Include the post format-specific template for the content. If you want to
                 * use this in a child theme, then include a file called called content-___.php
                 * (where ___ is the post format) and that will be used instead.
                 */
                get_template_part( 'content', get_post_format() );

            endwhile;
            // Previous/next post navigation.
            twentyfourteen_paging_nav();

        else :
            // If no content, include the "No posts found" template.
            get_template_part( 'content', 'none' );

        endif;
    ?>

Intuitively I understood that as long as there are posts these are displayed in the homepage.

Read More

My doubt is related about this line of code:

get_template_part( 'content', get_post_format() );

Reading the documentation it seems to me that:

1) get_template_part: Load a template part into a template. So I think that by this line I am including a template part used to show a post (the structure of a post into my homepage), is it correct?

2) What exactly do get_post_format() ?

Related posts

Leave a Reply

4 comments

  1. get_post_format() is to determine if a post has any post format.

    It returns the string value of the current post format type, which is useful in several ways. One of the most powerful is to call different template part files based on post format, e.g.:

    get_template_part( 'entry', get_post_format() )

    Which will include, e.g. entry-aside.php for an aside format, or entry.php for a standard format.

    Source: https://wordpress.stackexchange.com/questions/14257/has-post-format-vs-get-post-format

  2. get_template_part( 'content', get_post_format() );
    yes, in your theme directory there should be a template file content.php, for including it by get_template_part()

    get_post_format()
    Returns the post format of a post. This will usually be called in the the loop, but can be used anywhere if a post ID is provided.
    more

  3. get_template_part() will indeed call for a template to be included, which is a safer version of PHP’s include() as it will silently fail if no template is found.

    It follows WordPress’es slug namespacing for template files.

    {slug}-{name}.php

    Which you’ve defined content as your slug thus it’ll look for:

    content-{name}.php

    Where as, your second function, get_post_format() will call the second part. The post format is defined by the post itself, depending on the metadata applied to the post itself, i.e. PostID 221 is a "quote"

     //get_post_format() returns quote
     get_template_part( 'content', get_post_format() );
     //Finds: content-quote.php
    

    This is great for “Sections” of the page, where your post may include an uploaded Video or an quote from an external author, thus bring in an external format to be used across the site, as get_post_format() docs describes the list of possible post_format()'s:

    • side
    • chat
    • gallery
    • link
    • video
    • etc

    If you actually just wanted to get a straight forward template part for post content, calling:

    the_content();
    

    Will look for a template file called single.php, although if you wanted to author your own bespoke content-* template files:

    //Calls content-customtemplate.php 
    get_template_part( 'content', 'customtemplate' ); 
    
  4. if you want to display specific category wise post then you can use this code

    <?php 
                $ID = '6'; $NUMBEROFPOSTS = '5'; $catposts = get_posts('category='.$ID."&order=DESC&numberposts=".$NUMBEROFPOSTS);
                $cnt2 = 0;
                foreach($catposts as $item) :
                    $cnt2++;
                    $headlines .= '<div class="box1">'; 
                    $headlines .= '<div class="num">'.$cnt2.'</div>';
                    $headlines .=  '<p><a href="'.get_permalink( $item->ID ).'">'.$item->post_title.'</a></p>';
                    $headlines .= '</div>';
               endforeach;
               echo $headlines;
                ?>
    

    Where $ID is a category id and $NUMBEROFPOSTS is you want to display number of posts in home page specific category .

    On last echo $ variable name then it will display all the content from category posts.