2017-01-04 9 views
-3

私はループ内のループで、次のループは動的で、現在のページタイトル `get_the_title();を使用しています。ループ内のWordpressループ

問題は私にはありません。元のループのget_the_title();が2番目のループで上書きされているためです。

<?php if (have_posts()) : ?> 

<?php while (have_posts()) : the_post(); ?> 

<?php if(get_field('artist_photo')) { $image=g et_field('artist_photo'); } ?> 

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 

    <header> 
    <h1><?php the_title(); ?></h1> 
    <hr /> 
    </header> 

    <div class="content"> 

    <?php if(!empty($image)) { echo "<img src='" . $image[ 'url'] . "' alt='" . $image[ 'alt'] . "' class='featured' />"; } ?> 

    <?php the_content(); ?> 

    <hr /> 
    <h2>Artwork for Sale</h2> 
    <?php $args=a rray('post_type'=>'artwork', 'meta_key' => 'artist', 'meta_value' => the_title()); $query = new WP_Query($args); ?> 
    <?php if ($query->have_posts()) : ?> 

    <ul id="related-artwork" class="row"> 

     <?php while ($query->have_posts()) : $query->the_post(); ?> 

     <li class="col-sm-3"> 

     <?php if(get_field('image')) { $image=g et_field('image'); } ?> 
     <?php if(!empty($image)) { echo "<img src='" . $image[ 'url'] . "' alt='" . $image[ 'alt'] . "' />"; } ?> 

     <div class="info"> 
      <h3><?php the_title(); ?></h3> 
     </div> 

     </li> 


     <?php endwhile; ?> 

     <?php wp_reset_query(); ?> 

    </ul> 
    <?php else : ?> 

    <?php endif; ?> 


    </div> 

    <footer> 
    </footer> 

</article> 

<?php endwhile; ?> 

<?php wp_reset_query(); ?> 

<?php else : ?> 

<?php endif; ?> 
+0

エラーがありますか? –

+0

以下の2つの回答は正しいですが、この質問はここには含まれません。それは[wordpress stack exchange](http://wordpress.stackexchange.com/)に属します。 – CaldwellYSR

答えて

1

the_title()が返す値ではなく、get_the_title()をエコーので。私は

$args = array(
'post_type'  => 'artwork', 
'meta_key'  => 'artist', 
'meta_value' => the_title() 
    ); 

の主な問題だと思います

したがって、 'meta_value' => the_title()行を 'meta_value' => get_the_title()に置き換えてください。

また、2番目のループでは、$ the_query-> the_post()を呼び出してください。 whileループ内で、スイッチを作成します。以下のような

何か:

<ul id="related-artwork" class="row"> 

<?php while ($query->have_posts()) : $query->the_post(); ?> 

$the_query->the_post(); 

    <li class="col-sm-3"> 
    ..... 

二つの編集は私が思うトリックを行う必要があります。他のすべてはうまくいくようです。

0

wp_reset_query()の代わりにwp_reset_postdata()を使用してください。これらは、メインクエリオブジェクトを変更しないようwp_reset_queryを呼び出す

はWP_Queryまたは get_postsを使用した後は必要ありません。代わりに を使用してください。wp_reset_postdata