如何显示具有特定标记的帖子
我真的很苦恼,我确信是一个简单的问题。我似乎无法获得属于某个标记下的帖子以显示在该标签页上,例如:(/ tag/blog /),博客是标签。如何显示具有特定标记的帖子
到目前为止,Wordpress上的Official Tag Templates页面仍然无法工作。
我不需要一个层次,所以tag.php工作正常。使用single_tag_title()
它可以正确显示页面顶部的标签。
Official Tag Templates的其余部分没有提供更多关于它的细节,我可以使用默认的Loop或自定义的Loop。我试图用下面显示的自定义之一,但不起作用。 (我已经把它降到最低目前不担心造型。)
<?php get_header(); ?>
<p>Tag: <?php single_tag_title(); ?></p>
<div class="container">
<div id="content" class="clearfix row">
<div id="main" class="col col-lg-8 clearfix blog-page-padding" role="main">
<?php
if (have_posts()) :
while (have_posts()) :
the_title();
endwhile; // end while
endif; // end if
?>
</div> <!-- end #main -->
<?php get_sidebar('blog'); // Blog ?>
</div> <!-- end #content -->
</div>
<?php get_footer(); ?>
所以这个代码目前并显示标记的标题,但不是我正努力走出帖子的标题。
把问题包装起来。 “我如何显示属于某个标签的帖子。”
我发现我不得不使用WP查询循环,使他们由于被定制的帖子类型。
<?php if (is_tag()) {$term_id = get_query_var('tag_id'); $taxonomy = 'post_tag'; $args ='include=' . $term_id; $terms = get_terms($taxonomy, $args);} ?>
<!-- This gets the tags slug -->
<?php $query = new WP_Query(array("post_type" => array('blog', 'portfolio'), "tag" => $terms[0]->slug )); while ($query->have_posts()) : $query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
你应该在你的下一篇文章中添加**“details”**。另外,你可以使用'pre_get_posts'动作 – 2014-09-09 17:42:51
你不会告诉循环在任何地方寻找特定的标签......你所做的只是在页面顶部显示当前选定的标签。添加一个PHP if
语句转换成你的循环抓住与标签的帖子:
<?php get_header(); ?> // Get the header
<p>Tag: <?php single_tag_title(); ?></p> // Display the tag name
<div class="container">
<div id="content" class="clearfix row">
<div id="main" class="col col-lg-8 clearfix blog-page-padding" role="main">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> // Start your loop
<?php if(tag_slug(' THE SLUG ')) : ?> // Look for the tag
// Post HTML stuff
<?php endif; ?>
<?php endwhile; endif ?> // finish the loop
</div> <!-- end #main -->
<?php get_sidebar('blog'); // Blog ?>
</div> <!-- end #content -->
</div>
<?php get_footer(); ?>
This page from the Codex有如何抓住类别和标签的例子。
你只需要the_post()
函数,你必须在while中调用它,它应该是你调用的第一个函数,因为它加载了模板标签。
<?php
if (have_posts()) :
while (have_posts()) : the_post();
the_title();
endwhile; // end while
endif; // end if
?>
如果标签在自定义文章类型只是利用了你应该添加这样的事情对你的functions.php:
add_action('pre_get_posts', 'custom_function');
function custom_function($query){
if(! is_admin()){
if(is_tag()){
$query->set('post_type', array('your_post_type', 'your_other_post_type', 'post'));
}
}
}
是这些默认标签,还是您使用自定义分类标签作为标签 – 2014-09-04 16:31:33
默认标签。谢谢 – jackdh 2014-09-05 09:59:41
你是否切换到了捆绑主题之一。 WordPress的V4今天发布,你有升级。你没有任何代码限制来自特定用户角色的访问和信息,或者没有登录的用户 – 2014-09-05 15:32:21