2014-09-01 50 views
0

我正在构建复杂的标签云。在wordpress中使用元数据返回最流行的标签

Wordpress目前有一个名为'get_tags'的内置函数,我正在使用它来构建我的网站上使用的大多数杨树标签的列表。

继承人我当前工作的PHP函数:

function top_brand_tags($related_publication) { 

     $tag_data = ''; 
     $tags = get_tags(); 

     $counts = $tag_links = array(); 

     foreach ((array) $tags as $tag) 
     { 
      $counts[$tag->name] = $tag->count; 
      $tag_links[$tag->name] = get_tag_link($tag->term_id); 
     } 

     asort($counts); 
     $counts = array_reverse($counts, true); 
     $i = 0; 
     foreach ($counts as $tag => $count) 
     { 
       $i++; 
       $tag_link = str_replace(' ', '-', wp_specialchars($tag)); 
       $tag = str_replace(' ', ' ', wp_specialchars($tag)); 

       if($i < 11) 
       { 
         if ($_GET['the_post_tag'] == $tag_link){$post_tag_class = "on";}else{$post_tag_class = "";} 

         $tag_data.= '<a href="'.site_url().'/see-all?the_post_tag='.$tag_link.'&related_publication='.$related_publication.'" > 
         <li class="filter-btn">'.ucfirst($tag).' ('.$count.')</li> 
         </a>';      
       } 
     } 

     return $tag_data; 
} 

我现在需要的,这样它可以让我在多彩键和值传递到修改此功能。该功能然后必须搜索具有与其关联的该键和值的所有帖子,并基于这个新标准返回最受欢迎的标签。

因此,要总结:我需要编写一个新的函数,将:

1 - 搜索包含特定元键和值

2的所有帖子 - 返回基于前10个最常用的标签这些结果。

回答

0

首先,通过所述元获得具有想要的元集和订购的帖子。

$posts = new WP_Query(array(
    'meta_key' => 'your_meta_key', 
    'orderby' => 'meta_value' // or 'meta_value_num' 
    'order' => 'DESC', 
    'posts_per_page' => 50 // Grabs 50 posts at maximum, which in turn gets the tags only for those posts, set to -1 to get all posts 
)); 

然后通过每个帖子并抓住可用的标签。

// Save each unique tag and their count in this temp array. 
$tags_counts = array(); 

if ($posts -> have_posts()) : while ($posts -> have_posts()) : $posts -> the_post(); 

    $post_tags = wp_get_object_terms(get_the_ID(), 'post_tag'); 

    if (empty($post_tags) { 
     continue; // Skip post if no tags set. 
    } 

    foreach ($post_tags as $tag) { 
     if (array_key_exists($tag -> slug, $tags_counts) { 
      // Increment tag count if tag already present. 
      $tags_counts[ $tag -> slug ] = $tags_counts[ $tag -> slug ] + 1; 
     } 

     else { 
      // New tag found, add to the counts list. 
      $tags_counts[ $tag -> slug ] = 1; 
     } 
    } 

endwhile; endif; 

// Order the tag count array from most to least. Remember to reverse the sort afterwards to get "biggest first". 
asort($tags_counts, SORT_NUMERIC); // Use 'asort' to keep index association. 
$tags_counts = array_reverse($tags_counts, true); // 'true' preserves keys 

// If you want to limit the tags to 10 max, slice the array. Last parameter 'true' sets to preserve keys during slice operation. 
$tags_counts = array_slice($tags_counts, 0, 10, true); 

现在我们已经通过了通缉meta_key集和计算基于每个标签计数的职位了。您可以为每个标签云建立一个标签云:

$tag_cloud = '<ul class="custom-tag-cloud">'; 

foreach ($tags_counts as $tag => $count) { 
    $tag_obj = get_term_by('slug', $tag, 'post_tag'); // get tag term object. 

    // Linked element: 'tagname (count)'. 
    $tag_cloud .= sprintf('<li><a href="%s">%s (%s)</a></li>', get_tag_link($tag_obj -> term_id), $tag_obj -> name, $count); 
} 

$tag_cloud .= '</ul>'; 

声明:未经测试,但其逻辑应类似于上述内容。

+0

伟大的工作。谢谢。 – richelliot 2014-09-03 16:54:46

相关问题