0

我想从最近 10 个帖子中获取最近的标签。或者获取最近帖子中使用的最近 100 个标签。我的代码:

    <?php

 $tags .= "SELECT $wpdb->terms.term_id, $wpdb->terms.name, $wpdb->terms.slug
FROM $wpdb->terms
    INNER JOIN $wpdb->term_taxonomy ON ($wpdb->terms.term_id =     $wpdb->term_taxonomy.term_id)
    INNER JOIN $wpdb->term_relationships ON ($wpdb->terms.term_id = $wpdb->term_relationships.term_taxonomy_id)
    INNER JOIN $wpdb->posts ON ($wpdb->term_relationships.object_id = $wpdb->posts.ID)
WHERE $wpdb->term_taxonomy.taxonomy = 'post_tag'
ORDER BY $wpdb->posts.post_date DESC";

$tags= $wpdb->get_results($tags); // $query being the above SQL
foreach ($tags as $tag) {
if (!isset($stack[$tag->term_id]))
    $stack[$tag->term_id] = $tag;
}
print_r($stack); // should print an array of all tags, ordered by last used
 ?>

它不工作。例如,没有指向标签的链接。

4

2 回答 2

0

检查您的查询是否正确。

print_r($tags);

看看你是否得到任何数据?这是标签的示例代码。创建查询并将其传递给 $posttags 变量以获得所需的结果。

<?php
$posttags = get_the_tags();
if ($posttags) {
  foreach($posttags as $tag) {
    echo $tag->name . ' '; 
  }
}
?>
于 2012-04-11T11:56:46.880 回答
0

How about this:

<?php

function my_tags_test ()
{
    ?> 
    <div style="background: white; color: black; font-size: 15px;">
    <h1 style="font-size: 100px;">my_tags_test:</h1>
    <?php

    global $wpdb;


    $sql =
    '
drop temporary table if exists last_10_posts
    ;';
    $wpdb->query ($sql);

    $sql =
    '
create temporary table last_10_posts (ID int, post_date datetime)
    ;';
    $wpdb->query ($sql);

    $sql =
    '
insert into last_10_posts
select
    ID, post_date
from
    ' . $wpdb->posts . '
order by
    post_date desc
limit 0, 10
    ;';
    $wpdb->query ($sql);

    $sql =
    '
SELECT terms.term_id, terms.name, terms.slug

FROM ' . $wpdb->terms . ' terms

INNER JOIN ' . $wpdb->term_taxonomy . ' tt
    ON (terms.term_id = tt.term_id)

INNER JOIN ' . $wpdb->term_relationships . ' tr
    ON (terms.term_id = tr.term_taxonomy_id)

INNER JOIN ' . $wpdb->posts . ' posts
    ON (tr.object_id = posts.ID)

INNER JOIN last_10_posts
    ON (last_10_posts.ID = posts.ID)

WHERE
    tt.taxonomy = \'post_tag\'

ORDER BY
    posts.post_date DESC

    ';

    $tags = $wpdb->get_results ($sql); // $query being the above SQL


    $stack = array ();

    $links = '';

    foreach ($tags as $tag)
    {
        if (!isset($stack[$tag->term_id]))
        {
            $stack[$tag->term_id] = $tag;
        }

        $links .= '&rarr; <a href="' . esc_attr (get_tag_link ($tag->term_id)) .'">' . esc_html ($tag->name) . '</a><br/>';
    }


    echo '<div style="margin: 20px; padding: 20px;">' . $links . '</div>';

    echo '<pre>' . esc_html (print_r ($stack, true)) . '</pre>'; // should print an array of all tags, ordered by last used

    ?> 
    </div>
    <?php
}

add_action ('wp_footer', 'my_tags_test');

?>
于 2012-04-11T12:26:26.073 回答