2011-01-14 4 views
2

WordPress에서 현재 게시중인 태그와 동일한 태그가있는 게시물 목록을 쿼리하려고합니다. 나는 현재 게시물에 대한 태그 목록을 쿼리하여 변수에 전달한 다음 해당 변수를 query_posts 인수로 전달하면 작업이 완료 될 것이라고 생각했습니다. 그것은 게시물의 태그 중 하나를 위해 작동하는 것 같지만 분명히 잘못된 일을하고 있습니다. 내가 잘못 뭘하는지에 대한 어떤 설명이 매우 극명하게 될 것이다Wordpress에서 관련 게시물을 태그로 검색

<?php 
$posttags = get_the_tags(); 
if ($posttags) { 
foreach($posttags as $tag) { 
    $test = ',' . $tag->name; 
} 
} 
query_posts('tag=' .$test . '&showposts=-1'); while (have_posts()) : the_post(); ?> 
     <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p> 
<?php endwhile; wp_reset_query(); ?> 

: 여기에 내가 지금까지 작성한 코드의 샘플입니다.

답변

1

매번 $test을 재설정합니다.

<?php 
$test = ""; 
$posttags = get_the_tags(); 
if ($posttags) { 
foreach($posttags as $tag) { 
    $test .= ',' . $tag->name; 
} 
} 
$test = substr($test, 1); // remove first comma 
query_posts('tag=' .$test . '&showposts=-1'); while (have_posts()) : the_post(); ?> 
     <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p> 
<?php endwhile; wp_reset_query(); ?> 
+2

두 개의 서로 다른 응답을 시도하고 모두 일! 너 둘 다 록 스타 야. 진실하고 온화한 록 스타! 다시 한번 감사드립니다. –

1

당신은 테스트 변수에 태그를 축적 할 필요가

<?php 
$posttags = get_the_tags(); 
$test = ''; 
$sep = ''; 
if ($posttags) { 
    foreach($posttags as $tag) { 
     $test .= $sep . $tag->name; 
     $sep = ","; 
    } 
} 
query_posts('tag=' .$test . '&showposts=-1'); while (have_posts()) : the_post(); ?> 
<p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p> 
<?php endwhile; wp_reset_query(); ?> 
+0

감사합니다. 고마워요, 고마워요. 나는 지금 나를 대적하고 있었던 벽에 지점을 수리 할 수있다! –

관련 문제