获取标签帖子wordpress

时间:2015-09-30 09:01:24

标签: php html wordpress tags

我写了一些代码,会自动创建一些帖子并为其添加标签。我可以在“所有帖子”管理面板中看到这些标签,然后点击帖子“标记”链接即可获得带有标记的帖子。

这是我的代码:

<?php $tag_ID= single_tag_title();
                    $args = array(
                        'post_type' => 'post',
                        'tag_id'    =>  $tag_ID,
                        'posts_per_page' => 10,
                        'order'          =>'ASC'
                    );
                    $posts = get_posts( $args );
                    var_dump($args );
                    foreach ( $posts as $post ) {
                        ?>

你能帮我搞个标签吗? 谢谢。

1 个答案:

答案 0 :(得分:0)

wp-content/themes/yourthemefolder/中创建新文件( tag.php ) 并将代码放在其中。

<?php
get_header();

$tag = single_tag_title('', false);
echo '<h1>Tag: ' . $tag . '</h1>';

$args = array(
    'post_type' => 'post',
    'taxonomy' => $tag,
    'terms' => $tag,
    'posts_per_page' => 10,
    'order' => 'ASC'
);
$postslist = get_posts($args);

foreach ($postslist as $post) :
    setup_postdata($post);
    ?>
    <div id="post">
        <h2>Post title:<?php the_title(); ?></h2>

        <p><?php the_content(); ?></p>
    </div>
<?php endforeach; 
get_footer(); ?>