我是PHP新手,写了这个函数。这是一个按字母顺序排列的导航,只显示在该字母和当前过滤的“慷慨”分类术语中都有帖子的字母。
我使用多种分类查询来查找“artistas”帖子,例如,这些帖子被标记为“rock”和“funk”。
这个函数工作得很好,输出的正是我想要的,但是。。。
多个回路确实滞后于加载时间,而且作为n00b,我不确定如何优化功能。
任何指示或指导都是VERY 非常感谢。如果你愿意,我可以用时间来换取时间(我写了一些刻薄的CSS):)
非常感谢。
<?php
function bam_artist_alfa() {
$taxonomy = \'alfa\';
$uri = my_url();
$home = \'http://buenosairesmusic.com/\';
// save the terms that have posts in an array as a transient
if ( false === ( $alphabet = get_transient( \'bam_archive_alphabet\' ) ) ) {
// It wasn\'t there, so regenerate the data and save the transient
$terms = get_terms($taxonomy);
$alphabet = array();
if($terms){
foreach ($terms as $term){
$alphabet[] = $term->slug;
}
}
set_transient( \'bam_archive_alphabet\', $alphabet );
}
if(strstr($uri, \'/artista/\') ) {
$uri = str_replace(\'/artista/\',\'/?alfa=\', $uri);
$uri = substr_replace($uri ,"",-1);
}
$all_link = removeqsvar($uri, \'alfa\');
$last = $all_link[strlen($all_link)-1];
if($last == \'?\') $all_link = substr_replace($all_link ,"",-1);
if($all_link == $home)
$all_link = $home.\'artistas\';
if( isset($_GET) && isset($_GET[\'alfa\']) ) {
$is_alfa = (!is_tax(\'alfa\') && !$_GET[\'alfa\'] ? false : true );
} else {
$is_alfa = (!is_tax(\'alfa\') ? false : true );
}
$all_current = ($is_alfa == true ? null : \' bg1 round-res\' );
?>
<ul class="bbw bo alfa-nav c2">
<li class="all-link<?php echo $all_current; ?>">
<?php if(!$is_alfa) { echo \'A–Z\'; } else { ?>
<a href="<?php echo $all_link; ?>">A–Z</a>
<?php }?>
</li>
<?php
$query = $_SERVER[\'QUERY_STRING\'];
$genre = null;
$orden = (isset($_GET) && isset($_GET[\'orden\']) ? \'&orden=fecha\' : null);
if( strstr($uri,\'/genero/\') ) {
$genre = str_replace(\'/genero/\',\'/?genero=\', $uri);
$genre = substr_replace($uri ,"",-1);
$genre = explode(\'/\',$genre);
$genre = end($genre);
} elseif( isset($_GET) && isset($_GET[\'genero\']) ) {
$genre = $_GET[\'genero\'];
}
if(!empty($genre)) {
$spaces = strpos($genre,\' \');
$genre = ($spaces === false ? $genre : explode(\' \', $genre) );
}
function has_artists($i, $genre) {
if(empty($genre)) {
$termquery[\'tax_query\'] = array(
array(
\'taxonomy\' => \'alfa\',
\'terms\' => $i,
\'field\' => \'slug\',
),
);
$termquery[\'post_type\'] = \'artistas\';
} else {
$alfaquery[] = array(
\'taxonomy\' => \'alfa\',
\'terms\' => $i,
\'field\' => \'slug\',
);
if(is_array($genre)) {
$genres[] = array(
\'taxonomy\' => \'genero\',
\'terms\' => $genre,
\'field\' => \'slug\',
\'operator\' => \'AND\'
);
} else {
$genres[] = array(
\'taxonomy\' => \'genero\',
\'terms\' => $genre,
\'field\' => \'slug\',
);
}
$termquery[\'tax_query\'] = array_merge($genres, $alfaquery);
$termquery[\'tax_query\'][\'relation\'] = "AND";
$termquery[\'post_type\'] = \'artistas\';
}
$has_artists = get_posts($termquery);
if($has_artists) {
return true;
} else {
return false;
}
}
foreach(range(\'a\', \'z\') as $i) :
$current = ($i == get_query_var($taxonomy)) ? "bg1 round-res" : "menu-item";
if(empty($genre)) {
$link = $home.\'?alfa=\'.$i.$orden;
} else {
$genrestring = (is_array($genre) ? implode(\'+\',$genre) : $genrestring = $genre );
$link = $home.\'?alfa=\'.$i.\'&genero=\'.$genrestring.$orden;
}
if ( has_artists($i,$genre) && $i != get_query_var($taxonomy) ){
?>
<li class="<?php echo $current; ?>">
<?php printf(\'<a href="%s">%s</a>\', $link, strtoupper($i) ) ?>
</li>
<?php } else { ?>
<li class="<?php echo $current; if($i != get_query_var($taxonomy)) {echo \' empty\';} ?>">
<?php echo strtoupper($i); ?>
</li>
<?php
}
endforeach;
?>
</ul>
<?php } ?>