Wordpress - 在帖子类型查询中按类别抓取项目

发布于 2023-02-17 04:14:19 字数 662 浏览 23 评论 0原文

我在 Wordpress 中设置了自定义帖子类型。 这是一个客户可以上传 PDF 格式文件的部分。 这些文件分为两类——“可下载表格”和“菜单”; 该类别的自定义字段名称是“document_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();
?>
<li><a href="<?php echo get_field('document_pdf_file'); ?>"><?php the_title(); ?></a></li>
<?php
$i++;
endwhile;
// Reset Post Data
wp_reset_postdata();
?>

谢谢。

I have setup a custom post type in Wordpress. It is a section where the client can upload documents in PDF format. The documents are divided into two categories - 'Downloadable Forms' and 'Menu' ; the Custom Field name for the category is 'document_category'

I am trying to run a query and only display the 'Downloadable Forms' on a page. Here is the code I normally use --- I'm hoping someone can help me add what I need to make it work?

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

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雄赳赳气昂昂 2023-02-24 04:14:19
<?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();
?>

我使用了 Advanced Custom Fields 插件的 get_fields($post_id = false) 函数,它返回所有帖子的自定义字段的数组,然后过滤它以满足您的需要。

希望我有帮助

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

I've used the get_fields($post_id = false) function of the Advanced Custom Fields plugin that returns an array of all the posts' custom fields and then filtered it to match your needs.

Hope i've helped

电影里的梦 2023-02-24 04:14:19

好吧,你可以使用 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

希望对您有所帮助。

Well, you can use the is_category(); inside loop and only display those posts, like so (using your same code):

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

Better yet: http://codex.wordpress.org/Function_Reference/is_category

Hope that helps.

万水千山粽是情ミ 2023-02-24 04:14:19

为什么不简单点,设置一个基于自定义字段数据的查询呢?

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

这样,任何带有自定义字段键 display 和自定义字段值 true 的帖子都将显示在您的查询中。

Why dont you make it simpler and set a query based on custom field data?

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

That way any post with custom field key display and custom field value true will be shown on your query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击“接受”或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文