0
  1. I created a Custom Field ('custom_name') to allow content editors to specify a 'custom_name' (different from the title of the page) to any post created:

    • custom_name - (Field type --> 'text')
  2. For a specific page I created 4 Custom Fields that lets the content editor pick 4 articles he would like to feature:

    • featured_content_1 (Field type --> 'Post Object')
    • featured_content_2
    • featured_content_3
    • featured_content_4
    • promoted

On that page I display the featured content as a list of thumbnails:

<?php
    for ($i=1; $i<=4; $i++) {
        $fieldName = 'featured_content_' . $i;
        $featuredContentPostID = get_field($fieldName)->ID;
        $selectedFeaturedContent = get_post( $featuredContentPostID );
        $promoted_content = get_field('promoted');
        ?>
        <li class="<?php if($promoted_content == $fieldName) { echo 'active'; }?>">
                <a href="<?php echo esc_url( get_permalink($featuredContentPostID) ); ?>">
                        <?php echo get_the_post_thumbnail( $featuredContentPostID, 'thumbnail' ); ?>
                        <span><?php echo $selectedFeaturedContent->post_title ?></span>
                </a>
        </li>
        <?php
    }
?>

I manage to query and display the 'artist_name' in a different loop:

<?php   
    $artistNameArgs = array(
        'meta_key' => 'artist_name'
    );
    $artistName = new WP_Query( $artistNameArgs );
?>

<ul>
<?php if( $artistName->have_posts() ): ?>
    <?php while ( $artistName->have_posts() ) : $artistName->the_post(); ?>
        <li><?php the_field('artist_name'); ?></li>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>       
<?php endif; ?>
</ul> 

How can I combine the two so the 'artist_name' appears in the LI of the thumbnails LIs ?

Thanks

UPDATE

I managed to make it work. Here is my updated code:

<?php
        for ($i=1; $i<=4; $i++) {
            $fieldName = 'featured_content_' . $i;
            $featuredContents = get_field($fieldName);
            $featuredContentPostID = get_field($fieldName)->ID;
            $selectedFeaturedContent = get_post( $featuredContentPostID );
            $promoted_content = get_field('promoted');
            ?>
            <li class="<?php if($promoted_content == $fieldName) { echo 'active'; }?>">
                    <a href="<?php echo esc_url( get_permalink($featuredContentPostID) ); ?>">
                            <?php echo get_the_post_thumbnail( $featuredContentPostID, 'thumbnail' ); ?>
                            <span>
                                <?php
                                    foreach($featuredContents as $featuredContent){
                                        the_field('content_name', $featuredContent);
                                    }
                                ?>
                            </span>
                    </a>
            </li>
            <?php
        }
?>

1 Answer 1

0

Your question literally doesn't quite sound like "combining" loops, that's usually different thing in WP terminology.

Following it literally you already know the part that outputs artist name you want — the_field('artist_name').

What probably confuses it in first context is that it's not really a normal WP loop. Note how that call doesn't specify which post artist name should be for? That conventionally comes from global post if omitted. You are not setting up post globals at all in your first snippet.

Since the_field() isn't even WP function I assume you use one of numerous custom fields frameworks. You will have to consult its documentation how to retrieve the data for a specific post, provided by ID explicitly.

2
  • For the Custom Fields I use the Advanced Custom Fields plugin
    – John
    Oct 9, 2015 at 10:34
  • Hi Rarst, I updated my question with the latest I've done so far
    – John
    Oct 12, 2015 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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