【问题标题】:Get recent tags in wordpress在 wordpress 中获取最近的标签
【发布时间】:2012-04-11 11:24:24
【问题描述】:

我想从最近 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
 ?>

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

【问题讨论】:

    标签: sql tags wordpress


    【解决方案1】:

    检查您的查询是否正确。

    print_r($tags);
    

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

    <?php
    $posttags = get_the_tags();
    if ($posttags) {
      foreach($posttags as $tag) {
        echo $tag->name . ' '; 
      }
    }
    ?>
    

    【讨论】:

    • 谢谢,print_r($tags) - 函数返回标签列表及其参数,但它们没有链接。
    【解决方案2】:

    这个怎么样:

    <?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');
    
    ?>
    

    【讨论】:

    • 非常感谢,但是有一个空响应-Array()
    • 我刚刚在最新的 Wordpress 3.3.1 的全新安装上进行了尝试。添加了一些帖子,添加了一些标签,将此代码添加为插件。正如预期的那样,它在页面底部显示标签。也许您有不同的数据库结构。也许是旧版本的 WP?
    猜你喜欢
    • 2014-08-16
    • 2019-03-18
    • 2012-07-05
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    相关资源
    最近更新 更多