WordPress.org

Forums

[resolved] Get Post URL (10 posts)

  1. dvent
    Member
    Posted 2 years ago #

    I have the following code

    <div id="featured-posts" class="container_12">
    <?php
     global $post;
     $myposts = get_posts('numberposts=3&category=12');
     foreach($myposts as $post) :
     ?>
    
     <?php global $post; ?>
        <?php
        $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' );
        ?>
    
            <?php
                $colors = array("#000000", "#949c51", "#571c1e", "#f36533", "#782a80", "#f6a41d", "#ed1b24");
                $randomColor = $colors[array_rand($colors)];
            ?>
    
    <a href="LINK OF THE POST"><div class="grid_4 featured-home" style="background: url(<?php echo $src[0]; ?> ) !important;">
        <div class="featured-details" style="border-color: <?php echo $randomColor; ?>;">
            <h2 style="color: <?php echo $randomColor; ?>;"><?php the_title(); ?></h2>
            <?php the_excerpt(); ?>
        </div>
        <div class="featured-lower" style="border-color: <?php echo $randomColor; ?>;"></div>
    </div></a>
     <?php endforeach; ?>
     </div>

    This takes the most recent 3 posts, in category 12 and displays them in a div. I want this whole div to link to the post. You'll see where it says LINK OF THE POST. Can anyone help me get the URL in here?

    Thanks dvent

  2. Bea Cabrera
    Member
    Posted 2 years ago #

    Do you mean:
    <?php the_permalink(); ?>

    http://codex.wordpress.org/Function_Reference/the_permalink

  3. dvent
    Member
    Posted 2 years ago #

    Yes, the Permalink is what I'm trying to get

  4. Bea Cabrera
    Member
    Posted 2 years ago #

    Did that solve it?

    If you were outside the loop you should use get_permalink instead:
    http://codex.wordpress.org/Function_Reference/get_permalink.

  5. dvent
    Member
    Posted 2 years ago #

    That seems to give me the link of the page i'm on, not the URL of the post who's content i'm pulling into that DIV

  6. Bea Cabrera
    Member
    Posted 2 years ago #

    The first function:
    the_permalink(); will give you the link to the current post in the loop (ie. the same one you're pulling with wp_get_attachment_image_src(), the_excerpt(); , the_title(); .....

    The second function:
    get_permalink(); is to be used OUTSIDE the loop and will retrieve the link of a certain post you pass along as an id.

    Therefore, you should use the first one:
    http://codex.wordpress.org/Function_Reference/the_permalink

  7. dvent
    Member
    Posted 2 years ago #

    Thanks Bea!!

    That solved it!

  8. Bea Cabrera
    Member
    Posted 2 years ago #

    There you go.
    I'm glad you finally read the link for the solution I gave you on my first message.
    I had to post it twice.

    Cheers!

  9. dvent
    Member
    Posted 2 years ago #

    It wasn't until you explained about INSIDE / OUTSIDE the loop I got it! :)

  10. Bea Cabrera
    Member
    Posted 2 years ago #

    I'm glad you got it in the end ;)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags