WordPress.org

Ready to get started?Download WordPress

Forums

Retrieve permalink to attachment page / ID (6 posts)

  1. Agnes
    Member
    Posted 1 year ago #

    I am trying to retrieve the link or better permalink, inside single-post.php, to the page that displays the featured image (and uses an image.php template)

    I manage with this
    <a href="<?php bloginfo('url')?>/?attachment_id=<?php echo get_post_thumbnail_id(); ?>"><?php the_title() ?> </a>

    To get this:
    http://www.mysite.com/?attachment_id=2545

    Yet I d like to get the permalink, but whatever I try I end up getting the URL of the post the attachment is attached to (or parent page), that is the post I am on, and not the URL of the image template.

    Any idea?

  2. Rebecca Schaefer
    Member
    Posted 1 year ago #

    Have you tried get_attachment_link? You'd still need to use get_post_thumbnail_id, but it should return the permalink.

  3. Agnes
    Member
    Posted 1 year ago #

    I am trying here
    http://www.atable.com/traiteur/plateaux-repas/les-ecrins/

    The first link 'les Ecrins' (on bottom of the page) is made with

    <a href="<?php bloginfo('url')?>/?attachment_id=<?php echo get_post_thumbnail_id(); ?>">
    <?php the_title() ?> </a>

    It does send me to the addachment page (http://www.atable.com/?attachment_id=2671) as does the "display' link in the image library (http://www.atable.com/traiteur/plateaux-repas/les-ecrins/plateau-les-ecrins)

    But the second link made with

    <a href="<?php get_attachment_link($post->id); ?>">
    <?php the_title() ?> </a>

    Sends me back to the same product page the image is attached to
    (http://www.atable.com/traiteur/plateaux-repas/les-ecrins/)

    And it is the same if I try:

    <?php $link = get_post_thumbnail_id();?>
    <a href="<?php get_attachment_link($link); ?>">
    <?php the_title() ?> </a>

    I don't get it....

  4. Agnes
    Member
    Posted 1 year ago #

    Finally this worked:

    <?php
    $attachment_id = get_post_thumbnail_id();
    $attachment_page = get_attachment_link( $attachment_id );
    ?>
    <a href="<?php echo $attachment_page; ?>"><?php the_title() ?></a>

    Thanks for putting me on track!

  5. Agnes
    Member
    Posted 1 year ago #

    mm... don't know what happened I am now with ?attachment_id=2671 again :-(

  6. Agnes
    Member
    Posted 1 year ago #

    It seems the problem comes not from the code used, but from the permalink structure and changes to the image titles etc. In some cases even from the admin I can't get the permalink structure.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.