1
votes

I am pretty new in WordPress theme development and I have some doubts about the single-php file that show a single post.

I have created this single.php file starting from my index.php file:

Testo Introduttivo Introduction Text

<!-- SEZIONE IN CUI VENGONO VISUALIZZATI I POST DEL BLOG: -->
<section id="blog-posts">

    <header class="header-sezione">

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

                // Previous/next post navigation.
                //twentyfourteen_post_nav();

                // If comments are open or we have at least one comment, load up the comment template.
                if ( comments_open() || get_comments_number() ) {
                    comments_template();
                }
            endwhile;
        ?>
    </header>

    <!-- Qui viene visualizzato il singolo articolo -->

</section>

<section id="partnerSlide">
    <header class="header-sezione">
        <h2>Partner e Sostenitori</h2>
    </header>

    <div class="row">
        <?php
        // 'My_Widgtet_Area' area, where the id is called:
        if (is_active_sidebar('partner-slide')) : ?>

        <div id="widget-sidebar">
            <ul>
                <?php dynamic_sidebar('partner-slide'); ?>
            </ul>
        </div><!-- #widget-sidebar .widget-area -->

        <?php endif; ?>
    </div>
</section>

So I have take the index.php file and I deleted all the content that I do not want to be shown in the post visualization (So I have kept only the skeleton of my index.php file theme)

Then I have add this code to show my post:

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

                // Previous/next post navigation.
                //twentyfourteen_post_nav();

                // If comments are open or we have at least one comment, load up the comment template.
                if ( comments_open() || get_comments_number() ) {
                    comments_template();
                }
            endwhile;
        ?>

It work correctly but I have some problem to understand what the previous code exactly do:

I know that by this line I am refering on The Loop to my clicked post (the post to show):

while ( have_posts() ) : the_post();

The thing that I can't understand is what exactly do when perform this operation:

get_template_part( 'content', get_post_format() );

Reading on the documentation (http://codex.wordpress.org/Function_Reference/get_template_part) it seems to me to understand that it load a predefinied template into my theme (like a code snippet putted into my theme)

I think that this put the code that print my post on the page (the title, under it the author name and the datem under it the post text and finally the category and the add comment link)

In practice is as am I putting the content.php file in the position where I have declared the previous code?

Tnx

Andrea

Is it true?

1

1 Answers

1
votes

Andrea,

You are correct. This is the equivalent to an "include".

To break this down

get_template_part( 'content', get_post_format() );

get_template_part   // is calling a function to locate the template partial aka include

'content'    // this is the base slug. 
     //  Think of it like a root word as opposed to a suffix.

get_post_format()    //  is saying get the particular content "partial" or "include"

What this is doing? Let's say you're on a post with the format of "Video" (you know on the right hand side when you are creating a post, you can choose the "post format" --)

Standard Audio Aside Chat Gallery Image Link Quote Status Video

Okay, lets assume you've clicked the radio button for "Video". So this post contains a video.

NOW, back to your get_template_part... This will look FIRST for a file called "content-video.php" If you have not created one, then it will fall back to the default file "content.php"

Thats why you have the second item in get_template_part. Because it gives you a sort of "fall-back" in case a specific template it missing, it can then look for the "default" template.

The order it will look for the file is this

If you are using a child theme -- It will look first in your child theme folder / directory content-video.php content.php

Then if it cannot be located there, it will look for the files in the parent theme folder content-video.php content.php

You are using a child theme right?>?>

The additional beauty you can use it anywhere... And even include it conditionally.

For example if you wanted to include an offer to signup for eNews but only if someone is reading a post in a specific category... you could do the following...

if (in_category( '327' )){
    get_template_part('partials/enewssignup');
}

This would reference the file enewssignup.php located in the /partials folder.