148 posts
  • Has been part of the Envato Community for over 2 years
  • Located in Czech Republic
  • Sells items exclusively on Envato Market
Itachicz says

Hi everyone,

I have this piece of chode

<?php
if ( has_post_format( 'quote' )) { ?>

    <?php get_template_part('includes/post-formats/quote-format.php'); ?>

<?php
} else if (has_post_format('gallery')) { ?>

    <?php get_template_part('includes/post-formats/gallery', 'format'); ?>

<?php
} else if (has_post_format('video')) {
   // stuff to display the link format post here
} else { ?>

    <?php get_template_part('includes/post-formats/clasic', 'format'); ?>

<?php } ?>

and in each particular template part I’m using $item_width variable. Does anyone know how to pass this variable through these functions?

Thank you very much for all advices and suggestions :)

1509 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+2 more
OriginalEXE says

There is no way, either have your variables globally accessible, or use this instead:

include( locate_template( 'template-part.php' ) );
148 posts
  • Has been part of the Envato Community for over 2 years
  • Located in Czech Republic
  • Sells items exclusively on Envato Market
Itachicz says

There is no way, either have your variables globally accessible, or use this instead:
include( locate_template( 'template-part.php' ) );

Thank you very much for your reply :)

1285 posts
  • Has been part of the Envato Community for over 4 years
  • Has sold $40,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 50+ items on Envato Market
+5 more
Smartik says

I’ve been searching for a solution to send variable in “get_template_part” function and always without luck. I personally don’t like to use global variables because I know, this is just a bad practice. A few months ago I’ve been able to design a solution that pass Theme Check and has support for child themes. I think is a good idea to share with TF authors.
Here it is: https://github.com/Smartik89/SMK-Theme-View/blob/master/functions.php

Use it like this:
smk_get_template_part('filename.php', array(
   'variable_1' => 'Content',
   'other_variable' => 'Other content'
));
And access the variables in filename.php like this(note the $this-> prefix):
echo $this->variable_1; // Output `Content`
echo $this->other_variable; // Output `Other content`

Happy coding.

1 post
  • Has been part of the Envato Community for over 5 years
  • Has collected 1+ items on Envato Market
jachu says

Smartik why dont use include( locate_template( ‘template-part.php’ ) ); ? It is simplest…

7 posts
  • Has been part of the Envato Community for over 2 years
  • Has referred 10+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+2 more
QantumThemes says

Why not adding properties to the global post object?

Parent: <?php global $post; $post->myvar = “hello”; ?>

included file: <?php global $post; echo $post->myvar; //returns “hello” ?>

1285 posts
  • Has been part of the Envato Community for over 4 years
  • Has sold $40,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 50+ items on Envato Market
+5 more
Smartik says

Smartik why dont use include( locate_template( ‘template-part.php’ ) ); ? It is simplest…

Check the code that I’ve shared. I use include and I do also some additional checks to make sure it does not break anything. Also usign direct include is not allowed by ThemeForest because Theme Check will detect it as a warning.

1285 posts
  • Has been part of the Envato Community for over 4 years
  • Has sold $40,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 50+ items on Envato Market
+5 more
Smartik says

Why not adding properties to the global post object?

Parent: <?php global $post; $post->myvar = “hello”; ?>

included file: <?php global $post; echo $post->myvar; //returns “hello” ?>

Because you should not use global variables: http://smartik.ws/2014/07/do-not-use-php-global-variables-never/

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by