WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Wondrous WordPress humans please help me with a gallery & wp_get_attachment_url (5 posts)

  1. morgyface
    Member
    Posted 1 year ago #

    Good afternoon, if you are reading this I am very pleased and I thank you already even if you now cease reading and never respond, perhaps choosing to eat some eggs instead.

    I need my posts to have a gallery though I cannot use the gallery shortcode. Trust me I cannot. I need more control over the HTML that is generated and need to add classes to the a tag.

    So I have this code within single.php.

    <?php if ( $images = get_children(array(
    'post_parent' => get_the_ID(),
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'order' => 'ASC',
    ))): ?>
    <?php foreach( $images as $image ) : ?>
    <a class="zoom" rel="thumbnails" href="<?php echo wp_get_attachment_url($image->ID); ?>"><img src="<?php echo wp_get_attachment_url($image->ID, 'medium'); ?>"></a>
    <?php endforeach; ?>
    <?php else: // No images ?>
    <?php endif; ?>

    Now this code is so very, very nearly doing what is intended with one cruel exception, wp_get_attachment_url is an ignorant fool and chooses to turn a blind eye to 'medium', it instead returns the URL for the original full sized image.

    So I am told the solution to this is to use, wp_get_attachment_image_src.

    If only it were as straightforward as swapping wp_get_attachment_url with wp_get_attachment_image_src. Oh no it is more complex than that as wp_get_attachment_image_src returns an array and requires some clever coding that is beyond my ability.

    When someone says "oh just return an array using wp_get_attachment_image_src" they might as well be saying "oh just pop down the shop and buy me a wholemeal loaf" to a gerbil.

    So wise ones, I beg of you, put me out of my misery, help me to find a solution. I just want some code to dish out all the attached images in a medium size. Those images need to link to the original full sized images. It sounds so simple, but I've now been awake for 3 days trying to get this to work. During this time I have soiled my office chair and murdered four people. I just want it to end, you're my only hope.

  2. sherred
    Member
    Posted 1 year ago #

    Hi there,

    The top part of this reply is mainly about arrays so hopefully if you ever find yourself in a similar situation in the future you can hopefully avoid soiling your office chair - teach a man to fish and all that.
    If you want to just get the code skip to the bottom.

    You can think of an array as a collection of information (AKA values). You can get a specific value from an array by using a key.

    $array = array(
        "foo" => "bar",
        "bar" => "foo",
    );
    
    echo $array['foo']; //will echo bar

    In php the key is optional, when it is not used PHP will use the increment of the largest previously used integer key.

    $array = array("foo", "bar", "hello", "world");
    var_dump($array);

    Outputs:

    array(4) {
      [0]=>
      string(3) "foo"
      [1]=>
      string(3) "bar"
      [2]=>
      string(5) "hello"
      [3]=>
      string(5) "world"
    }

    The documentation for wp_get_attachment_image_src in the codex tells us that it returns an array containing:

    • [0] => url
    • [1] => width
    • [2] => height

    So the solution to your problem should be:

    <?php foreach( $images as $image ) : ?>
    $full_image = wp_get_attachment_image_src(,$image->ID,'full');
    $medium_image = wp_get_attachment_image_src(,$image->ID,'medium');
    <a class="zoom" rel="thumbnails" href="<?php echo $full_image[0]; ?>"><img src="<?php echo $medium_image[0]; ?>"></a>
    <?php endforeach; ?>
  3. morgyface
    Member
    Posted 1 year ago #

    Well, I didn't need your help, somehow, through a series of miracles I have managed to achieve what was required. I am posting it here for the benefit of those who may also struggle with this in the future.

    My code eventually ended up looking like this:

    <?php if ( $images = get_children(array(
    'post_parent' => get_the_ID(),
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'order' => 'ASC',
    )))
    {
    foreach( $images as $image ) {
    $attachmenturl = wp_get_attachment_url($image->ID);
    $attachmentimage = wp_get_attachment_image_src( $image->ID, medium );
    echo '<a class="zoom" rel="thumbnails" href="' . $attachmenturl .'"><img src="' . $attachmentimage[0] . '"></a>';
    }
    }
     else {
    echo "No Images";
    }
    ?>

    I hope this helps someone in the future. Good luck. x

  4. morgyface
    Member
    Posted 1 year ago #

    Hello again. I have now taken this one step further. I now have an initial bit of code which pulls in the featured image.

    <?php
    image
     if ( has_post_thumbnail()) {
       $full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
       echo '<a class="zoom" rel="thumbnails" href="' . $full_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >';
       echo get_the_post_thumbnail($post->ID, 'large');
       echo '</a>';
     }
    ?>

    And the second chunk of code which generates a list of linked thumbnails but excludes the featured image. This sits underneath the above code to produce an entire gallery.

    <?php
    get_post_thumbnail_id( $post->ID );
    $thumb_ID = get_post_thumbnail_id( $post->ID );
    if ( $images = get_children(array(
    'post_parent' => get_the_ID(),
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'order' => 'ASC',
    'exclude' => $thumb_ID //exluding featured image by its ID )))
    {
    foreach( $images as $image ) {
    $attachmenturl = wp_get_attachment_url($image->ID);
    $attachmentimage = wp_get_attachment_image_src( $image->ID, thumbnail );
    $imagetitle = apply_filters( 'the_title' , $image->post_title );
    echo '<a class="zoom" rel="thumbnails" href="' . $attachmenturl .'"><img width="' . $attachmentimage[1] . '" height="' .$attachmentimage[2] . '" alt="' . $imagetitle . '" src="' . $attachmentimage[0] . '"></a>';
      }
    }
     else { echo "No Images";}
    ?>
  5. morgyface
    Member
    Posted 1 year ago #

    @sherred I must've posted my follow-up as you posted yours. Although I did find a solution it was not done logically or through knowledge it was done through some cut, pasting and guess work so your explanation was thoroughly helpful, thank you so much for taking the time to respond. I reckon between us we've created THE most useful thread on this entire forum.

Topic Closed

This topic has been closed to new replies.

About this Topic