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' ); ?>
$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