• I’m using wp_get_attachment_link to link an image on my front page to the post. But it sends me to a post with the name of the attachment as the name of the post. So, the url ends up something like:

    https://mysite.com/category/title/attachment/image-name’

    Is there a way to do this so that it goes to the post instead of to the attachment?

    Here’s the code I’m using on my home page:

    ‘$args = array(
    ‘post_type’ => ‘attachment’,
    ‘numberposts’ => 5,
    ‘post_status’ => null,
    ‘post_parent’ => null, // any parent
    ‘order’ => ‘DESC’,
    );
    $attachments = get_posts($args);
    if ($attachments) {
    foreach ($attachments as $post) {
    setup_postdata($post);
    echo wp_get_attachment_link($post->ID, ‘medium’, true);’

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter modern

    (@popart)

    I figured it out!

    In WordPress folder “wp-includes”, find the file “link-template.php”

    On line 183, you will see the code:
    ‘$name = ‘attachment/’ . $object->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker’

    change it to:
    ‘$name = ‘.’; // <permalink>/<int>/ is paged so we use the explicit attachment marker’

    That worked for me. Yeah! ??

    Thread Starter modern

    (@popart)

    Can anyone help me edit the above code (in first post) so that ‘wp_get_attachment_link’ only pulls one attachment per post??

    Thread Starter modern

    (@popart)

    Or, even better, pull attachments randomly??…

    ‘numberposts’ => 1, i beleive.

    When I use the code above, it just pulls EVERYTHING from the media gallery..even when those images are not part of a post.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘wp_get_attachment_link’ is closed to new replies.