php wordpress 查询 [英] php wordpress query

查看:60
本文介绍了php wordpress 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我早些时候在 Stack Overflow 上发布了这个,但没有得到肯定的结果.我想我应该再做一次.

I had posted this earlier on Stack Overflow, but couldn't get a positive result. I thought I should do this again.

<?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 类的帖子.我认为我做的一切都正确,但仍然无法得到我想要的结果.

I want the above code not to display posts from Category 1. I think I have done everything right, but still I can't get the result that I want.

还有一件事是,如果我什至明确要求查询显示类别 3 或 4 的帖子,它就不会发生.它最终会显示所有类别的帖子.

One more thing is if I even explicitly ask the query to display the posts from category 3 or 4, it doesn't happen. It ends up displaying posts from all categories.

推荐答案

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

You should use the query_posts() function. If not, at least get rid of that $howMany variable and instead append "LIMIT 10" to your sql query.

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

Here is an example of getting a category prepped for the loop:

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

?>

这篇关于php wordpress 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆