Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Maybe I simply do not understand what get_template_part() is doing …

I have a file called event-list.php that should work as template for other pages and files so all my events (a custom post type) get listed!

Inside this event-list.php I have this …

<?php
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
    $wr_event_fields = get_post_custom();
    $event_date_timestamp = $wr_event_fields['_wr_event_date'][0];
    $event_date = strftime('%d.%m.%Y', $event_date_timestamp);
    $event_time = $wr_event_fields['_wr_event_time'][0];
    $event_speaker = $wr_event_fields['_wr_event_speaker'][0];  
    //get_template_part( 'event-item' );
?>
    <!-- event-item.php -->
    <div id="event-<?php the_ID(); ?>" <?php post_class(); ?>>

        <div class="event-date"><?php echo $event_date; ?></div>
        <div class="event_time"><?php echo $event_time; ?></div>
        <div class="event-speaker"><?php echo $event_speaker; ?></div>
        <div class="event-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
        <div class="event-description-excerpt"><?php the_excerpt(); ?></div>

    </div>
    <!-- event-item.php -->

<?php endwhile; ?>

So what I want to do is include another template inside of event-list.php named event-item.php which only holds the layout of an event. I want to use this event-item.php maybe later also in different loops. It should just be the markup for each single event-item on my website. If I make a change inside of this even-item.php template all "events" in any of my loops on my entire website should change.

The problem I have with this is the one in the above sample code. I use get_template_part( 'event-item' ); to include the event-item.php layout structure within my loop. And I get its custom post-meta outside of this template. However that does not work!

As you can see in my sample above I have to copy the entire layout structure from my event-item.php inside of my event-list.php so the post-meta stuff is filled in.

Any ideas what I don't get here or what I'm doing wrong here?

Thank you in advance.

Matt

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

Try globalizing $post inside of event-item.php.

Also: be sure to call wp_reset_postdata() after you close your $loop while loop.

e.g.:

<!-- event-item.php -->
<?php 
// globalize $post
global $post; 
?>
<div id="event-<?php the_ID(); ?>" <?php post_class(); ?>>

    <div class="event-date"><?php echo $event_date; ?></div>
    <div class="event_time"><?php echo $event_time; ?></div>
    <div class="event-speaker"><?php echo $event_speaker; ?></div>
    <div class="event-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
    <div class="event-description-excerpt"><?php the_excerpt(); ?></div>

</div>
<!-- event-item.php -->

and then:

<?php 
endwhile; 

// Restore $post global to the primary query
wp_reset_postdata();
?>

Edit

Assuming the problem is just the post meta data, I would suggest moving the post meta data variables inside loop-item.php. Only a guess, but perhaps your local variables aren't getting passed through the include() function that is part of get_template_part().

So, like this:

<!-- event-item.php -->
<?php
// Define these here, inside loop-item.php
$wr_event_fields = get_post_custom();
$event_date_timestamp = $wr_event_fields['_wr_event_date'][0];
$event_date = strftime('%d.%m.%Y', $event_date_timestamp);
$event_time = $wr_event_fields['_wr_event_time'][0];
$event_speaker = $wr_event_fields['_wr_event_speaker'][0]; 
?> 
<div id="event-<?php the_ID(); ?>" <?php post_class(); ?>>

    <div class="event-date"><?php echo $event_date; ?></div>
    <div class="event_time"><?php echo $event_time; ?></div>
    <div class="event-speaker"><?php echo $event_speaker; ?></div>
    <div class="event-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
    <div class="event-description-excerpt"><?php the_excerpt(); ?></div>

</div>
<!-- event-item.php -->

Also, to avoid undefined variable notices, you should define your variables using isset() conditionals; e.g. change this:

$event_date_timestamp = $wr_event_fields['_wr_event_date'][0];

...to this:

$event_date_timestamp = ( isset( $wr_event_fields['_wr_event_date'][0] ) ? $wr_event_fields['_wr_event_date'][0] : false );
share|improve this answer
    
Thank you, sounds good, but unfortunately doesn't work for me … any other thoughts? Wordpress in debug mode always says … Notice: Undefined variable: event_date in /Users/m2/htdocs/mysite.com/wp-content/themes/blank/event-item.php on line 11 –  mathiregister Apr 17 '12 at 15:31
    
First things first: do you get any output for the_ID() and/or post_class() prior to that $event_date undefined variable? If you are, then the problem is with your local variables; if you're not, then there's something else wrong. –  Chip Bennett Apr 17 '12 at 15:35
    
See updated answer, that assumes the problem lies only with your post custom meta data variables. –  Chip Bennett Apr 17 '12 at 15:58
    
thank you, that did the job! –  mathiregister Apr 17 '12 at 16:10
add comment

Instead of doing:

get_template_part( 'event-item' );

do this instead:

get_template_part( 'event','item' );

get_template_part expects the filename will be: parameter1-parameter2.php

Also make sure that you declare the variables you want to use as globals, else they will be out of scope and not be shown:

global $event_date, $event_time, $event_speaker;
share|improve this answer
    
As I read the OP, it's not about how to include a file via get_template_part(), but rather, why the global $post object/data aren't available to template tags, such as the_ID(), called within the included template-part file. –  Chip Bennett Apr 17 '12 at 14:47
    
But the_ID() calls the global $post object already. Hm. –  toscho Apr 17 '12 at 14:49
    
@chip Exactly … @toscho get_template_part( 'event','item' ); does load the same file (event-item.php) but doesn't change the fact that the post-meta is not loaded inside the template. –  mathiregister Apr 17 '12 at 15:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.