【问题标题】:echo list of tags associated with a post in a data-category tag回显与数据类别标签中的帖子关联的标签列表
【发布时间】:2014-10-02 11:32:49
【问题描述】:

我正在尝试回显与 div 内的数据类别标签中的帖子关联的标签列表。目前我的代码在标签中没有输出任何内容。我也有它输出单词'Array'的地方。我需要显示为'tag1 tag2 etc'

这是我目前在 if have posts 循环中设置代码的方式...

获取值...

    $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 */ ?>

【问题讨论】:

    标签: php wordpress tags


    【解决方案1】:

    根据文档,$tag 是 std 类的对象,为了得到标签名,试试这个

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

    $tag->name 只是获取名为 name 的 stdClass 对象属性。

    这是对象返回的内容,因此您还可以获得其他信息,例如 URL 的 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

    【讨论】:

    • 谢谢,但这没有返回。我要获取的标签是自定义分类法,所以我不知道是否需要特殊处理?
    • 它只是显示..'data-category'是你的意思吗?
    • 啊不,不是...我如何进行变量转储?
    • 如果你有帖子的标签,上面的代码应该可以工作,我不知道这里有什么问题。放置 var_dump($post_tag);在 $post_tag = wp_get_post_tags( $post_id, $args );然后访问该页面,看看你得到了什么输出?
    • 抱歉延迟响应,但 var 转储的输出是 array(0) { }
    【解决方案2】:

    在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;
    }
    }
    

    然后添加了这个 echo my_post_terms();在html中...

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

    【讨论】:

      猜你喜欢
      • 2015-11-02
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-26
      • 1970-01-01
      相关资源
      最近更新 更多