Created
October 30, 2017 10:37
-
-
Save igorveremsky/3cdceca1f2d9ef7e654b6fe5d47c7536 to your computer and use it in GitHub Desktop.
Terms Alphabet Catalog for Wordpress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Template Name: Catalog Template */ | |
function renderAlphabetTermsCatalog($terms, $maxTermCountForLetter = 5) { | |
$termsTitle = array_map(function($term) { | |
return mb_convert_encoding($term->name, 'utf-8'); | |
}, $terms); | |
$lastLetter = ''; | |
$termsForOneLetter = ""; | |
$termsCount = count($termsTitle); | |
$columnIndex = 1; | |
$columnTermIndex = 0; | |
for($i = 0; $i < $termsCount; $i++) { | |
$termTitle = $termsTitle[$i]; | |
$termLink = get_term_link($terms[$i], 'agencies'); | |
$currentLetter = mb_substr($termTitle, 0, 1, 'utf-8'); //first char | |
$termLinkDom = ($columnTermIndex < $maxTermCountForLetter || $currentLetter !== $lastLetter || $i == 0) ? | |
'<a class="catalog-agency-link" href="'.$termLink.'">'.$termTitle.'</a>' : | |
'<a class="catalog-hidden-agency-link" href="'.$termLink.'">'.$termTitle.'</a>'; | |
if ($columnTermIndex == $maxTermCountForLetter && $currentLetter == $lastLetter) $termLinkDom .= '<a class="show-all-agencies-for-letter-btn" href="#">'.__('more...', 'canos').'</a>'; | |
$columnTermIndex++; | |
//echo '<pre>'.$char.$lastChar.'</pre>'; | |
if ($currentLetter !== $lastLetter) { | |
if ($i !== 0) { | |
$shortcodeContent = generateShortcodeContent($lastLetter, $termsForOneLetter, $columnIndex%3 == 0); | |
echo $shortcodeContent; | |
$columnIndex++; | |
$columnTermIndex = 1; | |
} | |
$termsForOneLetter = $termLinkDom; | |
$lastLetter = $currentLetter; | |
} else { | |
$termsForOneLetter .= $termLinkDom; | |
} | |
if ($i == $termsCount - 1) { | |
$shortcodeContent = generateShortcodeContent($lastLetter, $termsForOneLetter, $columnIndex%3 == 0); | |
echo $shortcodeContent; | |
} | |
} | |
} | |
function generateShortcodeContent($letter, $content, $isLastColumn) { | |
$columnHeader = '<div class="catalog-column-header">' . mb_strtoupper($letter, 'utf-8') . '</div>'; | |
$columnContent = '<div class="catalog-column-content">' . $content . '</div>'; | |
$shortcodeContent = $columnHeader.$columnContent; | |
return $shortcodeContent; | |
} | |
get_header(); ?> | |
<h1>Terms Alphabet Catalog</h1> | |
<?php | |
$terms = get_terms([ | |
'taxonomy' => 'term_tax_type', | |
'hide_empty' => false, | |
'orderby' => 'name', | |
'order' => 'ASC', | |
]); | |
renderAlphabetTermsCatalog($terms); | |
?> | |
<?php get_footer(); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment