WordPress.org

Ready to get started?Download WordPress

Forums

Missing an image in my custon code (4 posts)

  1. YorkshireLad
    Member
    Posted 4 years ago #

    Hi,

    I've written my very first function - and shortcode - more or less from scratch, and it almost works perfectly (for what I want, I mean):

    codefunction get_the_person($atts, $content=null) {
    extract(shortcode_atts(array(
    'pid' => 0, 'align' => 'left'
    ), $atts));
    /* global $post; */
    $address = get_bloginfo("template_url");
    $person_id = get_post($pid);
    $ret='<div id="person-'.$person_id->ID.'" class="person">';
    $ret.='<p> post_name.'" rel="bookmark" title="'.$person_id ->post_title.'">';
    if(has_post_thumbnail()) {
    $pid_image = get_the_post_thumbnail( $person_id->ID, 'thumbnail', array( 'class' => 'index-post-thm border person-align-'.$align.'', alt => 'Alt text' ) );
    } else { $pid_image='<img src="'.$address.'/images/img-default.jpg" class="index-post-thm border" alt="blank" />' ; }
    $ret.= $pid_image; $ret.= $person_id -> post_title.'
    <!-- br / -->'.$person_id -> post_excerpt.'</p></div>';
    return $ret;
    }/code

    Sorry about the ugliness of the code. I'm new to it.

    Anyway, there are two problems which have got me stumped. Firstly, even though I've removed wpautop, that $person_id -> post_excerpt bit is still wrapped in unwanted p tags. Grr.

    Secondly; if there is no thumbnail associated with the called post, then the img tag is ignored, even though I set the variable to something to cope with such events. It's not that there's an image error in the browser; the image tag is completely left out.

    What am I doing wrong?
    Gary

  2. YorkshireLad
    Member
    Posted 4 years ago #

    Also, sorry for the way that formatted - I don't usually post code, anywhere.

    Gary

  3. YorkshireLad
    Member
    Posted 4 years ago #

    Okay... further investigation shows that the functions works fine - it just won't work if you include the shortcode more than once in the same page.

    If postA has no thumbnail but postB does, then two blanks are produced.

    If PostA has a thumbnail but postB doesn't, then a thumbnail is produced for postA and no tag/result at all for postB.

    The value of has_post_thumbnail() is persisting across iterations of the shortcode, so is there something that resets all the values, or something like %posts -> post_thumbnail ?

    Gary

  4. YorkshireLad
    Member
    Posted 4 years ago #

    Ha! Fixed!

    Used get_the_post_thumbnail($pid) instead.

    Gary

Topic Closed

This topic has been closed to new replies.

About this Topic