2011-11-03 64 views
0

我已经在Wordpress中设置了自定义发布类型。这是客户可以上传PDF格式文件的部分。这些文件分为两类 - “可下载的表格”和“菜单”;该类别的自定义字段名称是'document_category'WordPress的 - 按邮政类型查询类别抓取项目

我想运行查询,并只显示页面上的'可下载的表单'。这里是我通常使用的代码---我希望有人可以帮我添加我需要的东西使其工作?

<?php  
$args = array('post_type' => 'prep_forms', 'posts_per_page' => -1); 
// The Query 
$the_query = new WP_Query($args); 
// The Loop 
$i = 0; 
while ($the_query->have_posts()) : $the_query->the_post(); 
?> 
<li><a href="<?php echo get_field('document_pdf_file'); ?>"><?php the_title(); ?></a></li> 
<?php 
$i++; 
endwhile; 
// Reset Post Data 
wp_reset_postdata(); 
?> 

谢谢。

回答

0
<?php  
$args = array('post_type' => 'prep_forms', 'posts_per_page' => -1); 
// The Query 
$the_query = new WP_Query($args); 
// The Loop 
$i = 0; 
while ($the_query->have_posts()) : 
    $the_query->the_post(); 
    // Get all Advanced custom fields 
    $my_custom_fields = get_fields(get_the_ID()); 
    // Does document_category field exists and is it equal with 'Downloadable Forms'? 
    if(isset($my_custom_fields['document_category']) && $my_custom_fields['document_category'] == 'Downloadable Forms'): 
?> 
<li><a href="<?php echo get_field('document_pdf_file'); ?>"><?php the_title(); ?></a></li> 
<?php 
    endif; 
$i++; 
endwhile; 
// Reset Post Data 
wp_reset_postdata(); 
?> 

我已经使用了先进的自定义字段的插件,返回所有职位自定义字段的数组的get_fields($post_id = false)功能,然后过滤它来满足您的需求。

希望我已经帮助

+0

再次救了我。谢谢Kostis。 –

+0

所以它工作,很好:-) – Kostis

0

那么,你可以使用is_category();内环路,只显示那些帖子,像这样(使用相同的代码):

<?php  
$args = array('post_type' => 'prep_forms', 'posts_per_page' => -1); 
// The Query 
$the_query = new WP_Query($args); 
// The Loop 
$i = 0; 
while ($the_query->have_posts()) : $the_query->the_post(); 

if(is_category('Sownloadable Forms')){ // here the condition 

?> 
<li><a href="<?php echo get_field('document_pdf_file'); ?>"><?php the_title(); ?></a></li> 
<?php 
$i++; 
} // here ends the IF statment 
endwhile; 
// Reset Post Data 
wp_reset_postdata(); 
?> 

更妙的是:http://codex.wordpress.org/Function_Reference/is_category

希望有所帮助。

+0

这是唯一的问题 - 它不是一个WordPress的类别。我在高级自定义字段中设置了一个自定义字段,用户在上传PDF时选择一个类别。 –

0

你为什么不让它更简单,并设置基于自定义字段数据的查询?

<?php 
$count = 1; 
$args = array(
    'post_type' => 'any', 
    'posts_per_page' => 4, 
    'meta_key' => 'display', 
    'meta_value' => 'true' 
); 

$query = new WP_Query();$query->query($args); 
while ($query->have_posts()) : $query->the_post();      
$do_not_duplicate[] = $post->ID; 
?> 
    <!-- query content --> 
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(esc_attr__('Permalink to %s', 'advanced'), the_title_attribute('echo=0')); ?>" ></h2> 
    <?php the_excerpt() ;?> 
    <!-- query content --> 
<?php $count++; endwhile; wp_reset_query(); ?> 

这样与自定义字段键显示和自定义字段值的任何职位真正将您的查询显示。

相关问题