Domanda

Ho una funzione simile al seguente:

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

Si estrae i dati dalla "Categoria My Special" e la mostra. Voglio mettere un'istruzione condizionale attorno ad esso che dice che se, e solo se, quella categoria ha roba da esposizione, per visualizzarlo. Ci sono più di una category_url è così avvolgendolo con un'istruzione if e usando solo my_custom_function non funziona a causa della matrice, si genera l'errore:. "Argomento 1 Missing"

Qualche idea su come fare questa funzione solo eco se esiste e se l'argomento è uguale a se stesso? PHP niubbo domanda. ;) Grazie !!

È stato utile?

Soluzione

Le seguenti funzioni vi mostra alcuni esempi di "best practice";)

Esso comprende la gestione degli errori, il controllo di valori di matrice emtpy, l'analisi args di ingresso con le impostazioni predefinite, si preoccupa per se si vuole eco il risultato, ecc È anche ottenuto un filtro per i vostri temi bambino default. :)

La funzione nel file functions.php:

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;
} 

Si noti che non ho ancora testato la funzione, quindi ci potrebbero essere alcuni misstypings e altro. Ma gli errori vi condurranno.

Il caso d'uso:

$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 );
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top