Paolo Scamardella
Paolo Scamardella

Reputation: 245

the_permalink(); not working

Ok, I'm going crazy over the_permalink() function. This is not going to be easy to explain the issue, but I will try. When I'm on the index page of my website(index.php), and when I click on a link that's in the footer section, it will take me to the recent post (single.php), and that is fine. However, when I start off on the index page(index.php), and when I use the navigation bar to navigate to a certain page(page.php)(which has the same layout as the index.php), and then, when I click the same link that's in the footer section it should take me to the recent post (single.php), but it is not. It's taking me to the current page(page.php) instead of single.php. Here is a snippet of my code:

The footer:

   <div id="clear"></div>

        <div id="video">
            <!--<a href="http://www.youtube.com/watch?v=WYc4ZOxRX-4" target="_blank"><img src="<?php bloginfo('template_url'); ?>/images/video_03.png" alt="Video" border="0" width="263" height="193" title="Video" /></a>-->
            <iframe width="263" height="208" src="http://www.youtube.com/embed/WYc4ZOxRX-4?rel=0" frameborder="0" allowfullscreen></iframe>
        </div><!-- end of video -->

        <div id="weeklyadbox">
            <p id="content"><a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_url'); ?>/images/weeklyad.jpg" height="147" width="178" alt="Weekly Specials" border="0" title="Click to See Our Weekly Specials" /></a></p>
        </div><!-- end of weeklyadbox -->


        <div id="weathericon">
            <a href="<?php bloginfo("url"); ?>"><img src="<?php bloginfo('template_url'); ?>/images/icons_03.png" border="0" alt="Weather Icon" title="See Weather" height="96" width="83" /></a>
        </div>

        <div id="directionsicon">
            <a href="<?php bloginfo("url"); ?>"><img src="<?php bloginfo('template_url'); ?>/images/icons_04.png" border="0" alt="Directions Icon" title="Get Directions" height="94" width="96" /></a>
        </div>

        <div id="webcamicon">
            <a href="<?php bloginfo("url"); ?>"><img src="<?php bloginfo('template_url'); ?>/images/icons_05.png" border="0" alt="Web Cam Icon" title="Web Cam" height="96" width="84" /></a>
        </div>

        </div><!-- end of container --> 

        <div id="footer">
        &copy;<?php echo date("Y"); echo " "; bloginfo('name'); ?>
         </div>

</div>

<?php wp_footer(); ?>

The_permalink() is fine when I'm in the index section, but not when I'm on a different page. I tried using echo get_permalink(53), and it worked, but what happens when a client goes in the dashboard and creates a new post? The new post will have a different post ID, and that link will not point out to the new post, but will point out to the old post that has an ID equals to 53.

If you need to see other code, please let me know!

Any advice? Thank you for your help!

Upvotes: 0

Views: 7057

Answers (2)

user1133972
user1133972

Reputation: 21

I'm guessing you misunderstood the functionality of the "the_permalink" function. It must be used inside the loop, and it will give you the url to the current post that is being processed within the loop. If you want to have a link that will always point to your posts page, you could use something like:

<?php
        if(get_option('show_on_front') == 'page')
            echo get_permalink(get_option('page_for_posts'));
        else
            echo bloginfo('url');
?>

Upvotes: 1

Wade
Wade

Reputation: 1065

It looks like you are using the_permalink() outside the WP loop. get_permalink() works outside "the loop" because you are explicitly passing the "Post ID" to the function.

Read this page explaining "the loop" http://codex.wordpress.org/The_Loop

From looking at the snippet you posted that appears to be the problem.
I posted some sample "loop" code below:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> 
<?php /** YOUR CODE HERE **/ ?>
<?php endwhile; else: ?> 
<p><?php
_e('Sorry, no posts matched your criteria.'); ?></p> 
<?php endif; ?>

Upvotes: 1

Related Questions