dtrb96410 2017-01-28 11:23
浏览 24
已采纳

在The Loop中,我放置导航编码的位置是否重要?

I am currently building my own WordPress Theme, with the view of 'learning on the job' as I am fairly new to writing my own PHP Coding.

I want to place a 'Previous' and 'Next' set of links to appear at the bottom of my Blog Page. I have referred to the WordPress Codex, which states to place the following code after The Loop:

<div class="navigation">
 <div class="alignleft"><?php previous_posts_link('&laquo; Read Previous Blogs') ?></div>
 <div class="alignright"><?php next_posts_link('Read More Blogs &raquo;','') ?></div>
 </div>

By 'after The Loop', does this mean after the 'endwhile' statement but before the 'endif;' or after them both all together? I have placed the above code in both scenarios but it does not seem to make a difference. Just wondering if there is a best practice etc. I have included The Loop coding I am using, for reference, in case my coding affects any answers. Please ignore the numbers such as [1], [2] and [3] etc. I have placed these as comments for my own use, where I have created my own list of notes on a separate file. As a side question, commenting my coding like this won't affect performance etc?:

<?php   
   /*
    =========================================
    Generating the Posts.  The Loop.  (Start)
    =========================================
  */

if ( have_posts() ):                        //[1]
        while( have_posts() ): the_post();      //[2] and [3] 
?>
        <h3><a href="<?php the_permalink(); //[4]?>"><?php the_title(); //[5]?></a></h3> 
        <p><?php the_content(); // [6]?></p> 
        <small>This entry was posted on: <?php the_date('l, jS F Y'); //[7]?> at <?php the_time('g:i a'); //[7]?> and is filed under <?php the_category(); //[7]?></small>
        <small>This Article was written by: <?php the_author_link(); //[7]?></small>
        <div class="post"><?php edit_post_link('Edit','','<strong>|</strong>'); //[8]?>  
        <?php comments_popup_link('Be the first to comment »', 'Just the one comment so far »', '% Comments »', '', 'Comments are Closed for this article.'); //[8]?></div>

<?php 
        endwhile; //[9]
        endif;    //[10]
/*
    =======================================
    Generating the Posts.  The Loop.  (End)
    =======================================
*/
?>
 <div class="navigation">
 <div class="alignleft"><?php previous_posts_link('&laquo; Read Previous Blogs') ?></div>
 <div class="alignright"><?php next_posts_link('Read More Blogs &raquo;','') ?></div>
 </div>
  • 写回答

2条回答 默认 最新

  • dongzhang7382 2017-01-28 12:01
    关注

    Nope You Can place theme anywhere between endwhile; and endif loop . never put after endif; view below snippet :

    <?php
    
    if ( have_posts() ):    //Checks if posts available 
    
        while( have_posts() ): the_post();  //loop starts
        ?>
        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> 
        <p><?php the_content(); ?></p> 
        <small>This entry was posted on: <?php the_date('l, jS F Y'); ?> at <?php the_time('g:i a'); ?> and is filed under <?php the_category(); ?></small>
        <small>This Article was written by: <?php the_author_link(); ?></small>
        <div class="post"><?php edit_post_link('Edit','','<strong>|</strong>'); //[8]?>  
            <?php comments_popup_link('Be the first to comment »', 'Just the one comment so far »', '% Comments »', '', 'Comments are Closed for this article.');?></div>
    
            <?php 
                endwhile; //loop end
            ?>
        <div class="navigation">
           <div class="alignleft"><?php previous_posts_link('&laquo; Read Previous Blog Posts') ?></div>
           <div class="alignright"><?php next_posts_link('Read More Blog Posts &raquo;','') ?></div>
       </div> <?php endif;  ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”