0

I have some code that returns the latest 5 images from the WordPress Media Library:

<?php
    $excluded = array(1,35,37);
    $args = array(
        'post_type' => 'attachment',
        'numberposts' => '5',
        'category__not_in' => $excluded
    );
    $images = get_posts($args);
    if (!empty($images)) {
?>
    <div class="wrap">
        <h5><span>Recently</span> Added</h5>
        <ul>
            <?php
                foreach ($images as $image) {
                    $attachment_link = get_attachment_link( $image->ID );
                    echo "<li><a href='".$attachment_link."'>".wp_get_attachment_image($image->ID)."</a></li>";
                }
            ?>
        </ul>
    </div>
<?php } ?>

I'm excluding some categories that I do not want to display (1,35,37). This works, but sometimes I have multiple categories assigned in addition to the excluded categories...if category 1 is selected and category 15 is selected the image will not show because category 1 is selected.

What I am trying to do is exclude the categories only if those categories are selected by themselves. If they are selected with another category that isn't excluded they should show. So, an image that has category 1 and category 12 selected should still show because category 12 is not part of my excluded list.

Can anyone point me in the right direction?

Thanks,
Josh

2 Answers 2

0

Instead of trying to use "excluded categories", I think you should use "included categories".

One solution would be :

  • create an array of the catagories you want to exclude
  • create an array of all the categories
  • use "array_diff" to build an array of the categories to be included

Or maybe you can work with the wordpress function "get_terms" to create the list of the categories to be included.

Hope this help

1
  • Hey there, I found a solution by using my existing code based on your suggestion. Jul 23, 2017 at 22:10
0

This ended up solving it, hat tip to p1erstef for the suggestion of including the categories instead of excluding them.

What I ended up doing was creating a new category called "Pictures", I made all of my other child categories of "Pictures", which means my code becomes much more simplified:

<?php
    $args = array(
        'post_type' => 'attachment',
        'numberposts' => '5',
        'category_name' => 'pictures'
    );
    $images = get_posts($args);
    if (!empty($images)) {
?>
    <div class="wrap">
        <h5><span>Recently</span> Added</h5>
        <ul>
            <?php
                foreach ($images as $image) {
                    $attachment_link = get_attachment_link( $image->ID );
                    echo "<li><a href='".$attachment_link."'>".wp_get_attachment_image($image->ID)."</a></li>";
                }
            ?>
        </ul>
    </div>
<?php } ?>

This is a great solution because it has the behavior I'm looking for, and if I don't want a category included, then I don't make it a child category of "Pictures" and it won't get displayed.

Thanks,
Josh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.