Viewing 15 replies - 1 through 15 (of 16 total)
  • Plugin Contributor fireproofsocks

    (@fireproofsocks)

    If you’ve got an image field and you’re printing it out in your theme, just add the stuff you want directly in the image tag: no magic wand required.

    <img src="<?php print_custom_field('my_image:to_image_src'); ?>" alt="The sky is the limit" title="You heart's desire" />

    If you need to retrieve data about the image itself, then the “get_post” output filter (https://code.google.com/p/wordpress-custom-content-type-manager/wiki/get_post_OutputFilter) can help you here because it retrieves ALL of a post’s attributes (remember, images are just a different kind of post).

    Also see example 5 from https://code.google.com/p/wordpress-custom-content-type-manager/wiki/Image:

    <?php $img = get_custom_field('my_image:get_post'); ?>
    <img src="<?php print $img['guid']; ?>" title="<?php print $img['post_title']; ?>" />
    Thread Starter blufragola

    (@blufragola)

    Hello.
    Thank you for your suggestion.
    Unfortunately, I can’t figure out how to combine Example 4 (Retrieving Multiple Images) with Example 5 (All an Image’s Data): I made many tests but I don’t get how to write the correct code to make it work. ??
    Besides, I wonder if it is the correct way to get “alt” and “title” values from the image files as stored in Media library.

    I need to build a lightbox gallery with multiple images uploaded by a repeatable image custom field. I put the code in the template, and I’m trying to create with the correct code, the following scenario:

    <a href="path_to_original_img" rel="lightbox_or_whatever" title="title_from_media_library_img_details_for_lightbox_caption">
    <img src="path_to_thumbnail_or_medium_image"  width="width_of_thumbnail_or_medium_image" height="height_of_thumbnail_or_medium_image" alt="alt_from_media_library_img_details"/>
    </a>

    I thought also about putting 2 custom text fields after the one for the image, one for the text I want to insert as “title”, and one for the one I want to use as “alt”. It would be the PERFECT solution for me, because I’ll also have to translate these data, BUT
    I’d need this group of 3 custom fields to be repeatable, all together, and I haven’t got if it’s possible or not.

    I’ll go on to look for a solution by myself but I’ll appreciate if someone could help me.
    Anyway, thank you.

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    The way WP stores some of the image meta data is deeply obscured: it’s serialized in WP’s own private custom field for the image. It’s trickier — if you are comfortable with PHP, then go right ahead, but otherwise, you’ll probably not be able to retrieve it easily.

    Re doing this for multiple images, it’s in the FAQ: https://code.google.com/p/wordpress-custom-content-type-manager/wiki/FAQ

    But for your benefit, now there’s an example 6:
    https://code.google.com/p/wordpress-custom-content-type-manager/wiki/Image

    The whole thing boils down to this: you have an integer representing a post. How do you get that post’s data? There are thousands of ways to do this. An image is just another type of post in WordPress. Retrive the post’s data and format it however you’d like. Wash, rinse, repeat.

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    FYI, you can get the image’s alt text, but WP stores it in a hidden custom field named “_wp_attachment_image_alt” — you might be able to use WP’s get_post_meta() function to get it.

    I’m going to either create a new output filter to dumb this down or modify the existing filters to accomodate array data to make this easier. I just really didn’t expect this stuff to pose as many difficulties as it apparently has… I think the average MODX user is much more savvy than the average WP user.

    Thread Starter blufragola

    (@blufragola)

    You’re the Best! Thank you for the example and your suggestions.

    I want to share my code (it could be useful for someone else):

    <?php
    $array_of_images = get_custom_field('imgs_field:to_array');
    if($array_of_images[0]) {
    for ($i=0;$i<count($array_of_images);$i++){
    $img_orig = CCTM::filter($array_of_images[$i], 'to_image_src');
    $img_small = wp_get_attachment_image($array_of_images[$i], 'thumbnail');
    }
    ?>
    <div class="img_single">
    <a href="<?php print $img_orig; ?>" rel="lightbox_or_whatever" title="">
    <?php print $img_small; ?>
    </a>
    </div>

    imgs_field = is the name of the repeatable custom field for images
    $img_orig = it returns the path for the original uploaded image
    $img_small = it returns something like this:
    <img class="attachment-thumbnail" width="[the value of thumb width]" height="[the value of thumb height]" title="[the title of the image as written in Media library]" alt="[the alternative text as written in Media library]" src="[the path of the thumbnail]">

    Now I can organize my gallery, and set it up for lightbox, but I still have a problem: I’m trying to “extract” the title value from wp_get_attachment_image(), because I need to put it as an attribute of the <a> tag, not <img> (for lightbox).

    I’ve found something about wp_get_attachment_image_attributes() but I still don’t know how to use it and if it could be useful for my purposes.

    Thank you again for your help.

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    Glad you made someIf you download the current dev branch (0.9.5.6), it includes a “gallery” filter that helps streamline this, if you’re interested. I’ll be putting up some docs for this — it grabs the alt text — that’s trickier because WP hides the custom field where it stores that data. Re getting title, you could use WP’s get_post()

    FYI: never count your array in a for loop like that: it’s one of those PHP no-no’s because it can cripple your code. Consider using a foreach loop instead:

    <?php
    $array_of_images = get_custom_field('imgs_field:to_array');
    if(!empty($array_of_images) {
    	foreach($array_of_images as $img) {
    // ... etc
            }
    }
    Thread Starter blufragola

    (@blufragola)

    First of all, thank you.

    About the “for loop”, I’m using it because I have to separate the first item from the others (I haven’t pasted the whole code because it is very long): only the first one must be “medium” and not “thumbnail”, so I can identify it as $array_of_images[0]. I don’t know how to do this with the “foreach loop”. Is there a way? ??

    I look forward to reading your docs about “galley filter”. It seems to be very interesting and useful for my purposes.

    Thread Starter blufragola

    (@blufragola)

    Another (silly) question: what is the best way to upgrade to 0.9.5.6 version without messing up?
    Thank you

    Thread Starter blufragola

    (@blufragola)

    For now I have tried to change my code using foreach and array_slice() to separate the output for the first item of the array from that for the others. I’m not very satisfied about the result, because I’ve had to repeat a portion of the code, but for now it’s a little step forward.

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    You could use array_shift. The problem isn’t the for loop, it’s putting the count statement IN the for loop: that makes the array get counted each time.

    To update to the dev version, unzip the package and replace the “custom-content-type-manager” folder inside your wp-content/includes/plugins folder.

    Thread Starter blufragola

    (@blufragola)

    Got it! I used array_shift and then foreach.

    I’ve updated CCTM…now I wait for your documentation about the new “gallery” filter.

    Thread Starter blufragola

    (@blufragola)

    Hello again.
    I read your instruction about gallery filter.
    I tried to filter my custom field imgs_field this way:

    <?php
    $gallery_of_images = get_custom_field('imgs_field:gallery;');
    print_custom_field($gallery_of_images, '<img height="[+height+]" width="[+width+]" src="[+guid+]" title="[+post_title+]" alt="[+alt+]" class="cctm_image" id="cctm_image_[+i+]"/>');
    ?>

    I don’t know where I’m doing wrong but the output says:
    The ["387","388"] field is not defined as a custom field.

    Probably I’ve misunderstood how to use the code. ??

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    No, you can just do this:

    <?php
    print_custom_field('imgs_field:gallery', '<img height="[+height+]" width="[+width+]" src="[+guid+]" title="[+post_title+]" alt="[+alt+]" class="cctm_image" id="cctm_image_[+i+]"/>');
    ?>

    Do you understand? The gallery filter accepts one argument: a formatting template. But you had several errors: 1. you used a filter named ‘gallery;’ — see the semicolon? there is no such filter.

    Even without that code, what you did would work if you did it this way:

    <?php
    $gallery_of_images = get_custom_field('imgs_field:gallery');
    print $gallery_of_images;
    ?>

    See? You should use EITHER get_custom_field() OR print_custom_field(), not both. One prints the value, the other reads it into a variable. And BOTH of those functions take the first argument as the name of your field. Can you see how what you did there makes no sense?

    Thread Starter blufragola

    (@blufragola)

    Oh, what a mess! ?? …my fault.

    It’s difficult to me to put together filters: if I use your gallery filter I don’t know how to show the medium-sized image for the first of the gallery, and the thumbnail for the others; if I use the to_array filter, to iterate and show different size for the first image and the others, I don’t know how to use the gallery filter in the foreach loop to extract [+alt+], [+post_title+] and so on (width and height of thumbnails or medium images, for example).

    I know and I’m not so skilled with all this stuff…sorry! ??

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    You’re really in customization land, my friend, and WordPress is not friendly to people who end up there, unfortunately. I think the best approach would be to use the “to_array” filter, then try using various WP functions to get exactly what you need.

    The to_image_tag filter does accept the thumbnail arguments etc.:
    https://code.google.com/p/wordpress-custom-content-type-manager/wiki/to_image_tag_OutputFilter

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘[Plugin: Custom Content Type Manager] images – customize alt and title’ is closed to new replies.