0

I created 4 custom fields for a specific page.

Field type:

  • Post Object (I grab thumbnails and titles from the posts)

Field names:

  • featured_content_1
  • featured_content_2
  • featured_content_3
  • featured_content_4

Right now I display the content in a list like this:

<ul>
    <li>
        <?php 
            $featuredContentPostID = get_field('featured_content_1')->ID;
            $selectedFeaturedContent = get_post( $featuredContentPostID );
        ?>
        <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>
    <li>
        <?php 
            $featuredContentPostID = get_field('featured_content_2')->ID;
            $selectedFeaturedContent = get_post( $featuredContentPostID );
        ?>
        <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>
</ul>
  1. How can I loop through the fields and display the content without having to deplicate it?
  2. Is it best practice to name them the same? i.e "featured_content"
  3. One last question - how could I put a flag on one of them from the custom field section so I could promote one of them?

Thanks

UPDATE

I've added a new field (select box) to my set of custom fields :

  • featured_content_1
  • featured_content_2
  • featured_content_3
  • featured_content_4
  • promoted_content

How could I retrieve the value of that Select field ('promoted_content')?

UPDATE 2

Solution to promote content & add an 'active' class to LI:

<?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
    }
?>
1

How can I loop through the fields and display the content without having to deplicate it?

This for loop will cycle through the four field names to avoid duplication.

<?php
    for ($i=1; $i<=4; $i++) {
            $fieldName = 'featured_content_' . $i;
            $featuredContentPostID = get_field($fieldName)->ID;
            $selectedFeaturedContent = get_post( $featuredContentPostID );
            ?>
            <li>
                    <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
    }
?>

Is it best practice to name them the same? i.e "featured_content"

Normally, I think you're better off with more specific names - however if the plugin is adding a more "generic" feature (i.e. allowing the user to feature four arbitrary bits of content like this) then this naming scheme is fine.

One last question - how could I put a flag on one of them from the custom field section so I could promote one of them?

I'd add another custom field, perhaps a drop-down named something like promoted_content_field. There, the user can choose which of the four to promote (or none). Then, in the loop above just add a check to see if the field that's currently being processed is the promoted one chosen in promoted_content_field

  • Hi Aric, thanks for your answer. I made updated to my question. How would you retrieve the select option (promoted_content)? – John Oct 7 '15 at 15:18
  • I believe this should work: get_post('promoted_content'); – Aric Watson Oct 7 '15 at 17:13
  • As suggested, I added a check to see if the field currently processed is the promoted one - if so I add a class 'active' to the LI – John Oct 8 '15 at 11:05
  • Hi Aric, didn't want to hijack this Post so I created another question related to this post. In case you'd have a second to take a look. Thanks again. wordpress.stackexchange.com/questions/204958/… – John Oct 8 '15 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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