Support » Plugin: Toolset Types - Custom Post Types, Custom Fields and Taxonomies » Getting the 'alt' attributes on images – Is this right?

  • Moderator Andrew Nevins

    (@anevins)


    Forum moderator

    I’ve got a slideshow and have made an ‘Image’ custom field called “slideshow images” to show up on page with a particular template in the dashboard. It allows multiple values.

    I was just wondering if anyone could point out whether I’m going about this the wrong way?

    Note that I have managed to get the ‘alt’ values from the images that I set in the Media library onto my webpage.

    Here’s what I’m doing:

    I’m getting all of the information about the images in my custom field from the ‘get_children‘ function, but first I have to know (do I?) how many ‘numberposts‘ (images in the Custom Field) there are.

    I followed the ‘get_children‘ function from this thread, which seemed like @klongdesigns had the same issue as me.

    global $post;
    
    //Get all the images out of the Custom Field
    $images = types_render_field( 'slideshow-image', array( 'raw' => 'true' ) );
    
    //Put them in an array
    $images = explode( ' ', $images );
    
    //So that we can count them and then use that number as the 'numberposts' argument later on
    $counted_images = count( $images );
    
    //Arguments to retrieve all images from the Custom Field
    $args = array(
    	'numberposts' => $counted_images,
    	'order'=> 'ASC',
    	'post_mime_type' => 'image',
    	'post_parent' => $post->ID,
    	'post_type' => 'attachment'
    );
    
    //Get all the gallery images under the Custom Field
    //This way you'll have the image alt attribute value
    $gallery_images = get_children( $args );

    To me, doing the part where I get all of the images in the Custom Field just to count them and put that counted number into the ‘numberposts‘, ‘get_children‘ argument seems elongated. It seems like I’m going about this the wrong way.

    Then I just use ‘$gallery_images‘ to spit out the data;

    ?>
     <div class="flexslider banner">
      <ul class="slides">
       <?php
        //If there are images set in the slideshow
         if ( count( $gallery_images ) > 0 ) {
    
    	foreach ( $gallery_images as $image ) {
    
    	?>
        <li><img src="<?php echo $image->guid ?>" alt="<?php echo $image->post_title ?>" /></li>
            <!-- Etcetera -->

    Is my method right? If not, or if you’re not sure, could you share how you get your ‘alt’ attribute for the ‘Image’ custom field?

    http://wordpress.org/plugins/types/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    Okay I’ve reduced the PHP that gets the images to this:

    //Arguments to retrieve all images from the Custom Post Type
    $args = array(
    	'order'=> 'ASC',
    	'post_mime_type' => 'image',
    	'post_parent' => $post->ID,
    	'post_type' => 'attachment'
    );
    
    //Get all the gallery images under the Custom Post Type
    //This way you'll have the image alt attribute value
    $gallery_images = get_children( $args );

    What was I thinking with the code previously?
    BTW I’m still open to ways of improving it.

    Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    Just letting you know that ‘get_children()‘ will not work for images that have already been uploaded to the media library for the Custom Field (sorry, keep mixing this up with Custom Post Type).

    It will however work for images uploaded to the Gallery.

    Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    Just posting to say get_children doesn’t do what I want because it relies on images that a in the Gallery for the page (of course), but that’s not what I want.

    Ideally I want to be able to retrieve images assigned to a custom field of a edited post/ page and those images to contain an alt attribute value that can be set in the Media library or the edited post/ page.

    So far I’m using this plugin instead: http://wordpress.org/plugins/custom-content-type-manager/ – But will post an actual solution when I have one.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Getting the 'alt' attributes on images – Is this right?’ is closed to new replies.