WordPress获取当前文章或页面别名的函数-wordpress&系统-0660BBS_0660bbs

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

晨行星 2018-8-30 2679

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>


最新回复 (1)
全部楼主
  • 旱船 2018-9-5
    0 2

    还有一种方法。

    <?php
    $id=//这里是文章的ID
    $title = get_post($id)->post_name;
    echo $title;//输出文章的 标题  
    ?>


返回