简体   繁体   中英

Displaying Multiple Custom Fields of the same name in Post Content

So, working with a new Wordpress theme with a bunch of imported content from a client's existing website and in their old theme they have multiple images attached to their individual portfolio pages.

These images all share the same custom field name: project_photo_photo

The problem I have is, in the new theme, how can I salvage the imported data and utilize the existing custom field name and display the multiple project images for each post within the template of the post loop?

Below is the code example showing how far I've gotten in my attempt to display this content, it only displays one image result, adding multiples of the same block of code only results in displaying a duplicate of the first image:

<?php if ( get_post_meta( get_the_ID(), 'project_photo_photo' ) ) : ?>
            <div class="grid_gallery clearfix">
                <div class="grid_gallery_inner">
                    <figure class="gallery_item featured-thumbnail thumbnail single-gallery-item">
                        <a href="/wp-content/files_mf/<?php echo get_post_meta( get_the_ID(), 'project_photo_photo', true ); ?>" class="image-wrap" rel="prettyPhoto[gallery]">
                            <img class="project_photo_photo" width="260" src="/wp-content/files_mf/<?php echo get_post_meta( get_the_ID(), 'project_photo_photo', true ); ?>" alt="<?php the_title(); ?>" />
                        <span class="zoom-icon"></span>
                        </a>
                    </figure>
                </div>
                <!--END .slider -->
            </div>
        <?php endif; ?>

I feel I'm close, and there has got to be a way to do this gracefully. If anyone could help me out here I would greatly appreciate it.

If there are multiple entries for same custom field, you get an array if you try

get_post_meta( get_the_ID(), 'project_photo_photo' )

but you get only the first value if you use true as last parameter as

get_post_meta( get_the_ID(), 'project_photo_photo', true ) .

You can get more info here : http://codex.wordpress.org/Function_Reference/get_post_meta

That is why you are getting single result everytime.

Also you should just query for the meta field once. you have queried it 3 times which costs DB actions and time. Call once, save in variable and use as much as you want.

You will get an array of values for your photos. You need to loop through to get each of them.

I have modified your code, I hope i have placed the forloop at proper place, if not, place at appropriate place.

<?php  $project_photos = get_post_meta( get_the_ID(), 'project_photo_photo' );
    if ( $project_photos ) : ?>
            <div class="grid_gallery clearfix">
                <?php foreach( $project_photos as $project_photo ) { ?>
                <div class="grid_gallery_inner">
                    <figure class="gallery_item featured-thumbnail thumbnail single-gallery-item">
                        <a href="/wp-content/files_mf/<?php echo $project_photo; ?>" class="image-wrap" rel="prettyPhoto[gallery]">
                            <img class="project_photo_photo" width="260" src="/wp-content/files_mf/<?php echo $project_photo; ?>" alt="<?php the_title(); ?>" />
                        <span class="zoom-icon"></span>
                        </a>
                    </figure>
                </div>
                <?php } ?>
                <!--END .slider -->
            </div>
    <?php endif; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM