我提供这段代码只是为了以身作则。
我没有测试它,所以可能需要调试。
<?php
function sidebar_tag_cloud_5416__local_agents() {
// List in order of estate agents page
$selected_agents = [183, 184, 182, 181, 180, 179, 178, 177, 176, 174, 173, 258, 172, 171];
$tags_count = 6;
// Let's select 6 random tag ID's
$selected_agents = array_intersect_key($selected_agents, array_flip(array_rand($selected_agents, $tags_count)));
$selected_agents = array('include' => implode(',', $selected_agents));
// Retrieving tags
$tags = get_tags($selected_agents);
// List output
echo '<ul id="tag-cloud-sidebar">';
if (!empty($tags)) {
foreach($tags as $tag) {
// image id is stored as term meta
$image_id = get_term_meta($tag->term_id, 'image', true);
// image data stored in array, second argument is which image size to retrieve
$image_data = wp_get_attachment_image_src($image_id, 'tag_img');
// image url is the first item in the array (aka 0)
$image = $image_data[0];
if (!empty($image)) {
echo '<li><a href="'. get_tag_link($tag->term_id) . '">';
echo '<img title="' . $tag->name . '" alt="' . $tag->name . '" style="width: 160px;" src="' . esc_url($image) . '"/>';
echo '</a></li>';
}
}
}
echo '</ul>';
}
在实际的标签数据检索之前,最好得到6个随机的标签ID。还要注意变量的命名。正确的名称使代码更具可读性。看看随机标签选择算法,数组和做的技巧。最美好的祝福。