2014-10-02 50 views
0

我正在尝试与div中的数据类别标签中的帖子相关联的标签的echo列表。目前我的代码在标签中没有输出任何内容。我也有它输出单词'阵列'的地方。我需要显示为“标签1 TAG2等”与数据类别标签中的帖子相关联的标签的echo列表

下面是我目前有我的代码设置的,如果有帖子循环中......

获取值...

$post_tag = wp_get_post_tags($post_id, $args); 

呼应值...

 <div data-category="<?php foreach($post_tag as $tag){ echo $tag; } ?>"> 

下面是完整的代码...

<?php $layout = get_post_type(); ?> 

    <? 

    $slug = get_post($post)->post_name; 
    $post_tag = wp_get_post_tags($post_id, $args); 


    ?> 

    <? if ($layout == 'fullscreenimage'){ ?> 


    <p>layout 1</p> 

     <div class="portfolio-item" data-id="<?php echo $slug; ?>" data-category="<?php foreach($post_tag as $tag){ echo $tag; } ?>"> 

      <h1 class="entry-title"><?php the_title(); ?></h1> 

     </div> 

    <?php } elseif ($layout == 'singleimagetext') { ?> 

     <p>layout 2</p> 

     <div class="portfolio-item" data-id="<?php echo $slug; ?>" data-category="<? //echo $tags; ?>"> 

      <h1 class="entry-title"><?php the_title(); ?></h1> 

     </div> 

    <?php } elseif ($layout == 'casestudy') { ?> 

     <p>layout 3</p> 

     <div class="portfolio-item" data-id="<?php echo $slug; ?>" data-category="<? //echo $tags; ?>"> 

      <h1 class="entry-title"><?php the_title(); ?></h1> 

     </div> 

    <?php } elseif ($layout == 'gallery') { ?> 

     <p>layout 4</p> 

     <div class="portfolio-item" data-id="<?php echo $slug; ?>" data-category="<? //echo $tags; ?>"> 

      <h1 class="entry-title"><?php the_title(); ?></h1> 

     </div> 


    <?php } ?> 

<?php endwhile; ?> 

<?php else : ?> 

    <h2 class=”center”>Not Found</h2> 
    <p class=”center”>Sorry, but you are looking for something that isn’t here.</p> 

<?php endif; ?> 
<?php /* end my loop */ ?> 

回答

0

$标签是根据文档性病类的对象,以获得标记名称只是试试这个

<div data-category="<?php foreach($post_tag as $tag){ echo $tag->name; } ?>"> 

$标签 - >名称是刚开命名为名称stdClass的对象属性。

这是对象返回的内容,因此您可以获取其他信息,例如term_id或slug。

stdClass Object 
     (
      [term_id] => 4 
      [name] => tag2 
      [slug] => tag2 
      [term_group] => 0 
      [term_taxonomy_id] => 4 
      [taxonomy] => post_tag 
      [description] => 
      [parent] => 0 
      [count] => 7 
     ) 

wp get post tags文档

+0

谢谢,但这没有返回。我试图得到的标签是自定义分类,所以我不知道是否需要特殊待遇? – Amesey 2014-10-02 11:57:56

+0

你可以显示var_dump($ post_tag)的输出吗? – 2014-10-02 11:59:28

+0

它只是显示..'data-category'是你的意思吗? – Amesey 2014-10-02 12:01:08

0

加入的功能的functions.php

function my_post_terms() { 

// Get an array of all taxonomies for this post 
$taxonomies = get_taxonomies('', 'names'); 

// Are there any taxonomies to get terms from? 
if ($taxonomies) {  

    // Call the wp_get_post_terms function to retrieve all terms. It accepts an array of taxonomies as argument. 
    $arr_terms = wp_get_post_terms(get_the_ID(), array_values($taxonomies) , array("fields" => "names")); 

    // Convert the terms array to a string 
    $terms = implode(' ',$arr_terms); 

    // Get out of here 
    return $terms; 
} 
} 

然后加入该回波my_post_terms();在html ...

<div class="portfolio-item" data-id="<?php echo $slug; ?>" data-category="<?php echo my_post_terms(); ?>"> 
相关问题