how does get template part works in twenty ten theme

23
How Does get_template_part How Does get_template_part How Does get_template_part Works In Twenty Ten Theme How Does get_template_part Works In Twenty Ten Theme By: Rozani Ghani http://rozanighani.com/

Upload: mohd-rozani-abd-ghani

Post on 22-Apr-2015

7.069 views

Category:

Documents


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: How does get template part works in twenty ten theme

How Does get_template_partHow Does get_template_partHow Does get_template_partWorks In Twenty Ten Theme

How Does get_template_partWorks In Twenty Ten Theme

By: Rozani Ghani

http://rozanighani.com/

Page 2: How does get template part works in twenty ten theme

What Exactly GET_template_part is?

• get_template_part is used to load a template part into a template (other than header, part into a template (other than header, sidebar, footer)

• get_template_part is used for child theme development but actually is not just for child theme development only, is there more than theme development only, is there more than that.

Page 3: How does get template part works in twenty ten theme

Let See how GET_template_partis originated?

• We see how get_template_part works.• This example may vary with how developer • This example may vary with how developer

perception but this is just to illustrate how the the get_template_part works.

• I use index.html as the very famous example.• As a clear example, I use the index.php in • As a clear example, I use the index.php in

Twenty Ten Theme.

Page 4: How does get template part works in twenty ten theme

Index.html

• A wordpress index.html mostly consist of these 4 basic template php files: header.php, these 4 basic template php files: header.php, index.php, sidebar.php and also footer.php.

• I visualize these files in order to understand get_template_part is.

Page 5: How does get template part works in twenty ten theme

How Does get_template_partHow Does get_template_partHow Does get_template_partWorks In Twenty Ten Theme

How Does get_template_partWorks In Twenty Ten Theme

By: Rozani Ghani http://rozanighani.com/

Page 6: How does get template part works in twenty ten theme

Header.php

Index.htmlIndex.php Sidebar.php

Footer.php

• We break the index.html intothe basic wordpress templatefiles.

Page 7: How does get template part works in twenty ten theme

Header.php

• We break the index.html into the basic wordpress template files. That is header.php,index.php, sidebar.php and footer.php

Index.htmlIndex.php Sidebar.php

Footer.php

Page 8: How does get template part works in twenty ten theme

Header.php <?php get_header(); ?>

• We break the php into standard WordPress Codex tags.

Index.php Sidebar.php Index.php<?php

get_sidebar();?>

Footer.php <?php get_footer(); ?>

Page 9: How does get template part works in twenty ten theme

• Suppose there is a code to replace index.php that is

How About index.php?<?php get_header(); ?>

replace index.php that is <?php get_index (); ?>

• After search in the WordPress codex, <?phpget_index (); ?> is not exist.

• I prefer not to discuss get_index because

Index.php<?php

get_sidebar();?>

get_index because WordPress already done stuff to make the index.php working smoothly.

<?php get_footer(); ?>

Page 10: How does get template part works in twenty ten theme

Next…

• We going directly to get_template_part.• But first, we going to understand this example.• But first, we going to understand this example.• <?php get_template_part( $slug, $name ); ?>• <?php get_template_part( 'loop', 'index' ); ?>• What the heck are these?

Page 11: How does get template part works in twenty ten theme

What is $slug and $name?

• At the 1st sight, I don’t really understand what $slug and $name are.$slug and $name are.

• But, I take them as these: $slug as $ parent $name as $child

• As I take them like that, it much easier to • As I take them like that, it much easier to understand get_template_part and very much of child theme

Page 12: How does get template part works in twenty ten theme

Recall…

• <?php get_template_part( $slug, $name ); ?>can be written also can be written also is <?php get_template_part( $slug ); ?>

• We going to convert those code such as get_header into get_template_part (‘header’)

Page 13: How does get template part works in twenty ten theme

We going to convert those code such as get_header into get_template_part (‘header’)

<?phpget_template_part(‘header’); ?><?php get_header(); ?>

<?phpget_template_part

(‘index’); ?>

<?phpget_template_part(‘sidebar’)

; ?>

Index.php<?php

get_sidebar();?> ; ?>

<?phpget_template_part(‘footer’); ?><?php get_footer(); ?>

Page 14: How does get template part works in twenty ten theme

We can conclude that the get_template_part works like this.

<?php get_header(); ?>

<?php get_template_part(‘index’); ?> <?php get_sidebar(); ?>

<?php get_footer(); ?>

Page 15: How does get template part works in twenty ten theme

The real index.php of Twenty Ten Theme looks like this.

<?php get_header(); ?>

<div id="container"><div id="content" role="main">

<?php get_template_part( 'loop', 'index' );?><?php get_sidebar(); ?>

</div><!-- #content --></div><!-- #container -->

<?php get_footer(); ?>

Page 16: How does get template part works in twenty ten theme

Index.php of Twenty Ten Theme

• Its look this:

The index.php is using <?php • The index.php is using <?php get_template_part (‘loop’, ‘index’); ?>

Page 17: How does get template part works in twenty ten theme

Why?

• <?php get_template_part (‘loop’, ‘index’); ?> is used to load loop-index.php.is used to load loop-index.php.

• What if loop-index.php not exists?• get_template_part will load loop.php

Page 18: How does get template part works in twenty ten theme

Back to $slug and $name

Coding Technically• Codex:

File Rename Technically• Codex:• Codex:

• <?php get_template_part($slug, $name ); ?>

• Example:• <?php get_template_part(

'loop', 'index' ); ?>

• Codex:• $slug-$name.php

• Example:• loop-index.php

'loop', 'index' ); ?>

• must have “-” between $slug and $name

Page 19: How does get template part works in twenty ten theme

Why $parent and $child?

• To me, it is easier to understand and not much confused.confused.

• Back to loop-index.php, this file already a child php file.

• If <?php get_template_part (‘loop’, ‘index’); ?> try to load loop-index.php but it fail, it will try to load loop-index.php but it fail, it will load her parent php file which is loop.php

• Agree ?

Page 20: How does get template part works in twenty ten theme

How Does get_template_partHow Does get_template_partHow Does get_template_partWorks In Twenty Ten Theme

How Does get_template_partWorks In Twenty Ten Theme

By: Rozani Ghani http://rozanighani.com/

Page 21: How does get template part works in twenty ten theme

Another Example Much Similar

• If you have Twenty Ten Theme, you should the the footer.php file. See this:the footer.php file. See this:

• Much prettier right? But it is for sidebar.• That code will load sidebar-footer.php• That code will load sidebar-footer.php

Page 22: How does get template part works in twenty ten theme

sidebar-footer.php

• <?php get_sidebar( 'footer' );?>• is similar to• is similar to• <?php get_template_part ( 'footer' );?>• Why?• Look the php file which is sidebar-footer.php

• Revise back what you have learn.

Page 23: How does get template part works in twenty ten theme

How Does get_template_partHow Does get_template_partHow Does get_template_partWorks In Twenty Ten Theme

How Does get_template_partWorks In Twenty Ten Theme

By: Rozani Ghani http://rozanighani.com/