wordpress中the_permalink()是用于posts loop循环中(判断是否有文章,如果有文章则展示出来;如果没有文章就显示没有文章),常用于文章分类列表和文章页的模板中,用法如下
<?php if(have_posts()) : ?> <?php while(have_posts()) : the_post(); ?> <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2> <?php the_excerpt(); ?> <?php endwhile; ?> <?php posts_nav_link(); //分页导航 ?> <?php else : ?> no posts! <?php endif; ?>
get_permalink()则比较灵活,可以用在loop循环内,但没有echo打印出来显示在前端;也可以放在循环外,但是如果是在循环外必须加文章id
1、在循环内,the_permalink()相当于打印出来的get_permalink()
<?php echo get_permalink(); ?>
2、在循环外,get_permalink()必须加post id,调用方法如下
<?php echo get_permalink( 5 ); ?>
the_permalink()和get_permalink()的区别
原文:https://www.cnblogs.com/ytkah/p/11634718.html