the_permalink();不工作

时间:2012-01-06 15:20:41

标签: wordpress

好的,我对the_permalink()函数感到疯狂。这不容易解释这个问题,但我会尝试。当我在我的网站的索引页面(index.php),当我点击页脚部分的链接时,它将带我到最近的帖子(single.php),这很好。但是,当我从索引页面(index.php)开始,当我使用导航栏导航到某个页面(page.php)(它与index.php具有相同的布局)时,然后,当我点击页脚部分中的相同链接时,它应该带我到最近的帖子(single.php),但事实并非如此。它带我到当前页面(page.php)而不是single.php。以下是我的代码片段:

页脚:

   <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()很好,但是当我在不同的页面时却不行。我尝试使用echo get_permalink(53),它有效,但当客户端进入仪表板并创建新帖子时会发生什么?新帖子将有不同的帖子ID,该链接不会指向新帖子,但会指出ID等于53的旧帖子。

如果您需要查看其他代码,请告诉我们!

有什么建议吗?谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

看起来你在WP循环之外使用了_permalink()。 get_permalink()在“循环”之外工作,因为你明确地将“Post ID”传递给函数。

阅读本页解释“循环” http://codex.wordpress.org/The_Loop

从您发布的代码片段看,这似乎是个问题 我在下面发布了一些示例“循环”代码:

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

答案 1 :(得分:1)

我猜你误解了“the_permalink”功能的功能。它必须在循环内部使用,它将为您提供循环中正在处理的当前帖子的URL。如果您想要一个始终指向帖子页面的链接,您可以使用以下内容:

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