How to add a class to an image within loop

Community Forums Forums General Discussion How to add a class to an image within loop

This topic is: not resolved

This topic contains 3 replies, has 2 voices, and was last updated by  cdils 1 year, 7 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #17096

    J Korn
    Participant
    Post count: 2

    Hey all,

    I’m new to Genesis and still trying to find my way around all of the hooks and filters Genesis makes available.  I’m attempting to add a class to the image within a loop on a custom post archive page.  To do so, I was attempting to use genesis_get_image_default_args, however, that doesn’t seem to be working.

    Here is the code I’m currently using:

    [php]
    function korndev_archive_image( $args ){
    $args = array(
    ‘format’ => ‘html’,
    ‘size’ => ‘full’,
    ‘num’ => 0,
    ‘attr’ => array(‘class’=>’aligncenter’),
    ‘fallback’ => ‘first-attached’,
    );
    return $args;
    }
    add_filter(‘genesis_get_image_default_args’,’korndev_archive_image’);
    [/php]

    Am I doing something wrong?  Missing something?  Any help would be greatly appreciated!

    Thanks!

    #17698

    cdils
    Participant
    Post count: 421

    Are you using your own custom loop/query or using genesis_custom_loop to loop through your posts?

    You might take a look at image.php in the Genesis theme to see how it’s handling images. Looks like genesis_get_image_default_args is called in the genesis_get_image function, which uses wp_get_attachment_image (See how style classes are passed to this last function in the codex: http://codex.wordpress.org/Function_Reference/wp_get_attachment_image)

    I’m trying to be helpful and not send you off following rabbits. :) If that doesn’t get you going in the right direction, maybe you can post the entire contents of your archive page so I can see your function in context.

    Cheers,

    Carrie

     

     


    Have you been helped in this forum? Pay it forward and answer someone else’s question. I bet you’ll know the answer to at least one question. :)

    I host a weekly Genesis-focused podcast called Genesis Office Hours. I tweet @cdils.

    #18058

    J Korn
    Participant
    Post count: 2

    Thanks for the response, Carrie.

    Are you using your own custom loop/query or using genesis_custom_loop to loop through your posts?

    No.  I’m using a template, archive-customtype.php, and within that, just trying to filter the image to add in/modify the class. I was looking through the Genesis code, specifically the image.php file and saw the following within genesis_get_image:

    [php]

    $defaults = apply_filters( ‘genesis_get_image_default_args’, array(
    ‘format’ => ‘html’,
    ‘size’ => ‘full’,
    ‘num’ => 0,
    ‘attr’ => ”,
    ‘fallback’ => ‘first-attached’,
    ) );

    [/php]

    So, I assumed I could filter the genesis_get_image_default_args, however, my code above seems to have no effect.  I’ve found another way around what I was trying to do, but seems like using genesis_get_image_default_args would have made more sense.

    For your reference, here is what my archive template looked like before (pretty straight forward):

    [php]
    remove_action( ‘genesis_before_post_content’, ‘genesis_post_info’ ); // Remove post info
    remove_action( ‘genesis_after_post_content’, ‘genesis_post_meta’ ); // Remove post meta
    add_filter( ‘genesis_get_image_default_args’, ‘korndev_archive_image’); // Modify image

    function korndev_archive_image( $args ){
    $args = array( ‘attr’ => array(‘class’=>’aligncenter’) );
    return $args;
    }
    genesis();
    [/php]

    Thanks!

    #18087

    cdils
    Participant
    Post count: 421

    Check out this thread:

    http://wordpress.org/support/topic/center-feature-images-only

    Maybe you could use a CSS hack until the fix is included in 2.0? You could add a body class to your archive template to be able to specifically target just that page with your CSS (Here’s a good article on body classes: http://designsbynickthegeek.com/tutorials/dynamic-body-classes).

    Keep me posted. :)


    Have you been helped in this forum? Pay it forward and answer someone else’s question. I bet you’ll know the answer to at least one question. :)

    I host a weekly Genesis-focused podcast called Genesis Office Hours. I tweet @cdils.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.