• Resolved stardeuche

    (@stardeuche)


    hi,

    i want to build a slider with complex fields of carbon fields. my code is here :

    Container::make('post_meta', 'Slider Reference decoration')
        ->set_context('normal')
        ->set_priority('high')
        ->show_on_post_type('decoration')
        ->add_fields(array(
            Field::make('complex', 'crb_slider_references_deco')->add_fields(array(
                Field::make("image", "Image")->set_value_type('url')->help_text('Pour la bonne tenue du slider choisir une image aux dimensions suivantes : 1920px X 700px')
            ))
        ));

    in my template, i’m using the code in the documentation of the plugin :

    <?php
    $slidesDeco = carbon_get_post_meta(get_the_ID(),'crb_slider_references_deco', 'complex');				if ( $slidesDeco ) {										    foreach ($slidesDeco as $slide) { ?>
    											<div><img src="<?php echo $slide['image'] ?>" alt="slider"></div>
    
    <?php
    																									 }
    }
    ?>

    i want to specify a size for my images in the slider. in my code, the variable $slide must have an argument for put custom size (1920px x 700px)

    i try with wp_get_attachment_src but it doesn’t work. i don’t understand

    $slidesDeco = carbon_get_post_meta(get_the_ID(),'crb_slider_references_deco', 'complex');
    $images = wp_get_attachment_image_src( $slidesDeco, array( 1920, 700 ) );
    
    foreach ($images as $image) { ?>
    											...slider...
    
    <?php
    }
    ?>

    but the function wp_get_attachment_image_src return a false response. i think the first argument it’s an array($slidesDeco) and
    for the operation of the function it is not possible.

    have you an idea for my problem

    thanks so lot

    https://ww.wp.xz.cn/plugins/carbon-fields/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi Dewy, i have just done it too.
    personally i prefer to define an image size in my functions and choose this image size
    in this case don’t choose set_value_type(‘url’)
    then you can use wp_get_attachement_image_url($id,$image_size)
    and you can have another cf for the image size
    i think it also work with wp_get_attachement_image + it displays also the height and width
    i have not used it because i was needing an additional class

    Hope it helps

    Thread Starter stardeuche

    (@stardeuche)

    Hi alexadark,

    it’s a good idea. i use your idea and i found a function very useful forv me.

    for my problem, i use a function found on the site of pippin (https://pippinsplugins.com/retrieve-attachment-id-from-image-url/)

    my complex field i can use the url of images and with this function i can retrieve the id of images. then i put the information in function wordpress : wp_get_attachment_image_src with the id and the image size. my code is here :

    <?php
    $slidesDeco = carbon_get_post_meta(get_the_ID(), 'crb_slider_references_deco', 'complex');
    
    if ( $slidesDeco ) {
    										foreach ($slidesDeco as $slide) { 
    
    //url of images of the slider										$image_url = $slide['image'];
    																//retrieve ID of the images with pippin function
    $image_id = pippin_get_image_id($image_url);
    																				//redim images of slider								$image_redim = wp_get_attachment_image_src($image_id, 'panoramix'); ?>
    											<div><img src="<?php echo $image_redim[0] ?>" alt="slider"></div>
    
    <?php																			}
    }
    ?>

    thanks so lot alexadark for your help

    ok, but why do you want to retreive the ID, when carbon fields give you the id by default with his image cf.
    it would be simpler to not have the url option (unless you want only the url)
    and get the url with : wp_get_attachement_image_url
    that is in case you need to have classes to the image like me
    if not you can use wp_get_attachement_image and it outputs all the img code (try it, i have tried all these functions to see what they where doing, i use phpstorm, so when i begin to write theme all it list them all)

    I have also found this very useful function (which is not in wp, so i add it to my helper functions)

    /**
     * Retrieve parameters from attachments
     *
     * @since 1.0.0
     *
     * @param $attachment_id
     *
     * @return array
     */
    function wp_get_attachment( $attachment_id ) {
    
    	$attachment = get_post( $attachment_id );
    
    	return array(
    		'alt'         => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
    		'caption'     => $attachment->post_excerpt,
    		'description' => $attachment->post_content,
    		'href'         => get_permalink( $attachment->ID ),
    		'src'         => $attachment->guid,
    		'title'       => $attachment->post_title
    	);
    }

    in case you need the title, the caption etc…
    the ‘src’ is the url

    Thread Starter stardeuche

    (@stardeuche)

    hi

    when i’m deleting the url option of complex field. i can’t use the ID of images. I don’t retrieve this value…

    but i try again with your procedure

    thanks

    it’s strange because normally the id is the value which is retrieved by default…
    look here : https://carbonfields.net/docs/fields-image/
    it says :” set_value_type($value_type)

    Set the type of the stored value. (defaults to id)

    You can also url to store the URL of the image instead of the ID.”

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘dimension images for complex fields’ is closed to new replies.