0

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?

  • Yes, it is. That’s why shortcode callback function can get parameters. – Krzysiek Dróżdż Apr 12 at 20:22
0

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.

  • Thanks. It looks great. That's what I was looking for. But it is not working. I must have done somthing wrong. I changed the function, put the value to a variable and copyed your code for the shortcode. In the frontend I can only see the shortcode and not the images. Maybe you can help me once again. – kuh13 Apr 12 at 20:53
  • Can you show your code? – Anastis Apr 12 at 21:25
0

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 );
    } );
  • 1
    I found my fault: I had to insert: for ($i = 1; $i <= 7; $i++) {} around your code. Thanks a lot! – kuh13 Apr 13 at 10:58
  • Ah, yes, it seems I forgot to add the loop as well. I'll update my answer so that It's correct, for future reference. – Anastis Apr 15 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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