Advertisement
HarleyCreative

Wordpress: Content from Custom Post Type within a Post

Apr 1st, 2014
1,517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.06 KB | None | 0 0
  1. // Get the contents of the custom field 'post-band-name', create variable $thebandname
  2. <?php $thebandname = get_field('post-band-name'); ?>
  3.  
  4. // Display only if the custom field 'post-band-name' has something in it (a value)
  5. <?php if( get_field('post-band-name') ): ?>
  6.  
  7.     // Query only the post we specified
  8.     <?php $the_query = new WP_Query( array(
  9.         'post_type' => 'band', // The name of the Custom Post Type. (This is needed if you want to retrieve ACF fields.)
  10.         'name' => $thebandname // Retrieve the variable which has our post name
  11.     ) ); ?>
  12.    
  13.     // While there are posts to show, show the post
  14.     <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
  15.  
  16.         // ----- INSERT POST CONTENT HERE! -----
  17.         <div class="artistinfoblock">
  18.             <h2 class="artist-name"><a href="<?php the_permalink(); ?>"><?php the_field('artist-name'); ?></a></h2>
  19.             <h3 class="artist-location"><?php the_field('artist-location'); ?></h3>
  20.         </div>
  21.  
  22.     <?php endwhile; ?>
  23.     <?php wp_reset_postdata(); ?> // Important because we are using a loop within a loop.
  24. <?php endif; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement