如何在 WordPress 中为 post 检索 Tag_IDs?

How to retrieve Tag_IDs for a post in WordPress?

我需要显示 post 的标签 ID。我试过了

$myTopics_id = '';
$tags = get_the_tags();
        
foreach($tags as $tag) {
    $myTopics_id .= $tag->tag_ID;
}

在循环内获取要在此处显示的标签:

<div onclick="location.href='<?php the_permalink(); ?>'" data-topics="<?php echo $myTopics_id; ?>">

但它不显示任何内容,字符串只是保持为空:

<div onclick="http://localhost/posts/" data-topics="">

我做错了什么?

编辑:感谢用户 biesior,我现在将代码更改为:

<?php $wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1)); ?>

    <?php if ( $wpb_all_query->have_posts() ) : ?>

        <?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); 
        
            /* Get Category ID */
            $categories = get_the_category();
            $mySection_id = $categories[0]->cat_ID;

            /* ------- */

            /* Get Tag IDs */
            $myTopics_id = '';
            $tags = get_tags();
            
            foreach($tags as $tag) {
                $myTopics_id .= $tag->term_id;
                $myTopics_id .= ", ";
            }

        ?>

            <div data-topics="<?php echo $myTopics_id; ?>" data-section="<?php echo $mySection_id; ?>">   
                ... content to be displayed ... 
            </div>
        <?php endwhile; ?>

    <?php wp_reset_postdata(); ?>

<?php endif; ?>

但结果仍然是错误的,因为它是将 term_ids 相加而不是按 post.

单独显示

所以,举个例子,我目前的结果是

<div data-topics="15, 16, 13, 12, 10, 8, 11, " data-section="3"> ... </div>

如您所见,每个 post 的当前类别显示正确。但是数据主题值是错误的,并且在每个 post 上都是相同的。 每个 post 可能有多个标签。因此,如果结果是

,那将是正确的
<div data-topics="15, 16, " data-section="3"> ... </div>

第一个post,然后

<div data-topics="13, " data-section="7"> ... </div>

第二次

<div data-topics="12, 10, 8, " data-section="1"> ... </div>

第三个post依此类推。

但目前每个 post 都具有相同的数据主题值(“15、16、13、12、10、8、11,”),因此循环中似乎存在错误或在标签请求中。但我不知道是什么和在哪里。

首先,您不需要再开始一个 finish PHP 块,这样做一次就足够了。第二件事——你需要调试 get_tags() return 的作用。一般来说这段代码 应该 工作,无论如何没有实例 and/or 样本数据无法检查它。此外,有更好的方法来创建逗号分隔的值列表,而不仅仅是粘合(即避免尾随逗号)

<?php

$wpb_all_query = new WP_Query(array ('post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => -1));
if ($wpb_all_query->have_posts()) :

    while ($wpb_all_query->have_posts()) : $wpb_all_query->the_post();

        /* Get Category ID */
        $categories = get_the_category();
        $mySection_id = $categories[0]->cat_ID;

        /* Get Tag IDs */

        $tags = get_tags();
        var_dump($tags); // debug it to check what tags are returned in each `while` loop
        $tagsArray = [];


        foreach ($tags as $tag) {
            $tagsArray[] = $tag->term_id;
        }
        $myTopics_id = implode(',', $tagsArray);

        echo "<div data-topics='{$myTopics_id}' data-section='{$mySection_id}'>
            ... content to be displayed ...
        </div>";


    endwhile;

    wp_reset_postdata();
endif;

我会 post 在这里提供我的解决方案,以防万一有人遇到同样的问题:

$myTopics = get_the_tags();
$myTopics_id = "";
$myTopics_array = [];

if ($myTopics) {
    foreach($myTopics as $tag) {
         $myTopics_array[] = $tag->term_id;
    }
}

$myTopics_id = implode(',', $myTopics_array);


echo "<div data-topics='{$myTopics_id}'>";