wordpress 根据自定义字段中的值对帖子进行分组-将永久链接添加到帖子标题并显示在2列中

hrirmatl  于 2023-01-25  发布在  WordPress
关注(0)|答案(1)|浏览(122)

我试图完成这个成员学校(职位类型)按字母顺序和ACF "省"列出的列表。我也想显示在2列列表。
省值1

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

省值2

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

等等......
我一直在用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>';
wfsdck30

wfsdck301#

我想我找到解决办法了-把身份证拿走

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

相关问题