获取页面别名 the_slug 文章别名 获取当前文章或页面别名的函数

admin123 2020-11-15 1052


获取页面别名 the_slug 文章别名,获取当前文章或页面别名的函数。

获取页面别名。the_slug

添加到主题的模板的functions.php中。

/* 文章别名 */
function the_slug() {
    $post_data = get_post($post->ID, ARRAY_A);
    $slug = $post_data['post_name'];
    return $slug; 
}

获取别名,调用函数。

<?php echo the_slug();  ?>


根据页面别名($slug)调用相关文章

<?php
 $post_data = get_post($post->ID, ARRAY_A);
 $slug = $post_data['post_name'];
 query_posts('tag=' . $slug . '&showposts=12'); ?>
 <?php while (have_posts()) : the_post(); ?>
 <figure class="col-xs-4 col-sm-3 col-md-3 col-lg-2">
 <div class="news-box tags">
 <a href="<?php the_permalink(); ?>" target="_blank"><img src="<?php index_thumbnail(); ?>" alt="<?php the_title(); ?>"></a>
 <figcaption class="pic-hover">
 <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
 </figcaption>
 </div> 
 </figure>
 <?php endwhile; wp_reset_query();?>


最新回复 (0)
全部楼主
返回