2016-07-19 4 views
0

私はやや複雑なdesignを持っています。Wordpress - wp_queryを通してX投稿ごとに静的画像を含む

私はCPTを使ってそれを出力したいと思います。これはwp_queryに私のCPTある:私は静的IMGを追加し、4つのポスト別の静的および2つの以上のポスト別の静的IMG後の後したい2回の投稿後

/** 
* Management Team Shortcode 
**/ 
function team_query() { 
    $args = array(
     'posts_per_page' => -1, 
     'post_type'  => 'management-team', 
     'order'   => 'DESC', 
    ); 
    $posts = get_posts($args); 
    if (!empty($posts)) { 

     $flag = 0; 
     foreach ($posts as $counter => $p) { 
      $counter++; 

      if ($flag <= 2) { 
       $flag++;     
      } 

      $role = get_field("role"); 
      $name = get_field("team_member_name"); 
      $bio = get_field("bio"); 
      $profile = get_the_post_thumbnail_url($p->ID, 'full' ); 
      $flip = get_field("flip_content"); 

      $html_out = '<article class="team-member">'; 
       // Do stuff with each post here 

      if ($flag % 2 == 0 ) { 
       //add image after second post like 
       $html_out .= '<img src="http://www.ankitdesigns.com/demo/rawafid/wp-content/themes/rawafid-systems/assets/img/mt-1.jpg" alt="Safety Whistle" />'; 
      } 

      if ($counter % 6 == 0) { 
       $flag = 0; 
       //add image after sixth post like 
       $html_out .= '<img src="http://www.ankitdesigns.com/demo/rawafid/wp-content/themes/rawafid-systems/assets/img/mt-2.jpg" alt="Safety Whistle" />'; 
      } 

      $html_out .= '<div class="meta-team"><h6>' . $role . '</h6>' . '<h4>' . $name . '</h4>' . '<p>' . $bio . '</p></div>'; 
      $html_out .= '</article>'; 
     } 
    } else { 
     // No results 
     $html_out = 'No Management Team Members Found.'; 
    } 

    return $html_out; 
} 
add_shortcode('show_management_team', 'team_query'); 

など

私は」より良いアプローチで提案に公開する。

私は別の方法を考えようとしています。たぶんビジュアルコンポーザーを使ってグリッドを構築していますか?

+0

ねえダレンは、表示されますどのように多くの静止画像、明らかにすることができるだろうか? 私が理解する限り、2つの投稿の後に静的な画像があるでしょうか? または4番目の投稿の後に画像が変更されていますか? ありがとうございました –

+0

両方。それは2、4、2、4、2、4などになります。これは私が知っている奇妙に聞こえる。これはデザインが[link](http://imgur.com/a/uGKh4)のようなものです –

答えて

0

ここでは論理があります。私の機能を変更してコード内に実装できると思います。

/** 
* Management Team Shortcode 
**/ 
function team_query() { 
    $args = array(
     'posts_per_page' => -1, 
     'post_type'  => 'management-team', 
     'order'   => 'ASC', 
    ); 
    $posts = get_posts($args); 
    if (!empty($posts)) { 

     $flag = 0; 
     foreach ($posts as $counter => $p) { 
      $counter++; 

      if ($flag <= 2) { 
       $flag++;     
      } 

      $title = get_the_title($p->ID); 
      $link = get_the_permalink($p->ID); 
      $featured_img = get_the_post_thumbnail_url($p->ID, 'full' ); 

      $html_out = '<article class="recent-project" style="background: url(' . $featured_img . ') no-repeat center center; background-size: cover;">'; 
       // Do stuff with each post here 

      if ($flag % 2 == 0 ) { 
       //add image after second post like 
       // $html .= <img src="css/images/myimage1.png" alt="" /> 
      } 

      if ($counter % 6 == 0) { 
       $flag = 0; 
       //add image after sixth post like 
       // $html .= <img src="css/images/myimage2.png" alt="" /> 
      } 

      $html_out .= '<div class="container"><div class="meta-project"><h6>Latest Project</h6>' . '<h2>' . $title . '</h2>' . '<a class="btn btn-lg btn-tertiary" href="' . $link . '">' . 'Discover' . '</a></div></div>'; 
      $html_out .= '</article>'; 
     } 
    } else { 
     // No results 
     echo "Nothing to show"; 
    } 

    return $html_out; 
} 

乾杯:)

+0

私は 'team_query'でどの' if'文を追加し始めますか? –

+0

私のコードを変更しました。 ifsのコメントは画像に置き換えてください。あなたが何か質問があれば教えてください:) –

+0

ねえ、コンテンツが出力されていません。私は 'Undefined variable:html'を取得していますが、' $ html'ではなく '$ html_out'を意味しましたか? –

関連する問題