Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a function like the following:

<?php echo my_custom_function( array( 'category_url'=>'My Special Category' ) ); ?>

It pulls data from the "My Special Category" and displays it. I want to put a conditional statement around it that says if, and only if, that category has stuff to display, to display it. There are more than one category_url's so wrapping it with an if statement and just using my_custom_function doesn't work because of the array, it throws the error: "Missing Argument 1".

Any idea on how to make this function only echo if it exists AND if the argument is equal to itself? PHP noob question. ;) Thanks!!

share|improve this question
1  
I assume that your function is returning something so inside your function check if it resists and if not return ''; but seeing your function I'll be able to give a better answer –  Bainternet Apr 29 '11 at 0:20
1  
It would help if you pasted the function code. –  John P Bloch Apr 29 '11 at 1:08
    
Ok the contents of the function can be seen here pastebin.com/5ExA1peb -- Normally I can kind of read thru php and functions and stuff and get accomplished what I need but this is beyond me. Thanks! –  RodeoRamsey May 2 '11 at 17:21
    
So it turns out that the custom function I was using already had the conditional options set, and all I had to do was find the right variable and use it in my array! –  RodeoRamsey May 3 '11 at 1:12

1 Answer 1

up vote 2 down vote accepted

The following function shows you some "best practice" example ;)

It includes error handling, checking for emtpy array values, parsing input args with defaults, cares about if you want to echo the result, etc. You even got a filter for your child themes defaults. :)

The function in your functions.php file:

function wpse15886_check_cat( $args = array( 'cat_url', 'other_args_for_the_output', 'echo' => false ) )
{
    // Abort if no 'cat_url' argument was given
    if ( ! isset( $args['cat_url'] ) )
        $error = __( 'You have to specify a category slug.' );

    // check if the category exists
    $given_cat = get_category_by_slug( $args['cat_url'] );

    if ( empty( $given_cat ) )
        $error = sprintf( __( 'The category with the slug %1$s does not exist.' ), $args['cat_url'] );

    // check if we got posts for the given category
    $the_posts = get_posts( array( 'category' => $args['cat_url'] ) );

    if ( empty( $the_posts ) )
        $error = __( 'No posts were found for the given category slug.' );

    // error handling - generate the output
    $error_msg = new WP_Error( __FUNCTION__, $error );
    if ( is_wp_error( $error_msg ) ) 
    {
        ?>
            <div id="error-<?php echo $message->get_error_code(); ?>" class="error-notice">
                <strong>
                    <?php echo $message->get_error_message(); ?>
                </strong>
            </div>
        <?php 
        // abort
        return;
    }

    // Set some defaults
    $defaults = array(
         'key a' => 'val a'
        ,'key a' => 'val a'
        ,'key a' => 'val a'
    );
    // filter defaults
    $defaults = apply_filters( 'filter_check_cats', $defaults ); 

    // merge defaults with input arguments
    $args = wp_parse_args( $args, $defaults );
    extract( $args, EXTR_SKIP );

    // >>>> build the final function output
    $output = $the_posts;
    $output = __( 'Do what ever you need to do here.' );
    $output = sprintf( __( 'Manipulating some of the parsed %1$s for example.' ), $args['other_args_for_the_output'] );
    // <<<< end build output

    // just return the value for further modification
    if ( $args['echo'] === false )
        return $output;

    return print $output;
} 

Note that i haven't tested the function, so there might be some misstypings and else. But errors will lead you.

The usecase:

$my_cats = array(
     'cat_slug_a' 
    ,'cat_slug_b' 
    ,'cat_slug_c'
);

if ( function_exists( 'wpse15886_check_cat' ) )
{
    foreach ( $my_cats as $cat )
    {
        $my_cats_args = array(
             $cat
            ,'other_args_for_the_output' // this could also be another array set in here
            ,'echo' => true
        );

        // trigger the function
        wpse15886_check_cat( $my_cats_args );
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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