我想从过去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
?>
它不起作用。例如,没有标记链接。
答案 0 :(得分:0)
检查您的查询是否正确。
print_r($tags);
看看你是否收到任何数据? 这是标记的示例代码。创建查询并将其传递给$ posttags变量以获得所需的结果。
<?php
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name . ' ';
}
}
?>
答案 1 :(得分:0)
这个怎么样:
<?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 .= '→ <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');
?>