我的网站

wordpress 根据tag name调用的文章

分类:编程 发布日期:2024-05-11 16:14 浏览:1
<?php
// 假设你要查询的标签名称是 'example-tag'
$tag_name = 'example-tag';
 
// 获取标签对象
$term = get_term_by( 'name', $tag_name, 'post_tag' );
 
// 检查标签是否存在
if ( $term ) {
    // 根据标签ID获取文章
    $args = array(
        'tag_id' => $term->term_id,
        'posts_per_page' => 5, // 调用的文章数量
    );
 
    $posts = get_posts( $args );
 
    if ( $posts ) {
        // 输出文章
        foreach( $posts as $post ) {
            setup_postdata( $post );
            ?>
            <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <?php the_excerpt(); ?>
            <?php
        }
        wp_reset_postdata();
    } else {
        echo '没有找到相关文章。';
    }
} else {
    echo '标签不存在。';
}
?>
← 上一篇:dedecms织梦网站栏目增加字段调用方法 下一篇:使用百度地图 API 出现 A parser-blocking, cross site (i.e. different eTLD+1) script 警告 →

相关文章