add exception on var (alphabetic listing on wordpress)

I’m doing an alphabetical listing of post titles and i add the initial letter on top of each list.
Example: if i have 3 articles in my category named respectively

Hello World
The first article
Welcome to the page

I make them appear like this :

H
Hello World

T

The first article

W
Welcome to the page

This is the working code i use:

[php] <?php
get_header();
?>


    <?php dynamic_sidebar( 'sidebar-4' ); ?>
<?php
$args=array(
'cat' => get_query_var('cat'),
  'orderby' => 'date',
  'order' => 'DESC',
  'posts_per_page'=>-1,
  'caller_get_posts'=>1
);
$my_query = new WP_Query($args);


if( $my_query->have_posts() ) {


	if
	 ($my_query->have_posts()) : $my_query->the_post();
 ?><div id="lt-title"><?php 
$category = get_the_category(); 
if($category[0]){
echo ''.$category[0]->cat_name.'';
}
?></div><?php

endif;  
?><div id="lt" class="LTclass"><?php 
 while 
 
 ($my_query->have_posts()) : $my_query->the_post();
    $this_char = strtoupper(substr($post->post_title,0,1));


  if ($this_char != $last_char) {
       $last_ord = ord($last_char);
       while ( $last_char >= 'A' && (ord($this_char) - $last_ord) > 1) {
          ++$last_ord;
          $last_char = chr($last_ord);
          $pfx_date = get_the_date( $d );
		  
		  echo '<div id="ladatea"><a name="'.$last_char.'"></a></div>';

       }
       $last_char = $this_char;
      echo '<a name="'.get_the_date().'"></a><div id="ladatea">'.get_the_date().'</div>';
    } ?>
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a><br/>
    					<?php echo get_post_meta($post->ID, 'description', true); ?>

    </p>
    <?php
  endwhile;
}
?></div><?php


wp_reset_query();
get_footer();
?>[/php]

Now, i wanted to improve my code, adding a rule.
I wanted to create an exception, so the article called “The first article” would not appear under the letter T but under the letter F, which is the first letter of the second word.

F
The first article

I’ve started to code this part but i’m now stuck.
This is the idea of what i was trying to do :

[php] $art_title = $post->post_title ;
$ifstartswiththe = explode(" ", $art_title);

if ($ifstartswiththe[0] = 'The') { /* if the first part of the title contains the */
$ifstartswiththe[0] = $ifstartswiththe[1]; /* then select the 2nd word */
$this_char = strtoupper(substr($ifstartswiththe[0],0,1)); /* set this_char with the first letter of the second word */
}[/php]

but now it seems that $this_char is set twice. Can someone show me how could it be done?
I thank you in advance.

Dan

How about something like this? You can use array referencing on strings, so $string[0] will give the first letter.

[php]$titleTrimmed = ltrim(strtoupper($art_title), 'THE ');
$this_char = $titleTrimmed[0];[/php]

From PHP 5.4 you could do it in one line, since you can reference directly on functions.
[php]$this_char = ltrim(strtoupper($art_title), 'THE ')[0];[/php]

Hm, reading this again that might not have been your question :stuck_out_tongue:

Post your working code (as it is now), you need add the articles / sort letters to an array, then sort the array by the sort letters, then iterate trough the array printing the articles.

Here is the solution

[php] while

 ($my_query->have_posts()) : $my_query->the_post();
 $excluded_words=array('la','le', 'les');
 $words = explode(" ",$post->post_title);
 if(in_array(strtolower($words[0]),$excluded_words)){
 $this_char = strtoupper(substr($words[1],0,1));
 }
 else{
   $this_char = strtoupper(substr($post->post_title,0,1));
 }

[/php]
Thx to my friend Driss Bennani !

Not trying to be an ass, but how/why is that better than my solution?

well i think the difference is if the title is : ‘Therefore’ then it will be considered as “the” and the article will be put in R (following letter after ‘the’)

Sponsor our Newsletter | Privacy Policy | Terms of Service