Rumah  >  Soal Jawab  >  teks badan

Cara membuat tatasusunan bersekutu menggunakan gelung foreach

<p><br /></p> <pre class="brush:php;toolbar:false;">$featured_posts = get_field('parts', $postId->ID); jika( $featured_posts ): foreach( $featured_posts sebagai $post ): setup_postdata($post); $permalink = get_permalink( $part->ID ); $title = get_the_title( $part->ID ); //membuat tatasusunan $part_pages = tatasusunan( $permalink => ); endforeach; wp_reset_postdata(); endif;</pre> <p>Saya cuba mencipta tatasusunan bersekutu menggunakan gelung foreach, tetapi saya mendapat ralat. Sebarang bantuan amatlah dihargai. </p>
P粉381463780P粉381463780401 hari yang lalu407

membalas semua(1)saya akan balas

  • P粉359850827

    P粉3598508272023-08-17 00:34:02

    Untuk menetapkan data yang diperlukan dalam tatasusunan bersekutu, cuba yang berikut (beranotasi):

    $featured_posts = get_field('parts', $postId->ID);
    
    if( $featured_posts ):
        // 在foreach循环之前始终初始化数组变量
        $part_pages = array(); 
    
        foreach( $featured_posts as $post ): 
    
            setup_postdata($post); 
            $permalink = get_permalink( $post->ID );
            $title = get_the_title( $post->ID );
    
            // 将链接设置为键,标题设置为值
            $part_pages[$permalink] = $title;
    
        endforeach; 
    
        wp_reset_postdata();
    
        // 测试输出
        echo '<pre>' . print_r($part_pages, true) . '</pre>';
        
    endif;
    

    Nota: $partPembolehubah tidak ditentukan.

    balas
    0
  • Batalbalas