Creating an alphabetical navigation links based on the entries’ titles.
Method 1 - Alphabet at the top linking to titles with that letter
1. First, create a navigation bar where you want it to be displayed:
<a href="{path=weblog/test/a}">A</a> | <a href="{path=weblog/test/b/}">B</a>
| <a href="{path=weblog/test/c/}">C</a>
and so on for all the letters you need.
Here ‘’weblog’’ is a title of the template group and ‘’test’’ is a title of the template which will serve the query (you might need to create a new template or you can use an existing one)
2. In the ‘’weblog/test’’ template insert the following code:
{exp:query sql="SELECT title, entry_id, url_title AS urlt FROM exp_weblog_titles
WHERE weblog_id = '2' AND title LIKE '{segment_3}%' ORDER BY title ASC"}
<a href="{path=Template_Group/Template}{urlt}/">{title}</a><br />
{/exp:query}
where ‘’weblog_id’’ is your weblog’s ID (you can check it in the Control Panel --> Admin --> Weblog Management)
Note: this code will only sort entries based on their titles. It does not work if you want to sort by a custom field.
Method 2 - Titles listed alphabetically with each new letter a header
eg.
A
Apples
Avacados
B
Bananas
NOTE: PHP must be set on and to Output in the template group preferences.
<ul>
<?php $letter='_'; ?>
{exp:weblog:entries orderby="title" sort="asc" weblog="{weblog_name}" }
<?php
if ($letter<>substr("{title}",0,1)) {
$letter=substr("{title}",0,1);
echo '<h3>'.$letter.'</h3>';
}
?>
<li><a href="{path={template_name}/business/{url_title}}">{title}</a> </li>
{/exp:weblog:entries}
</ul>
Category:Tricks Category:Queries
