Custom Content Type Manager
Display Custom Field Label (7 posts)

  1. manthono
    Posted 3 years ago #

    I love this plugin due to the simplicity and it's powerful too.

    I made whole bunch of custom fields for a certain kind of post type. I display them using conditional div, only display the div containing the custom field value if the value is exist.

    Right now, I do it manually using this code:

    <?php $konten = get_post_meta($post->ID, 'objective', true);
    if($konten != '') { ?>
    <div class="field_label">Objective:</div>
    <div class="field_value"> <?php echo nl2br($konten); ?> </div>
    <?php } ?>

    I have almost zero PHP skill. That code I stole somewehere on the internet :-)
    My question is, since I have like 20 custom fields, I feel like it's too much if I have to write them one by one. I am sure there's a way to use one PHP function to display all the custom field on the post conditionally.

    I am thinking of using foreach function. Can anybody help me on how to do that, please?
    I get confused because I need to pull the field label too, because on my code above, I type it manually. How do I display the field label automatically?

    Thank you very very much


  2. manthono
    Posted 3 years ago #

    I got the way to show the field label using get_custom_field_meta() function.
    But still figuring out how to use the for each function work :-)

  3. fireproofsocks
    Plugin Contributor

    Posted 3 years ago #

    Using get_custom_field_meta() is probably the best approach. There are ways to loop through all field definitions for a post type, but if you don't have much PHP skill, I wouldn't recommend them.

  4. manthono
    Posted 3 years ago #

    Thanks fireproofsocks,

    Can you recommend some documentations on this that i can read slowly?
    nice name btw :-)

  5. fireproofsocks
    Plugin Contributor

    Posted 3 years ago #

    The docs are here: https://code.google.com/p/wordpress-custom-content-type-manager/wiki/

    If you are trying to show or hide a field and a div depending on whether the field has a value, you should look at the "wrapper" output filter: https://code.google.com/p/wordpress-custom-content-type-manager/wiki/wrapper_OutputFilter

    There are examples there that do exactly what you want.

  6. manthono
    Posted 3 years ago #

    Can you help on this please:

    I want to use three variables bellow to loop all custom fields I have.
    I want to use WP get_post_custom_keys() , get_post_custom_values(), and the CCTM function print_custom_field_meta($key, 'label')

    $key = get_post_custom_keys();
    $b = get_post_custom_values();
    $c = print_custom_field_meta($key, 'label')

    I want to use the get_post_custom_keys() as the $key for the function of print_custom_field_meta()
    to show a loop of

    Label 1 -> value 1
    label 2 -> value 2
    and soon ...

    I can't get it right until now. So desperate :-(

    please help
    Thank you

  7. fireproofsocks
    Plugin Contributor

    Posted 3 years ago #

    I don't know if that will work.... WordPress relies heavily on global variables, so mixing some of those functions can have unpredictable results. I don't think is so much a question about the CCTM as it is about displaying data in WP templates. I always advise people to start off using the sample templates that the CCTM generates. They may not be pretty, but they should be functional. See CCTM --> Content Types --> View Sample Templates.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic