ansarob
ansarob

Reputation: 825

Displaying All Data from Custom Post Type Fields through Advanced Custom Fields

I have looked all over Google, trying to figure this out. I've made some progress but still stuck. I'm pretty new to ACF and custom post types. I have a custom post type of Attorneys that I setup through WCK. That post type has a field group with field names of attorney_photo, attorney_name and attorney_areas_of_practice. With the code below, I can get the attorney_name and attorney_areas_of_practice (repeater field) to display, but not the attorney_photo. I have the info displaying correctly on each attorneys specific page, but I need this page to be a listing of all the attorneys. Not sure what I am doing wrong with the image part.

<?php get_header(); ?>

<?php
$args = array(
    'posts_per_page' => 30,
    'order' => 'ASC',
    'orderby' => 'title',
    'post_type' => 'attorneys'
);

query_posts($args);

if ( have_posts() ) : 

?>
<?php while ( have_posts() ) : the_post(); ?>

<div class="attorney-preview">

<?php $photo = get_post_meta($post->ID,'attorney_photo', true); ?>
<img src="<?php echo $photo; ?>" />

<p><strong><a href=""><?php echo get_post_meta($post->ID,'attorney_name', true); ?></a></strong></p>

<ul>
<?php
    while ( have_rows('attorney_areas_of_practice') ) : the_row();
        $attorney_area_of_practice = get_sub_field('attorney_area_of_practice');
        echo "<li>" . $attorney_area_of_practice . "</li>";
    endwhile;   
?>
</ul>

</div><!-- .attorney-preview -->

<?php endwhile; ?>


<?php endif; ?>

<?php
wp_reset_query();  // Restore global post data stomped by the_post().
?>

<?php get_footer(); ?>

Upvotes: 1

Views: 2011

Answers (2)

Abdoon Nur
Abdoon Nur

Reputation: 307

When you add image field through ACF plugin there are some options of return value. For example return value is: image object or return value is: image url, in your case return value might be selected as image object, not image url. That is why here with your code is being returned an array rather than only url. To get only image url from this array, please write as following a bit change:

<?php $photo = get_post_meta($post->ID,'attorney_photo', true); ?>
<img src="<?php echo $photo['url']; ?>" />

Upvotes: 1

Quebrando Cabeca
Quebrando Cabeca

Reputation: 325

Try this

        <?php query_posts(array('posts_per_page' => 30,'order' => 'ASC','orderby' => 'title','post_type' => 'attorneys'));

        if (have_posts()) : while (have_posts()) : the_post(); 
        $attorney_photo = get_post_meta($post->ID, 'attorney_photo', true);      
        $attorney_name = get_post_meta($post->ID, 'attorney_name', true); ?>

       <div class="attorney-preview">
         <img src="<?php echo $attorney_photo; ?>" />

          <p><strong><a href=""><?php echo $attorney_name; ?></a></strong></p>

       <ul>
       <?php
       while ( have_rows('attorney_areas_of_practice') ) : the_row();
       $attorney_area_of_practice = get_sub_field('attorney_area_of_practice');
       echo "<li>" . $attorney_area_of_practice . "</li>";
       endwhile;   
        ?>
     </ul>

    </div><!-- .attorney-preview -->

    <?php endwhile; endif; ?>

    <?php
     wp_reset_query();  // Restore global post data stomped by the_post().
     ?>

Upvotes: 0

Related Questions