I created a loop for logged-in-users-only which works perfectly for all data except for meta keys. I'd like to be able to also extract meta keys but had no luck so far.
This is my loop (based on this post):
<?php add_shortcode( 'current-userposts' , 'show_userposts' );
function show_userposts(){
$user_id = get_current_user_id();
$loop = new WP_Query( array( 'post_type' => 'photo-spot', 'author' => $user_id ) );
if ( $loop->have_posts() ) {
?>
<form class="pps-results"><?php
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<fieldset>
<legend class="title-block">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</legend>
<div class="category-block">
<?php $categories = get_the_category();
if ( ! empty( $categories ) ) {
echo esc_html( $categories[0]->name );
}
?>
</div>
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
?><div class="image-block"><?php
the_post_thumbnail( 'thumbnail' );
?></div><?php
}
?>
<div class="address-block">
<?php echo get_post_meta($post->ID, '_cf_address', true); ?>
</div>
</div>
</fieldset>
<div class="clear"></div>
<?php endwhile; ?>
</form>
<?php
}
wp_reset_postdata();
} ?>
The function is called via a shortcode
When I use the exact same loop in a search.php template the custom value ('_cf_address') shows up perfectly but somehow not with the shortcode...
Any idea what might be going wrong?
PS> The preview screen doesn't show my html code - I hope it show up alright.
{}
sign above the editor. This indent each line of code with 4 spaces. Alterbnatively, indent each line with 4 spaces ;-)$post
global, a shortcode should never ever echo output, output should be returned</div></div></fieldset>
has one closing div to much? (has nothing to do with the rest of the code but thought it would be better to mention (only when I am correct ofcourse)).