WordPress custom fields - Function

I'd love any ideas.

This is what i have inside the loop

    <?php
    $mediapost = get_post_meta($post->ID, 'mediapost', true);

    if ($mediapost == 'gallery') {
        $posticon = '<i class="fa fa-camera-retro"></i>&nbsp;';
    } elseif ($mediapost == 'video') {
        $posticon = '<i class="fa fa-video-camera"></i>&nbsp;';
    } else {
        $posticon = '';
    }
    ?>

    <?php echo $posticon; ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf( esc_attr__( 'Permalink to %s', 'citydesk' ), the_title_attribute( 'echo=0' ) ); ?>" ><?php the_title(); ?></a>

I have around 5 loops and same code keeps repeating one after another.

Is there a way to shorten this (maybe to wrap it in a function) and use it that way?

Solutions

You should consider using wordpress post formats:

It implements this interface in posts admin area:

Post Formats

Also a few functions to know what kind of post is it: has_post_format( '[type]' )

So you can create a function in you function.php file:

function the_post_icon(){
    $format = get_post_format();
    switch($format){
        case 'gallery':
            $posticon = 'gallery';
            break;
        case 'video':
            $posticon = 'video';
            break;      
        default:
            $posticon = '';
            break;
     }
     echo '<i class="fa fa-'.$posticon.'"></i>&nbsp;';
}

or even

function the_post_icon(){
         echo '<i class="fa fa-'.get_post_format().'"></i>&nbsp;';
    }

You could use a switch though with only the three options it doesn't save you any code:

switch(type){
    case 'gallery':
        $posticon = '<i class="fa fa-camera-retro"></i>&nbsp;';
        break;
    case 'video':
        $posticon = '<i class="fa fa-video-camera"></i>&nbsp;';
        break;
    default:
        $posticon = '';
        break;  
}

Similar questions

Advanced Custom Fields - Disable Users to Edit Custom Fields
I don't really know how to explain this, so this will most probably not make much sense. Basically, I made a custom field (drop-down selection) for the Add/Edit User page, and I want to be the only one who can edit this. Like... when users go to the Edit User page, they can change the value of this field, which I don't want them to be able to do. I...
Advanced Custom Fields - Get custom fields from parent page
I'm using Advanced Custom Fields making it easier for my client to manage his content. On all of my child- and parent pages i would like to have the same header image and sidebar information. Allthough i wish to make "Parent Page #1" and "Parent Page #2" have different information and this is controlled with Advanced Custom Fields by the Client. Ri...
Transfer self made functions.php custom fields to Advanced Custom Fields
I have recently taken over a wordpress site that needs some serious renovating. The previous developer created custom fields himself in functions.php. I am serious advocate of Advanced Custom Fields, the power of this plugin far outweighs that of self coded custom fields. My plan is to try and find a way off transferring the data from the current c...
Using "Advanced Custom Fields" checkbox fields in custom queries
I’m trying to write a WP_Query which uses some data from two ACF checkboxes as part of the arguments. I found the documentation showing how to use fields in custom queries however I cannot work out what the correct syntax for my checkboxes is. My ACF’s: This is the query I have: I guess what I can’t figure out is why data is actually needed for met...
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...
Including ACF (Advanced custom fields) in a custom theme or plugin: Exporting fields on distribution
I am working on a custom theme and have included acf pro inside that theme as mentioned in the docs. The theme is working fine and acf is activated on theme activation. Here is the code. The issue i am facing for couple of hours now is due to the groups/fields that i want to be instantiated inside acf. I have some field groups that i want to be sho...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.