繁体   English   中英

按自定义字段中的值对帖子进行分组 - 添加固定链接以发布标题并显示在 2 列中

[英]Group posts by values from a custom field - Add permalink to post titles and display in 2 columns

我试图完成按字母顺序和 ACF“省”列出的成员学校列表(职位类型)。 我还想在 2 列中显示列表。

省值1

  • 帖子 1(包括帖子的永久链接)
  • 帖子 2(包括帖子的永久链接)

省值2

  • 帖子 3(包括帖子的永久链接)
  • 帖子 4(包括帖子的永久链接)

ETC.........

我一直以这个https://stackoverflow.com/a/72384518/20940105为例。 我在将列表拆分为 2 列并将永久链接添加到帖子列表时遇到问题。

任何人都可以提供一些方向吗?

这是我的代码:

                    $field_posts = array();

                    $args = array(
                        'post_type'         => 'cicancorp_member',
                        'post_status'       => 'publish',
                        'posts_per_page'    => 200,
                        'meta_key'          => 'member_province',
                        'orderby' => array( 
                           'meta_value' => 'ASC',
                           'title'      => 'ASC'
                        ) 
                    );

                    $query = new WP_Query($args);
                    
                    // Number of members to split list into 2 columns
                    $count_posts = wp_count_posts( 'cicancorp_member' )->publish;
                    
                    echo '<div class="facetwp-content">';
                    echo '<div class="row"><div class="col-md-6">';

                    while ( $query->have_posts() ) {
                        $query->the_post();

                        $title = get_the_title();
                        $value = get_field_object('member_province');
                        $permaURL = get_permalink();
                        $field = $value['value'];
                        $field_posts[$field][$title][$permaURL] = $post;

                    }

                    foreach($field_posts as $field_post => $field_title) {

                        setup_postdata( $post );
                        
                        echo '<h2 class="member-location-heading"><strong>' . $field_post . '</strong></h2>';
                        echo '<ul class="ms-0">';

                        foreach($field_title as $post_listing => $listing) {

                            echo '<li><a href="' . $permaURL . '">' . $post_listing . '</a></li>';
                        }

                        echo '</ul>';

                    } wp_reset_postdata();

                    echo '</div></div></div>';

我想我找到了解决方案 - 获取 ID


$field_posts = array();
    
    echo '<div class="facetwp-content">';
    echo '<div class="row"><div class="col-md-6">';

    while ( $query->have_posts() ) {
        $query->the_post();

        $id                             = get_the_id();
        $value                          = get_field_object('member_province');
        $province                       = $value['value'];
        $field_posts[$province][$id]    = $post;
        $count                          = 0;
        $count_posts                    = $query->found_posts;
        $count_split                    = round($count_posts/2);
    }
    
    foreach($field_posts as $field_province => $field_ids) {

        setup_postdata( $post );
        
        if ($count == $count_split && $count_posts < 20) {
            echo '</div><div class="col-md-6">';
        }
        
        echo '<h2 class="member-location-heading mt-4 mb-2">' . __($field_province, 'cicancorp') . '</h2>';
        echo '<ul class="ms-0">';

            foreach($field_ids as $field_id) {
                echo '<li><a href="' . get_permalink($field_id) . '">' . get_the_title($field_id) . '</a></li>';
                if ($count == $count_split) {
                    echo '</ul></div>';
                    echo '<div class="col-md-6">';
                    echo '<h2 class="member-location-heading mt-4 mb-2">' . __($field_province, 'cicancorp') . '</h2>';
                    echo '<ul class="ms-0">';
                }
                $count++;
            }

        echo '</ul>';
        
    } wp_reset_postdata();

    echo '</div></div></div>';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM