create a shortcode with acf including a filter

I am using advanced custom fields and my problem is: I have several campers for a different number of persons. Now I nee

I am using advanced custom fields and my problem is: I have several campers for a different number of persons. Now I need a shortcode to display all campers for all certain numbers of persons. The shortcode should be like this [shortcode_name, personens="x"]. The x stands for the different number of persons for which the function should filter the campers.

My starting code is the following (shows all campers for 6 persons):

function modell_nach_personenzahl_variabel() { 

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

Thanks for the help.

I am using advanced custom fields and my problem is: I have several campers for a different number of persons. Now I need a shortcode to display all campers for all certain numbers of persons. The shortcode should be like this [shortcode_name, personens="x"]. The x stands for the different number of persons for which the function should filter the campers.

My starting code is the following (shows all campers for 6 persons):

function modell_nach_personenzahl_variabel() { 

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

Thanks for the help.

Share Improve this question edited Apr 12, 2019 at 7:01 nmr 4,5672 gold badges17 silver badges25 bronze badges asked Apr 11, 2019 at 22:01 kuh13kuh13 51 bronze badge 1
  • Take a look at add_shortcode codex.wordpress/Function_Reference/add_shortcode – MikeNGarrett Commented Apr 12, 2019 at 2:19
Add a comment  | 

1 Answer 1

Reset to default 0

This is not the full working code, just to illustrate how to add the shortcode and trigger the function.

<?php

   // Check if ACF is available
   if(function_exists('get_field')) {
       function modell_nach_personenzahl_variabel() {
           $args = array(
               'post_type'   => 'womos',
               'order'       => 'ASC',
               'orderby'     => 'personen',
               'field'    => $atts['personen'],
               'numberposts' => -1,
               'meta_query' => array (
                array (
                    'key' => 'personen',
                    'value' => '6'
                )
            )
        );

        $query = new WP_Query($args);

        if($query->have_posts()):
            while($query->have_posts()):

             // for testing purposes
             echo "<pre>";
                print_r($query->the_post);
             echo "</pre>";

             ?>
                <h1><?php the_title(); ?></h1>
                <!-- get the rest of the fields -->

            <?php
            endwhile;

        endif;

       // Add the shortcode
       add_shortcode('filter-camper-nach-personen', 'modell_nach_personenzahl_variabel');
   }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745597270a4635190.html

相关推荐

  • create a shortcode with acf including a filter

    I am using advanced custom fields and my problem is: I have several campers for a different number of persons. Now I nee

    13小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信