Question

I am trying to create a function whicht has different shortcodes as output. I have the post-type => womos and the field => personenzahl. The field has the values 1 up to 7. Now I need a shortcode for every value. This is my function for value => '1':

function modell_nach_personenzahl() { 
    $args = array(
        'post_type'   => 'womos',
        'order'       => 'ASC',
        'orderby'     => 'personen',
        'field'       => $atts['personen'],
        'numberposts' => -1,
        'meta_query'  => array (
      array (
         'key' => 'personen',
         'compare' => '>=',
         'value'   => '1'
      ),
    )
    );


$myquery = new WP_Query($args);

    while($myquery->have_posts()) : $myquery->the_post();

        $bild = get_field('header-bild', get_the_ID()); ?>
        <div class="modell-liste">
        <a href="<?php echo get_permalink(get_the_ID()); ?>"><img src="<?php echo($bild); ?> "> </a><br>

        <strong>Modell: </strong><a href="<?php echo get_permalink(get_the_ID()); ?>"><?php echo get_post_meta(get_the_ID(),'modell', true);?></a> <br> 

        <strong>max. Personen: </strong><?php echo get_post_meta(get_the_ID(),'personen', true);?> <br><br> 
        </div>

    <?php endwhile; 

    wp_reset_postdata();

Now I need a shortcode for value => '1', for value => '2', ...value => '7'. Is ist possible do do this with the one function above? If yes, how can I achieve it?

Était-ce utile?

La solution

Edit your function to accept a parameter, e.g. function modell_nach_personenzahl( $value ) { Edit your meta query to accept this parameter, e.g. 'value' => $value

Then register a bunch of shortcodes:

for ( $i = 1; $i <= 7; $i++ ) {
    add_shortcode( "shortcode-name-{$i}", function() use ( $i ) {
        modell_nach_personenzahl( $i );
    } );
}

Then just use [shortcode-name-1], [shortcode-name-2], etc.

Autres conseils

Here is the code:

function modell_nach_personenzahl_2( $value ) {

    $args = array(
        'post_type'   => 'womos',
        'order'       => 'ASC',
        'orderby'     => 'personen',
        'field'       => $atts['personen'],
        'numberposts' => -1,
        'meta_query'  => array (
      array (
         'key' => 'personen',
         'compare' => '>=',
         'value'   => $value
      ),

    )

    );


$myquery = new WP_Query($args);


    while($myquery->have_posts()) : $myquery->the_post();

        $bild = get_field('header-bild', get_the_ID()); 

        ?>
        <div class="modell-liste">
        <a href="<?php echo get_permalink(get_the_ID()); ?>"><img src="<?php echo($bild); ?> "> </a><br>

        <strong>Modell: </strong><a href="<?php echo get_permalink(get_the_ID()); ?>"><?php echo get_post_meta(get_the_ID(),'modell', true);?></a> <br> 

        <strong>max. Personen: </strong><?php echo get_post_meta(get_the_ID(),'personen', true);?> <br><br> 
        </div>


    <?php endwhile; 


    wp_reset_postdata();

    }

    add_shortcode( "anzahl_personen-{$i}", function() use ( $i ) {
        modell_nach_personenzahl_2( $i );
    } );
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top