2014-10-02 46 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(); ?>"> 
相關問題