• Resolved Giray

    (@giray)


    My need: create an academic reference which is the result of multiple inputs which are all output on a single line with different styles per entry. Imagine that each of these words is a different field which are then output as follows:
    FLOWER, 1978, George Orwell, Canada.

    Is this something I can do with Carbon Fields?

    Thanks

    • This topic was modified 7 years, 5 months ago by Giray.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Atanas Angelov

    (@atanasangelovdev)

    Hi @giray,

    Yes, this is something you can do with Carbon Fields.
    If you have a specific list of fields that will never change you can use the following snippets:

    Add this to the top of your theme’s functions.php file:

    
    <?php
    use Carbon_Fields\Field;
    use Carbon_Fields\Container;
    
    add_action( 'carbon_register_fields', 'crb_attach_post_meta' );
    function crb_attach_post_meta() {
        Container::make( 'post_meta', __( 'Post Options', 'crb' ) )
            ->show_on_post_type( 'post' )
            ->add_fields( array(
                Field::make( 'separator', 'crb_reference_separator', 'Reference' ),
                Field::make( 'text', 'crb_reference_title', 'Title' )
                    ->set_width( 25 ),
                Field::make( 'text', 'crb_reference_year', 'Year' )
                    ->set_width( 25 ),
                Field::make( 'text', 'crb_reference_name', 'Name' )
                    ->set_width( 25 ),
                Field::make( 'text', 'crb_reference_country', 'Country' )
                    ->set_width( 25 ),
            ) );
    }
    

    Add this to your post loop where you wish to output the combined reference:

    
    <?php
    $reference = array(
        carbon_get_the_post_meta( 'crb_reference_title' ),
        '<strong>' . carbon_get_the_post_meta( 'crb_reference_year' ) . '</strong>',
        carbon_get_the_post_meta( 'crb_reference_name' ),
        '<strong><em>' . carbon_get_the_post_meta( 'crb_reference_country' ) . '</em></strong>',
    );
    echo '<p>' . implode( ', ', $reference ) . '.</p>';
    ?>
    

    Alternatively, if you wish to have multiple references per post you can wrap the 4 fields in a complex field. If this is the case use these snippets instead:

    Add this to the top of your theme’s functions.php file:

    
    <?php
    use Carbon_Fields\Field;
    use Carbon_Fields\Container;
    
    add_action( 'carbon_register_fields', 'crb_attach_post_meta' );
    function crb_attach_post_meta() {
        Container::make( 'post_meta', __( 'Post Options', 'crb' ) )
            ->show_on_post_type( 'post' )
            ->add_fields( array(
                Field::make( 'complex', 'crb_references', 'References' )
                    ->add_fields( array(
                        Field::make( 'text', 'title', 'Title' )
                            ->set_width( 25 ),
                        Field::make( 'text', 'year', 'Year' )
                            ->set_width( 25 ),
                        Field::make( 'text', 'name', 'Name' )
                            ->set_width( 25 ),
                        Field::make( 'text', 'country', 'Country' )
                            ->set_width( 25 ),
                    ) )
            ) );
    }
    

    Add this to your post loop where you wish to output the combined reference:

    
    <?php
    $references = carbon_get_the_post_meta( 'crb_references', 'complex' );
    foreach ( $references as $reference_values ) {
        $reference = array(
            $reference_values['title'],
            '<strong>' . $reference_values['year'] . '</strong>',
            $reference_values['name'],
            '<strong><em>' . $reference_values['country'] . '</em></strong>',
        );
        echo '<p>' . implode( ', ', $reference ) . '.</p>';
    }
    ?>
    
    Thread Starter Giray

    (@giray)

    Thanks Atanas. I’ll give it a try!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Multistyle complex(?) field’ is closed to new replies.