Forums

[resolved] Image in excerpt = permalink to post wont work? (15 posts)

  1. richardwilkinson
    Member
    Posted 2 years ago #

    Apologies if this is already answered somewhere in support. I've been searching all day and have found nothing here.
    I'm a total novice too! So i hope all my terms are correct!

    Here's the problem:

    I'm using WP2.1 for my portfolio site. It's therefore heavily image-biased.
    I php the_excerpt in the portolio page using
    <?php the_excerpt(); ?><a href="<?php the_permalink(); ?>"></a>
    The excerpt is an image , so there's an href=... in the excerpt.

    here's example code for the excerpt:
    <a href="http://www.richard-wilkinson.com/blog/wp-content/uploads/2007/03/adhd_large.jpg" title="ADHD In Adults"><img src="http://www.richard-wilkinson.com/blog/wp-content/uploads/2007/03/adhd_thumb.jpg" alt="'ADHD In Adults' (Telegraph Magazine)" /></a>

    This seems to be confusing the permalink for the post because when I click on the image I get the image URL instead of a link to the post.

    I just want to be able to click the thumbnail and go to the post.

    There have been similar problems discussed on this forum, but I've found nothing on this actual prob.

    I hope that makes some sort of sense. Let me know if anything (or everything!) needs clarifying.

  2. Otto42
    Moderator
    Posted 2 years ago #

    <?php the_excerpt(); ?><a href="<?php the_permalink(); ?>"></a>

    That makes no sense. You're showing the excerpt and the link *after* it. The link to your post is empty, in order words.

  3. richardwilkinson
    Member
    Posted 2 years ago #

    Thanks for that. I've swapped it round & now it links to the large image (direct link just to the image, not to the post), which is better, but still a problem.

    Any ideas?

    As I said, I'm just learning as I go, adapting a theme, so excuse my ignorance!

  4. Otto42
    Moderator
    Posted 2 years ago #

    What is your code now, and what is the result of it running?

  5. richardwilkinson
    Member
    Posted 2 years ago #

    the code now is:

    <a href="<?php the_permalink(); ?>"><?php the_excerpt(); ?></a>

    ..the result is that the thumbnail does have a link (i.e. the cursor changes when hover, as it did when the code was wrong, actually!) but when I click it it links to the thumbnail URL instead of the post.

  6. Otto42
    Moderator
    Posted 2 years ago #

    Ahh. Well, this is probably because the excerpt itself contains a link to the image file.

    Try something like this:
    <a href="<?php the_permalink(); ?>"><?php strip_tags(get_the_excerpt(),'<img>'); ?></a>
    This will have it strip all the tags out of the excerpt except for the img tags.

  7. doodlebee
    Member
    Posted 2 years ago #

    How can I explain this - you're doing something odd here, and I find it difficult to explain.

    Okay, your post has the image linked to the file on your server. This is also an excerpt of the post - which is just the image. So, when you show the excerpt, you're showing a linked image. So you're showing a link within a link - which will link to the *last* link (God, I feel like Monty Python.)

    So, what your end result is, is this (edited for clarity):

    <a href="URL/archives/42">
    
    <a href="URL TO IMAGE/adhd_large.jpg">
    <img src="URL TO IMAGE/adhd_thumb.jpg">
    </a>
    
    </a>

    Do you see the weirdness here? It's going to link to the *most recent* link - which is the *image itself* not the URL to the post.

    <?php the_permalink(); ?> creates a FULL URL:

    <a href="URL/archives/42">
    text you want to link here but you've replaced it with
     a linked image
    </a>

    <?php the_excerpt(); ?> *also* creates a full URL.

    Am I making sense to you?

    So you need to somehow strip the excerpt of it's anchor link, so that the thumbnail you wait links to the title.

    EDIT: posted at the same time as Otto - he's got a good answer!

  8. richardwilkinson
    Member
    Posted 2 years ago #

    Thanks Otto, that makes a lot of sense!

    I've tried your code in place of mine (I guessed that's what you meant), but now its not showing anything...no thumbnails.

    What am I doing wrong?

    I'm going to read up on strip_tags now!!

    Thanks again for your help.

  9. richardwilkinson
    Member
    Posted 2 years ago #

    Thanks Doodlebee. Yes, that's starting to make sense.

    I'm looking into Otto's answer but to be honest I'm having to learn very fast!!

    Strip Tags remove HTML from the excerpt, right? So, why wont that remove my image and just leave me with the text (of which there is none!!)

    I'm still reading about strip tags....can't find a list of the parameters yet.

  10. Otto42
    Moderator
    Posted 2 years ago #

    http://php.net/strip_tags

    The second parameter to strip tags is a list of tags that it won't strip. Thus the '<img>' bit.

  11. richardwilkinson
    Member
    Posted 2 years ago #

    Thanks Otto, now I understand how strip tags works. So I understand how your workaround works.

    But.....!
    ..it's still not working. It's not outputting anything. I've tried

    <a href="<?php the_permalink(); ?>"><?php strip_tags(get_the_excerpt(),'<img>'); ?></a>
    
    and
    
    <a href="<?php the_permalink(); ?>"><?php strip_tags(get_the_excerpt(),'<src>'); ?></a>

    ..still no luck.
    Any ideas?

  12. yoshi
    Member
    Posted 2 years ago #

    You need to print the value.

    <a href="<?php the_permalink(); ?>"><?php echo strip_tags(get_the_excerpt(),'<img>'); ?></a>

  13. richardwilkinson
    Member
    Posted 2 years ago #

    Yoshi, thankyou so much!

    That was getting frustrating!

    Thanks Otto & Doodlebee too for all your help.

  14. Otto42
    Moderator
    Posted 2 years ago #

    Doh. I knew there was something wrong with that. :)

  15. doodlebee
    Member
    Posted 2 years ago #

    Cool! Glad you got it working :)

Topic Closed

This topic has been closed to new replies.

About this Topic