1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

the_permalink php not working

Discussion in 'WordPress' started by sixrfan, Feb 20, 2011.

  1. #1
    at the bottom of the homepage here I have the following code which reproduces the most recent post from category 1 on the homepage:

    
    <h2>The Latest at Cornerstone Antiques</h2>
    
    <?php
    $latestposts = get_posts('numberposts=1&category=1');
    foreach($latestposts as $post) :
       setup_postdata($post);
    ?>
    <h3><a href="<?php the_permalink(); ?>"><?php echo $post->post_title; ?></a></h3>
    <?php the_content(); ?>
    <?php endforeach; ?>
    
    PHP:
    but if you hover over the hyperlink for the post title you'll notice it just links back to the homepage and not to that particular post. I've tried similar code in multiple sites and there always seems to be a different variation of the code to make this work.

    so anyway...how do i get the post title that appears on the homepage to actually link to that post???

    FYI, i did post practically the same issue here but that technique doesnt seem to be working on this site.

    please advise. thanks.
     
    sixrfan, Feb 20, 2011 IP
  2. Dodger

    Dodger Peon

    Messages:
    1,494
    Likes Received:
    60
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Using the_permalink() can only be used within the Loop. Try this instead:

    <h3><a href="<?php echo get_permalink($post->ID); ?>"><?php echo $post->post_title; ?></a></h3>
    Code (markup):
     
    Dodger, Feb 20, 2011 IP
  3. sixrfan

    sixrfan Well-Known Member

    Messages:
    354
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    that worked.

    the force is strong with this one. thanks!!!
     
    sixrfan, Feb 21, 2011 IP