Hi,
The best way to ignore the first character is to use the a_z_listing_item_index_letter
filter. This can be added to your theme or child-theme’s functions.php
and will ignore any non-alphabetical letter:
add_filter( 'a_z_listing_item_index_letter', 'ignore_a_z_non_alphabetical_letters_at_start_of_title', 10, 3 );
function ignore_a_z_non_alphabetical_letters_at_start_of_title( $index_letters, $item, $type ) {
// if we already have an alphabetical letter, return it immediately
if ( preg_match( '/^[A-Za-z]$/', $index_letters[0] ) ) {
retrun $index_letters;
}
// loop through the title letters and return the first alphabetical letter
for ( $i = 0; $i < mb_strlen( $item->title ); $i++ ) {
$letter = mb_substr( $i, 1 );
if ( preg_match( '/^[A-Za-z]$/' $letter ) ) {
return array( $letter );
}
}
// if we get here, then there are no alphabetical letters in the title so return the original index
return $index_letters;
}
-
This reply was modified 4 years ago by Dani Llewellyn. Reason: add missing regex `//` characters
You can allow numbers to be separated by changing both instances of /^[A-Za-z]$/
to /^[A-Za-z0-9]$/
.
Thread Starter
dknut
(@dknut)
Okay, thanks you, Daniel. I’ll try it. But need some time for that.
Thread Starter
dknut
(@dknut)
Hi Daniel,
there is something wrong in this code. I’m not a developer but I think it is in the line
for ( $i = 0; $i < mb_strlen( $item->title ); $i++ ) {
Thanks.
dknut
-
This reply was modified 4 years ago by dknut.
You’re quite right, I think the $item->title
should be $item->post_title
.
Thread Starter
dknut
(@dknut)
Did not work. If I add above filter to my theme the whole site will be dead until I remove it.
When the site dies there should be some logged errors in a file on your server somewhere. This is likely called “Error Log” or “error.log” or “error_log”. It is possible that the functions.php
file had an error with your changes that caused the site to stop, which the error log would help us to isolate. I suspect I probably made a mistake in the code sample above.
Thread Starter
dknut
(@dknut)
Thanks, @diddledan ! Its not necessary to waste more time on this. Its not so important to use it. It was nice but no more.