2014-09-04 29 views
0

我真的很苦恼,我确信是一个简单的问题。我似乎无法获得属于某个标记下的帖子以显示在该标签页上,例如:(/ 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(); ?> 

所以这个代码目前并显示标记的标题,但不是我正努力走出帖子的标题。

把问题包装起来。 “我如何显示属于某个标签的帖子。”

+0

是这些默认标签,还是您使用自定义分类标签作为标签 – 2014-09-04 16:31:33

+0

默认标签。谢谢 – jackdh 2014-09-05 09:59:41

+0

你是否切换到了捆绑主题之一。 WordPress的V4今天发布,你有升级。你没有任何代码限制来自特定用户角色的访问和信息,或者没有登录的用户 – 2014-09-05 15:32:21

回答

0

我发现我不得不使用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; ?> 
+1

你应该在你的下一篇文章中添加**“details”**。另外,你可以使用'pre_get_posts'动作 – 2014-09-09 17:42:51

0

你不会告诉循环在任何地方寻找特定的标签......你所做的只是在页面顶部显示当前选定的标签。添加一个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有如何抓住类别和标签的例子。

+0

感谢您的回复,我试过使用您的标签并用有问题的标签替换SLUG,但它似乎并不想工作。有什么你想让我去测试,看看为什么? – jackdh 2014-09-04 15:50:43

+0

你在'if'循环中包含了post PHP,对吗?我没有在上面的示例中显示它,所以添加'<?php the_content(); ?>看看是否有效。 – Brian 2014-09-04 15:57:23

+0

替换//用HTML发布HTML内容?是的,我做了,但没有发布任何内容。这让我觉得它仍然无法找到帖子。 – jackdh 2014-09-04 16:02:50

0

你只需要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')); 
    } 
    } 

} 
+0

感谢您的回复。虽然我恐怕没有用。 – jackdh 2014-09-04 15:51:31

+0

你的标签不是空的吗?尝试打开调试。您没有任何自定义查询或过滤器? – 2014-09-04 16:08:32

+0

感谢您的回复。没有标签确实有一些帖子。该页面上没有自定义查询或过滤器。我不太清楚如何使用调试。 – jackdh 2014-09-04 16:26:40

相关问题