【问题标题】:php wordpress queryphp wordpress 查询
【发布时间】:2009-09-06 16:39:11
【问题描述】:

我之前曾在 Stack Overflow 上发布过此内容,但没有得到肯定的结果。我想我应该再做一次。

<?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 的帖子,它也不会发生。它最终会显示所有类别的帖子。

【问题讨论】:

  • 您的类别 ID 是 char 类型吗?我想它应该是某种int
  • 你不应该再这样做了。如果您更新原始问题并尝试解决它会更好。
  • 我很抱歉。但它没有得到正确的回应。我真的坚持这一点。原来的帖子是大约一个月前发布的......

标签: php sql mysql wordpress


【解决方案1】:

您应该使用query_posts() 函数。如果不是,至少去掉那个 $howMany 变量,而是将“LIMIT 10”附加到你的 sql 查询中。

以下是为循环准备类别的示例:

<?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();

?>

【讨论】:

    【解决方案2】:

    我会第二次使用 wordpress 功能。

    这是一个很棒的资源http://codex.wordpress.org/Function_Reference/query_posts

    正确的语法是 query_posts('category__not_in=1');

    我想你说过你不希望它在第 1 类中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多