WordPress获取当前文章或页面别名的函数。
第一步、在wordpress主题的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)
<div class="row">
<?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-6 col-sm-4 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();?>
</div>