• Resolved richardwilkinson

    (@richardwilkinson)


    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="https://www.richard-wilkinson.com/blog/wp-content/uploads/2007/03/adhd_large.jpg" title="ADHD In Adults"><img src="https://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.

Viewing 14 replies - 1 through 14 (of 14 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    <?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.

    Thread Starter richardwilkinson

    (@richardwilkinson)

    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!

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

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

    Thread Starter richardwilkinson

    (@richardwilkinson)

    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.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    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.

    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!

    Thread Starter richardwilkinson

    (@richardwilkinson)

    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.

    Thread Starter richardwilkinson

    (@richardwilkinson)

    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.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    https://php.net/strip_tags

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

    Thread Starter richardwilkinson

    (@richardwilkinson)

    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?

    You need to print the value.

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

    Thread Starter richardwilkinson

    (@richardwilkinson)

    Yoshi, thankyou so much!

    That was getting frustrating!

    Thanks Otto & Doodlebee too for all your help.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Doh. I knew there was something wrong with that. ??

    Cool! Glad you got it working ??

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Image in excerpt = permalink to post wont work?’ is closed to new replies.