我正在列出我的自定义帖子类型“ Person”的所有帖子,按照自定义字段的字母顺序排序 last_name 在页面上。

在字母范围开始之前,我将如何插入分隔器(例如字母的图像)?

这是我想做的:

alphabetical list with dividers

更新:
这是我正在使用的代码:

<ul class="list-ensemble">
<?php query_posts('post_type=person&post_status=publish&meta_key=last_name&orderby=meta_value&order=ASC'); 
if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <li data-id="<?php the_ID(); ?>">
        <a href="<?php the_permalink(); ?>" class="ensemble-single-link">
            <?php if ( has_post_thumbnail() ) { the_post_thumbnail(thumbnail); } ?>
        </a>
    </li>
<?php endwhile; // end of the loop. ?>
</ul>
有帮助吗?

解决方案

尝试这个:

<ul class="list-ensemble">
<?php query_posts('post_type=person&post_status=publish&meta_key=last_name&orderby=meta_value&order=ASC'); 
$current_letter = '';
if ( have_posts() ) while ( have_posts() ) : the_post();
    $last_name = get_post_meta( $post->ID, 'last_name', true );
    $letter = strtolower( substr( $last_name, 0, 1 ) );
    if ( $letter != $current_letter ) {
        $current_letter = $letter; ?>
        <li class="letter">
            <img src="<?php echo $letter; ?>.jpg" alt="<?php echo $letter; ?>" title="<?php echo $letter; ?>">
        </li>
    <?php } ?>
    <li data-id="<?php the_ID(); ?>">
        <a href="<?php the_permalink(); ?>" class="ensemble-single-link">
            <?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'thumbnail' ); } ?>
        </a>
    </li>
<?php endwhile; // end of the loop. ?>
</ul>

对于循环中的每个帖子,它都会检索 last_name PostMeta字段(这不会在页面上添加任何查询,因为WordPress缓存了PostMeta),然后检查其第一个字母。如果是一个新字母,它将输出一个列表元素,其中包含字母命名的图像(例如 f.jpg).

其他提示

好吧,由于WordPress的排序功能不包括这种功能,因此您可能应该问Matt Mullenweg ...哈哈...

不,但实际上,您可以为每个字母使用query_posts()帖子元的第一个字母符合相应的字母。如果没有匹配该字母的帖子,则只需跳过该字母即可。

您目前只有一个循环。您可能必须编写一个for()循环,该循环又将生成每个WordPress循环。

这样的东西(只是一个粗略的草稿):

<ul class="list-ensemble">
    <? for ($i=65; $i<91; $i++) : // 65 through 90 represent the uppercase alphabet
        query_posts('post_type=person&post_status=publish&meta_key=last_name&orderby=meta_value&order=ASC'); 
        //PUT SOME CODE HERE TO CHECK IF THE FIRST LETTER IS EQUAL TO $i
        //Set some variable $letter_matches == true if the letter matches.
    ?>
        <h3 class="letter">
            <?php //ECHO THE LETTER CORRESPONDING TO $i HERE ?>
        </h3>
        <?
        if ( have_posts() and $letter_matches ) while ( have_posts() ) : the_post(); ?>
            <li data-id="<?php the_ID(); ?>">
                <a href="<?php the_permalink(); ?>" class="ensemble-single-link">
                    <?php if ( has_post_thumbnail() ) { the_post_thumbnail(thumbnail); } ?>
                </a>
            </li>
        <?php endwhile; // end of the loop. ?>
<?endfor; ?>
</ul>

因此,从本质上讲,您将在页面中最多有26个WordPress循环。我不确定服务器上的紧张性如何,但应该起作用。这是我想到的第一件事。 Lemme知道进展如何!

许可以下: CC-BY-SA归因
scroll top