WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] HELP: Why are get_template_part and conditional tags breaking my code? (10 posts)

  1. kzoo
    Member
    Posted 4 months ago #

    I have a block of HTML that shows up in several places across my theme...

    <header class="row">
    	<div class="headline"></div>
    	<hr>
    	<div class="subhead"></div>
    </header>

    Instead of repeating myself I decided to drop my code into a file called headline.php and use get_template_part( 'headline' ) to call my code where I need it without being redundant.

    Then I add a statement to update my values according to the page being display.
    Now headline.php looks like so:

    <?php if (is_home()) {
    	$the_post 	= get_post(2);
    	$headline 	= $headline->post_title;
    	$subhead 	= $headline->post_content;
    } elseif (is_single()) {
    	$headline 	= the_title();
    	$subhead 	= the_content();
    } elseif (is_search()) {
    	$headline 	= "here is the headline for search";
    	$subhead 	= "here is the subhead for search";
    }
    else {
    	$headline 	= "here is the default headline";
    	$subhead 	= "here is the default subhead";
    } ?>
    <header class="row">
    	<div class="headline"><?php echo $headline; ?></div>
    	<hr>
    	<div class="subhead"><?php echo $subhead; ?></div>
    </header>

    I assumed this would work. However, when I refresh my page, <div class="headline"></div> and <div class="subhead"></div> are empty.

    Am I missing something? Can someone tell me what I'm doing wrong?

  2. Andrew
    Forum Moderator
    Posted 4 months ago #

    Can you put your entire template code into PasteBin and link us that?
    http://pastebin.com

  3. kzoo
    Member
    Posted 4 months ago #

    headline.php has the template part:
    http://pastebin.com/tBpq0UfQ

    and these two files are where I call get_template_part( 'headline' ):
    http://pastebin.com/5zCJrLDq
    http://pastebin.com/GmaGxJw1

  4. Andrew
    Forum Moderator
    Posted 4 months ago #

    So even if you do this still nothing is echoed: http://pastebin.com/Dn83h0CH ?

  5. kzoo
    Member
    Posted 4 months ago #

    Sorry, no dice.

  6. Andrew
    Forum Moderator
    Posted 4 months ago #

    Are you sure you don't have a caching plugin enabled?

  7. alchymyth
    The Sweeper & Moderator
    Posted 4 months ago #

    what page are you looking at?
    - the index page?
    - a single post?
    - the search results page?
    - a static page?
    - any other like archive?

    is the result empty for all of those?

    review
    https://codex.wordpress.org/Function_Reference/get_post
    http://codex.wordpress.org/Function_Reference/the_title
    http://codex.wordpress.org/Function_Reference/the_content

    after integrating the information from the Codex, try:

    <?php if (is_home()) {
    	$the_post 	= get_post(1);
    	$headline 	= $the_post->post_title;
    	$subhead 	= $the_post->post_content;
    } elseif (is_single()) {
    	the_post();
    	$headline 	= get_the_title();
    	$subhead 	= apply_filters('the_content', get_the_content());
    	rewind_posts();
    } elseif (is_search()) {
    	$headline 	= "here is the headline for search";
    	$subhead 	= "here is the subhead for search";
    }
    else {
    	$headline 	= "here is the default headline";
    	$subhead 	= "here is the default subhead";
    } ?>
    <header class="row">
    	<div class="headline"><?php echo $headline; ?></div>
    	<hr>
    	<div class="subhead"><?php echo $subhead; ?></div>
    </header>
  8. PaulMighty
    Member
    Posted 4 months ago #

    Experienced an issue similar to what OP describes when trying to conditionally include get_template_part. Here's the code running in a page template in the theme root ("modules" directory is also in the theme root):

    if( $some_var = 'a' ) {
    	get_template_part( 'modules/dashboard-a' );
    } elseif( $some_var = 'b' ) {
    	get_template_part( 'modules/dashboard-b' );
    }

    This would result in no output to the page (blank page). Unexpected and difficult to troubleshoot when there's nothing to look at!

    -PaulMighty

  9. kzoo
    Member
    Posted 4 months ago #

    That was it @alchymyth
    You've saved the day as always.

    Also, @PaulMighty you're using get_template_part wrong.
    Here is a great article on the topic.
    http://kovshenin.com/2013/get_template_part/

  10. PaulMighty
    Member
    Posted 4 months ago #

    @kzoo - I read the article you linked to and it says nothing about conditional statements conflicting with get_template_part. Maybe you're suggesting something like this?

    get_template_part( 'modules/dashboard', $some-var );

    -PaulMighty

Reply

You must log in to post.

About this Topic