- 查詢所有tag及其對(duì)應(yīng)跳轉(zhuǎn)鏈接
$tags = get_tags(array('get'=>'all'));
$output .= '<ul class="tag-cloud-list">';
if($tags) {
foreach ($tags as $tag):
$output .= '<li><a href="'. get_term_link($tag).'">'. $tag->name .'</a></li>';
endforeach;
} else {
_e('No tags created.', 'text-domain');
}
$output .= '</ul>';
echo $output;
-
交互tag查詢
場(chǎng)景如下均唉,通過頁(yè)面中選中一個(gè)或多個(gè)tag進(jìn)行交互查詢選擇tag下的文章,涉及到post請(qǐng)求
1、將post請(qǐng)求發(fā)送給根目錄下的admin-ajax.php
2、在主題functions.php中通過鉤子函數(shù)护姆,處理傳來(lái)的參數(shù),返回處理結(jié)果 注意
js
中post請(qǐng)求的action:tagQuery掏击,為回調(diào)鉤子函數(shù)的tagQuery函數(shù)名稱
// javascript
<?php $admin_url=admin_url( 'admin-ajax.php' ); ?>
$(document).ready(function($){
var data={
action:'tagQuery',
tag: param, // 要查詢的tag數(shù)組
page: paged
}
$.post("<?php echo $admin_url;?>", data, function(response) {
if(!response){
return;
}else{
$("#tagRes").html(response);
}
});
});
// php functions.php
function tagQuery(){
$tagArr = $_POST['tag'];
$page = $_POST['page'];
// 動(dòng)態(tài)分頁(yè)查詢
$quyStr = $tagArr[0];
for($i = 1 ; $i < count($tagArr) ; $i++){
$quyStr.='+'.$tagArr[$i]; // 多個(gè)tag查詢卵皂,為+拼接的tag字符串
}
$tagQur = array(
'tag' => $quyStr,
'orderby' => 'modified',
'order' => 'DESC',
'posts_per_page'=> 12,
'paged'=> $page
);
$wp_query = new WP_Query($tagQur);
$max_page = $wp_query->max_num_pages; // 最大頁(yè)數(shù)
query_posts( $tagQur );
while ( have_posts() ) : the_post();
$perLink = get_the_permalink();
echo "<a href=".$perLink.">";
echo post_item_show(1);
echo "</a>";
endwhile;
}
pageSet($page,$max_page);
}
wp_reset_query();
die();
}
add_action('wp_ajax_tagQuery', 'tagQuery');
add_action('wp_ajax_nopriv_tagQuery', 'tagQuery');
參考文章: