Some doubts about how to show posts in a custom theme?

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

3 comments

  1. get_template_part() is including a template file from the active theme directory.

    For example:

    get_template_part('content') will include the template content.php from the theme folder.

    When having two parameters, it will search for a template named firstParameter-secondParameter.php. So:

    get_template_part( 'content', get_post_format() );
    

    will include the content-{$post_format}.php file.

    get_post_format() returns one of this defined formats:

    aside
    chat
    gallery
    link
    image
    quote
    status
    video
    audio 
    

    or false. So the file included will be content.php or content-format.php

  2. 1) Yes!

    2) WordPress supports 9 post formats now, let’s say if your theme support 2 of them: ‘aside’ and ‘gallery’, there should be the following code in your functions.php:

    add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );
    

    You could build different content template part for each post format, and save them as ‘content-aside.php‘ and ‘content-galery.php‘ in your theme folder.

    If you don’t have those 2 files in your theme, get_template_part will automatically fallback to use the ‘content.php’ instead.

  3. All the good WordPressers have already answered your questions, and I think they are successful in eliminating your doubts. So I’m not touching exactly into your question, but to the concepts.

    Before starting theme developing, I think a raw theme can be very good to understand the concept behind each specific zones. I started theme development with my [virtual] teacher Ian Stewart’s tutorial on how to develop a theme:

    Source Code of the entire tutorial is in Google Code Trunk. I’d suggest you to not just follow the tutorial, but to read the comments too. 🙂

    get_template_part() is a theme function – not necessarily it loads only the post part – rather it can be anything. The code part (a portion of the code) you want to separate from the main file, in PHP we include that using include() function, but in WordPress, we do it using get_template_part().

    The code you mentioned, separated the template part where the code to show the post resides. You can comment out that portion and can try the following to understand the basic:

    <?php
    if ( have_posts() ) :
    while ( have_posts() ) : the_post();
    //get_template_part( 'content', get_post_format() ); ?>
       <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
          <h2 class="post-title"><?php the_title(); ?></h2>
          <div class="post-content"><?php the_content(); ?></div>
       </article>
    <?php
    endwhile;
    endif;
    ?>
    

    Sorry, I’s not actually with your question, but I tried to pass the concept, and I believe CONCEPT WITH DIFFERENT PERCEPTIONS IS THE KEY TO KNOWLEDGE.

    EDIT

    You may also start with the _s starter theme. Get it directly from GitHub:

Comments are closed.