2009-09-06 8 views
1

이전에 스택 오버플로에 게시했지만 긍정적 인 결과를 얻을 수 없었습니다. 나는 이것을 다시해야한다고 생각했다. PHP wordpress query

<?php require_once 'news/wp-config.php'; 
$howMany = 0; 
$query ="SELECT `ID`, `post_title`,'post_category', `guid`,SUBSTRING_INDEX(`post_content`, ' ', 100) AS `post_excerpt` FROM $wpdb->posts WHERE `post_status`= \"publish\" AND `post_type` = \"post\" AND post_category != \"1\" "; 
$posts = $wpdb->get_results($query); 
$posts = array_reverse($posts); 
foreach($posts as $post) 
{ 
    if($howmany<10) 
    { 
     $link = $post->guid; 
     echo "<li><a target='_blank' href='$link'>$post->post_title</a></li>"; 
     $howmany++; 
    } 

}     
?> 

나는 내가 바로 모든 짓을 생각 종류 1. 게시물을 표시하지 않도록 위의 코드를 원하지만, 여전히 내가 원하는 결과를 얻을 수 없습니다.

카테고리 3 또는 4의 게시물을 표시하도록 쿼리에 명시 적으로 요청하는 경우 한 가지 더 있습니다. 모든 카테고리의 게시물을 표시합니다.

+0

은'char' 타입의 당신의 카테고리 id인가? 나는 그것이 어떤 종류의'int'이어야한다고 생각합니다. – SilentGhost

+0

그리고 당신은 이것을 다시하지 않았습니다. 문제를 풀려고하면 원래 질문을 업데이트하는 편이 낫습니다. – SilentGhost

+0

죄송합니다. 그러나 그것은 적당한 응답을 얻는 것을 wasnt한다. 나는 이것에 정말로 붙어있다. 원래 게시물은 한 달 전쯤에 만들어졌습니다 ... – amit

답변

2

query_posts() 기능을 사용해야합니다. 그렇지 않다면 적어도 $ howMany 변수를 없애고 대신 "LIMIT 10"을 SQL 쿼리에 추가하십시오. I는 두 번째 워드 함수를 이용한 것이다

<?php 
    // Borrowed heavily from link above... 
    $categoryvariable=1; // assign the variable as current category 
    $numposts = 10; 
    // Set up the query 
    $query= 'cat=' . $categoryvariable. '&orderby=date&order=ASC&showposts='.$numposts;  
    query_posts($query); // run the query 

//The Loop 
if (have_posts()) : while (have_posts()) : the_post(); 
    // Do all your echo stuff here 
endwhile; else: 

endif; 

//Reset Query 
wp_reset_query(); 

?> 
+0

제발 어떻게 보여주십시오 ...? – amit

+0

고마워요. 당신은 많은 도움을주었습니다. – amit

1

: 여기

는 루프 수험 공부 범주를 얻는 예이다.

여기에게 유용한 리소스의 http://codex.wordpress.org/Function_Reference/query_posts

정확한 구문 query_posts 것 ('category__not_in = 1');

나는 당신이 카테고리 1에서 그것을 원하지 않는다고 생각한다고 생각한다.