0

在 wordpress (3.4) 中,我创建了一些代码,它返回自定义标签的字母列表/索引,并在网格布局中过滤自定义帖子。标签被命名为“tagdirectory”。自定义帖子被命名为“目录”。

这是代码:

<?php $list = ''; 
$tags = get_terms( 'tagdirectory' ); 
echo '<ul id="portfolio-filter">'; 
echo'<li><a href="#all" title="">All</a></li>';
$groups = array();
if( $tags && is_array( $tags ) ) {
foreach( $tags as $tag ) {
$first_letter = strtoupper( $tag->name[0] );
$groups[ $first_letter ][] = $tag;}
if( !empty( $groups ) ) {
foreach( $groups as $letter => $tags ) {
$list .= "\n\t" . '<h2>' . apply_filters( 'the_title', $letter ) .'</h2>';
$list .= "\n\t" . '<ul>';
foreach( $tags as $tag ) {
$lower = strtolower($tag->name);
$name = str_replace(' ', ' ', $tag->name);
$naam = str_replace(' ', '-', $lower);
$list .= "\n\t\t" . '<li><a href="#'.$naam.'">'.$name.'</a></li>';
}}}}else $list .= "\n\t" . '<p>Sorry, but no tags were found</p>';print $list;
echo "</ul>";
?>

这非常有效,但我也希望显示字母表中的空字母。

例如,现在它返回:

一个

艾歇尔奥特

阿佩洛伊格·菲利普

低音扫罗

F

菲茨蒙斯莫林

... 等等

但它不显示空字母组,因为没有以该字母开头的标签。我确实需要它来显示空组的大写字母,如下所示:

一个

艾歇尔奥特

阿佩洛伊格·菲利普

低音扫罗

C

D

F

菲茨蒙斯莫林

G

... 等等

任何人都可以帮助我并告诉我应该添加哪些代码才能使其正常工作吗?

谢谢!

4

1 回答 1

0

我刚刚编辑了这个解决方案> http://wordpress.mcdspot.com/2010/12/03/template-to-list-posts-by-first-letter-of-title/

我通过将第 37 行的“帖子类型”更改为正确的自定义分类法(我用来按名称列出公司的“分销商”)来显示自定义帖子类型,并且成功了。

这是代码:

<?php
/*
Template Name: A-Z Pages

A WordPress template to list page titles by first letter.

You should modify the CSS to suit your theme and place it in its proper file.
Be sure to set the $posts_per_row and $posts_per_page variables.
*/
$posts_per_row = 3;
$posts_per_page = 15;
?>

<?php get_header(); ?>

<style type="text/css">
.letter-group { width: 100%; }
.letter-cell { width: 5%; height: 2em; text-align: center; padding-top: 8px; margin-bottom: 8px; background: #e0e0e0; float: left; }
.row-cells { width: 70%; float: right; margin-right: 180px; }
.title-cell { width: 30%;  float: left; overflow: hidden; margin-bottom: 8px; }
.clear { clear: both; }
</style>

<div id="main-background">

   <div id="main-column">
      <h1><?php the_title(); ?></h1>

      <div class="margin-top"></div>

      <div id="a-z">

         <?php
         $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
         $args = array (
            'posts_per_page' => $posts_per_page,
            'post_type' => 'page',
            'orderby' => 'title',
            'order' => 'ASC',
            'paged' => $paged
         );
         query_posts($args);
         if ( have_posts() ) {
            $in_this_row = 0;
            while ( have_posts() ) {
               the_post();
               $first_letter = strtoupper(substr(apply_filters('the_title',$post->post_title),0,1));
               if ($first_letter != $curr_letter) {
                  if (++$post_count > 1) {
                     end_prev_letter();
                  }
                  start_new_letter($first_letter);
                  $curr_letter = $first_letter;
               }
               if (++$in_this_row > $posts_per_row) {
                  end_prev_row();
                  start_new_row();
                  ++$in_this_row;  // Account for this first post
               } ?>
               <div class="title-cell"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
            <?php }
            end_prev_letter();
            ?>
            <div class="navigation">
               <div class="alignleft"><?php next_posts_link('&laquo; Higher Letters') ?></div>
               <div class="alignright"><?php previous_posts_link('Lower Letters &raquo;') ?></div>
            </div>
         <?php } else {
            echo "<h2>Sorry, no posts were found!</h2>";
         }
         ?>

      </div><!-- End id='a-z' -->

   </div><!-- End class='margin-top -->

</div><!-- End id='rightcolumn' -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

<?php
function end_prev_letter() {
   end_prev_row();
   echo "</div><!-- End of letter-group -->\n";
   echo "<div class='clear'></div>\n";
}
function start_new_letter($letter) {
   echo "<div class='letter-group'>\n";
   echo "\t<div class='letter-cell'>$letter</div>\n";
   start_new_row($letter);
}
function end_prev_row() {
   echo "\t</div><!-- End row-cells -->\n";
}
function start_new_row() {
   global $in_this_row;
   $in_this_row = 0;
   echo "\t<div class='row-cells'>\n";
}

?>
于 2012-09-03T08:27:49.827 回答