Pass in string to reusable php function

I don’t use php very much, so – my actual knowledge is centered around require() etc – for templates – and when needed, WordPress loops.

In a current project, I had to use this block of code 10 times on a page.

Read More
<?php
    $commercialLoop = new WP_Query(
        array(
            'post_type' => 'project',
            'posts_per_page' => -1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'project-type',
                    'field' => 'slug',
                    'terms' => 'commercial'
                )
            )
        )
    );
?>

<?php while ( $commercialLoop->have_posts() ) : $commercialLoop->the_post(); ?>

    <li class='project'>
        <a href='<?php the_permalink(); ?>'>
            <div class='image-w'>
                <?php if ( has_post_thumbnail() ) { ?>
                    <?php the_post_thumbnail(); ?>
                <?php } else { ?>
                    <img src='http://placehold.it/1600x800&text=No featured image yet' alt=''>
                <?php } ?>
            </div>
            <h3 class='project-name'><?php echo get_the_title(); ?></h3>
        </a>
    </li>

<?php endwhile; wp_reset_query(); ?>

In JavaScript I would just pass a few strings into a function – and then I could use that. In this case, the only thing that changes for each loop is commercialLoop and the `commercial’ – and I think that is all that changes… So, really just one string is all that I would need to pass in for each different category, ‘residencial’, ‘housing’, ‘details’ etc… I would expect I could do something like:

<?php getLoopThing('commercial'); ?>

BUT – I must not know the right search terms to find direction.

HELP???

================================================

This is what I ended up doing:

Created a partial : taxonomy-loop.php – replacing the string in question with $taxonomyName

<?php
    $taxonomyName = new WP_Query(
        array(
            'post_type' => 'project',
            'posts_per_page' => -1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'project-type',
                    'field' => 'slug',
                    'terms' => $taxonomyName
                )
            )
        )
    );
?>

<?php while ( $taxonomyName->have_posts() ) : $taxonomyName->the_post(); ?>

    <li class='project'>
        <a href='<?php the_permalink(); ?>'>
            <div class='image-w'>
                <?php if ( has_post_thumbnail() ) { ?>
                    <?php the_post_thumbnail(); ?>
                <?php } else { ?>
                    <img src='http://placehold.it/1600x800&text=No featured image yet' alt=''>
                <?php } ?>
            </div>
            <h3 class='project-name'><?php echo get_the_title(); ?></h3>
        </a>
    </li>

<?php endwhile; wp_reset_query(); ?>

In function.php : build the function with the $taxonomy-name passed in.

function getTaxonomyLoop( $taxonomyName ) {

    // grab the partial (wherever your partial is located)
    return include('parts/taxonomy-loop.php');

}

Then, where I wanted it to appear:

<?php getTaxonomyLoop('commercial'); ?>

See any gotchas?

Related posts

2 comments

  1. You can pass arguments to functions like this:

    function a($a) {
        return $a;
    }
    

    Or If you just want the complete code:

    function getLoop($st){
    return new WP_Query(
        array(
            'post_type' => 'project',
            'posts_per_page' => -1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'project-type',
                    'field' => 'slug',
                    'terms' => $st
                )
            )
        )
    );
    }
    

    So now you can call:

    $loop = getLoop('commercial');
    
  2. get_template_part is better suited for this sort of thing. It allows you to include the same template file multiple times, while still keeping your front-end code separate from your functions. It also allows your template code to be overwritten by a child theme.

    In your theme directory, you would create a file containing your template code.

    Then when you need to reference it you can just call get_template_part with the name of the file.

    For example, if I were to create a file called projects.php in the theme root, I could include it in another template file like this:

    get_template_part('projects');
    

    You cannot pass parameters to your template file this way, but you can use globals or define functions for any additional data your template needs. For example:

    global $taxonomyName;
    $taxonomyName = 'commercial';
    get_template_part('projects');
    

    From there you could reference the taxonomy: global $taxonomyName.

    Depending on how you are determining $taxonomyName you may be able to define a function to determine the taxonomy name without having to declare it global.

Comments are closed.