从自定义字段构建索引页

时间:2012-07-28 作者:101phil

Im starting to delve deeper into Wordpress custom fields and wanting to replicate this page in Wordpress:http://www.clubsact.com.au/corporate/index.php

So my site is not tightly structured to a parent/child relationship, I have created a custom field \'Partner_Level\' and based on the partner level entered by the author the function will write out the Feature Image associated with the post and create my index page.

Though I cant even seem to get my query working within a function. If I follow the instructions on this page:http://css-tricks.com/snippets/wordpress/custom-loop-based-on-custom-fields/ writing straight to my template, all is good. So Im assuming I don\'t have something configured correctly, but at a lose as to what.

Functions.php

function the_partners($cpLevel) {
// This query grabs all fields with Metadata, matches 
 $querydetails = "
   SELECT wposts.*
   FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
   WHERE wposts.ID = wpostmeta.post_id
   AND wpostmeta.meta_key = \'Partner_Level\'
   AND wpostmeta.meta_value = \'$cpLevel\'
   AND wposts.post_status = \'publish\'
   AND wposts.post_type = \'page\'
   ORDER BY wposts.post_date DESC
 "; 

$pageposts = $wpdb->get_results($querydetails, OBJECT);

if ($pageposts) {
    echo "Got em";
}
}

page_corp.php

if (have_posts()) : while (have_posts()) : the_post();
<h1><?php the_title(); ?></h1>

the_partners(\'Gold\');
endwhile; endif; 

In the page, Im simply expecting "Got em" to be printed to the screen, though Im getting a blank.

Cheers,

Phil

2 个回复
SO网友:pcarvalho

我的建议是通过使用get\\u pages()采用另一种方式

例如

$args = array(
         \'meta_key \' => \'Partner_Level\',
         \'meta_value\' => \'Gold\' );
$pageslist = get_pages( $args );
$pageslist应该让您的所有帖子都处于“黄金”级别。

SO网友:101phil

我最终回到了我原来的方法。

添加全局$wpdb;在我的功能中,我能够检索到我需要的信息。

现在一切正常。。希望这对其他人有帮助。

结束

相关推荐

WordPress分页$wpdb->获取结果

由于一个复杂的多站点配置,我有一个查询,它将来自两个博客的帖子组合在一起,我想paginate the results. 我很感激你的帮助。我已经发布了我的查询。 $latestposts = $wpdb->get_results( \" (SELECT * FROM net_5_posts INNER JOIN net_5_term_relationships ON ne