Output HappyFiles Folders in Metabox Select-Field

This tutorial is part of a series where I’m gonna show you how to output custom HappyFiles Galleries and Sliders. Part one will cover a little Meta Box Integration, so you can also manage the galleries and sliders inside your Bricks’ single templates.

Bricks 1.5.0 will introduce the possibility to query against the post_type “attachment” which will bring the opportunity to query after HappyFiles folders. That’s the major reason for me to separate this tutorial in different steps as you would still need this step of the tutorial for outputting it dynamically to your single template.

Register the code first

First, you need to copy & paste the provided PHP Snippet to fetch all HappyFiles folders and output them in the correct format for MetaBox.

In case you don’t know where to place that snippet I recommend you to download & install the child theme from your Bricks account and paste the snippet in the functions.php file of it!

<?php
function get_happyfile_folders() {
    $outputChilds = true;
    $args = [
        "taxonomy" => "happyfiles_category",
        //"slug" => "downloads", // get Folder by slug
        "orderby" => "name",
        "order" => "ASC",
        "hide_empty" => false,
    ];

    $folders = get_terms( $args );

    $selectOptions = [];
    foreach ( $folders as $folder ) {
        if ( ! $outputChilds && $folder->parent != 0 ) {
            continue;
        }
        $selectOptions[$folder->term_id] = $folder->name;
    }
    return $selectOptions;
}
?>

From now on you have acces to the function get_happyfile_folders() which basically only looks for all HappyFiles folders and returns them in the needed format.

Parameters ($outputChild)

If you don’t want to have the option to select child folders then you should definitely switch $outputChilds to false.

Add your MetaBox Fields

Next, you have to add your Metabox fields. I normally use MetaBox for CPTs like Job Listings for all the stuff that can be standardized. I’ve developed the snippet above for a customer who sells Fences. So my CPT was about the different fence types and he could choose which HappyFiles gallery he wants to show on which post type and he is also able to easily keep the reference images up to date.

With that being said as a short demo use-case, you’ll need to register your MetaBox Fields. That snippet above works very well with the MetaBox ‘select’ field. But instead of passing your options you would need to provide the callback-function like so:

HF_MB-Integration

In clean words: callback: get_happyfile_folders

Validate the functionality

We haven’t created an output yet, but from now you should be able to choose a folder from your select-dropdown. You can test it out on your posts.

HF_MB-Integration(2)

How to get the corresponding Images/Files on Frontend (preview)

I’m gonna provide the steps on how to query after the Images/Files on Frontend. Feel free to use that code if you are, like me, impatient and can’t wait for the follow-up guides! ๐Ÿ˜‰

function output_happyfiles() {
    // ENTER THE ID OF YOUR METABOX-FIELD HERE:
    $hf_id = rwmb_meta( 'ID-OF-YOUR-MB-FIELD-GOES-HERE' );
    $args = [
        "post_type" => "attachment",
       // "post_mime_type" => "image",
        "post_status" => "inherit",
        "posts_per_page" => -1,
        "tax_query" => [
            [
                "taxonomy" => "happyfiles_category",
                "field" => "ID",
                "terms" => $hf_id,
            ],
        ],
    ];

    $hf_query = new WP_Query( $args );
    
    // you could probably loop here through the resulsts and output the images!
    return $hf_query->posts;
}
Instant access to all 250+ Bricks code tutorials with BricksLabs Pro

Leave the first comment