I'm trying to make the following mock up with WordPress: .jpg
I created a custom post type and I created two categories (general and filling instructions). I used ACF plugin to create custom fields for the post type: .jpg
However, when I create the loop I can't get it to show posts for only the general category, it will show any category. Also, it only shows one post (most recent), I want it to show all of them for the one category.
<ul class="pdfLinks">
<?php
//The Arguments
$args = array(
'posts_per_page' => 50,
'post_type' => 'documents',
'category_name' => 'general'
);
//The Query
$genral_documents = new WP_Query ( $args ); ?>
<?php
//If we have the posts...
if (the_field ('title')) :
while(the_field('title')) :
the_field('title');
endwhile;
endif;
?>
<?php
$pdf1 = get_field('pdf_1');
?>
<a class="download_button" target="_blank" href="<?php echo $pdf1['url']; ?>">
<li>
Download File
</li>
</a>
</ul>
I'm very new to coding for WordPress, so I'm not sure what I'm doing wrong. Thank you for your help :)
I'm trying to make the following mock up with WordPress: https://i.sstatic.net/wyYS1.jpg
I created a custom post type and I created two categories (general and filling instructions). I used ACF plugin to create custom fields for the post type: https://i.sstatic.net/xBn3S.jpg
However, when I create the loop I can't get it to show posts for only the general category, it will show any category. Also, it only shows one post (most recent), I want it to show all of them for the one category.
<ul class="pdfLinks">
<?php
//The Arguments
$args = array(
'posts_per_page' => 50,
'post_type' => 'documents',
'category_name' => 'general'
);
//The Query
$genral_documents = new WP_Query ( $args ); ?>
<?php
//If we have the posts...
if (the_field ('title')) :
while(the_field('title')) :
the_field('title');
endwhile;
endif;
?>
<?php
$pdf1 = get_field('pdf_1');
?>
<a class="download_button" target="_blank" href="<?php echo $pdf1['url']; ?>">
<li>
Download File
</li>
</a>
</ul>
I'm very new to coding for WordPress, so I'm not sure what I'm doing wrong. Thank you for your help :)
Try this:
`
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$genral_args = array(
'posts_per_page' => '50',
'post_type' => 'documents',
'cat' => 'your-category-id',
'paged' => $paged
);
$genral_query = new WP_Query( $genral_args );
while ( $genral_query->have_posts() ) : $genral_query->the_post(); ?>
<?php
//If we have the posts...
if (the_field ('title')) :
while(the_field('title')) :
the_field('title');
endwhile;
endif;
?>
<?php
$pdf1 = get_field('pdf_1');
?>
<a class="download_button" target="_blank" href="<?php echo $pdf1['url']; ?>">
<li>
Download File
</li>
</a>
<?php endwhile; wp_reset_postdata(); ?>
</ul> `
(change the "your-category-id" to id, or if you want choose more category, use thus: 'cat' => 'your-category-id, your-category-id2'
)
Try This:
<?php
$taxonomy="category";
$postData = new WP_Query(
array(
'post_type' => 'documents',
'posts_per_page'=> 50,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => $taxonomy, // your custom taxonomy name here
'field' => 'slug', // you can specify field like id,name and slug.
'terms' => 'general' // you can specify category id, name and slug
)
)
));
if($postData->have_posts()):
while ($postData->have_posts()): $postData->the_post();
echo the_title()."<br>";
$pdf1 = get_field('pdf_1'); ?>
<a class="download_button" target="_blank" href="<?php echo $pdf1['url']; ?>">
<li>
Download File
</li>
</a>
<?php endwhile;
wp_reset_postdata();
endif; ?>
Thanks
category_*
parameters only work for the built-in Categories ( used for posts ). I'm assuming you have a Custom Taxonomy and thus need to use atax_query
. – Howdy_McGee ♦ Commented Jan 31, 2018 at 21:33meta_*
properties of WP_Query. You also have Term ID as the selected value so you won't be able to use the slug. – Howdy_McGee ♦ Commented Jan 31, 2018 at 21:38