lundi 2 mars 2015

Get images by category


My current way of getting the images for sliders is by using the following code that works perfectly:



$args = array(
'post_type' => 'attachment',
'sort_order' => 'ASC',
'sort_column' => 'menu_order',
);

$attachments = get_posts($args);

if ($attachments) {
$i = 0;
foreach ($attachments as $attachment) {
$title = $attachment->post_title; // title
if ( strpos($title, 'slide') !== false ) {
$img = wp_get_attachment_url($attachment->ID);
$caption = $attachment->post_excerpt;
//...


But now I have realized for later stuff I will need to categorize images so I registered categories in functions.php and categorised them. Now I am unsure though how to change the code to look for images in the slides category.


I have been working through the codex and found wp_list_categories() do I need to use that at all or is there a parameter in args that I can set?


Any links or help are great. I will continue to look through the codex and let you guys know if I come up with anything.


Edit: Also I have seen some questions on here trying to find attachments of posts. I am not trying to do that. I want to get images directly from the media gallery.





Aucun commentaire:

Enregistrer un commentaire