• I realise Visual Composer is a premium plugin, so if it’s too much to ask for help on #1 below, I’d be just as happy with help on solving #2 – knowing what code would filter based on CPT categories (1-bed, or, 2-bed, or, 3-bed).

    ——————

    I have a custom post type of ‘Cottages’, with categories (1-bed, 2-bed, 3-bed).

    I can get an element to show in Visual Composer with the following vc_map in my functions.php:

    // Cottages List
    if(function_exists('vc_map')){
       vc_map( array(
       "name" => __("My Cottages List", 'archi'),
       "base" => "cottageslist",
       "class" => "",
       "category" => 'My Elements',
       "icon" => "icon-rtcvc",
       "params" => array(      
          array(
             "type" => "textfield",
             "holder" => "div",
             "class" => "",
             "heading" => "Show how many cottages per page?",
             "param_name" => "number",
             "value" => "",
             "description" => __("Add Number -1 for show all post.", 'archi')
          ),  
        )
        ));
    }'

    Then, I know it displays with the following php/html code (that also staggers rows of feature-image, post content … post content, feature-image), also in functions.php

    // Cottages List start
    add_shortcode('cottageslist','cottageslist_func');
    function cottageslist_func($atts, $content = null){
        extract(shortcode_atts(array(
            'number'    =>      '',
        ), $atts));
        $number1 = (!empty($number) ? $number : 4);
        ob_start(); 
    ?>
    
        <?php
            $i = 0;
            $args = array(
                'post_type' => 'cottages',
                'posts_per_page' => $number1,
            );
            $cottages = new WP_Query($args);
            if($cottages->have_posts()) : while($cottages->have_posts()) : $cottages->the_post(); $i++;
        ?>
    
            <section class="side-bg no-padding service-list" id="section-service-<?php echo esc_attr($i); ?>">
    
                <?php if ($i % 2 == 1) { ?>
    
                <div class="image-container col-md-5 pull-left" data-delay="0">
                    <div class="background-image" style="background-image:url('<?php the_post_thumbnail_url( 'full' ); ?>');"></div>
                </div>
    
                <div class="container">
                    <div class="row">
                        <div class="inner-padding">
                            <div data-wow-delay=".5s" class="col-md-6 col-md-offset-6 wow fadeInRight">
                                <h3 class="id-color"><a class="" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                                <?php the_excerpt(); ?>
                                <div class="spacer-single"></div>
                                <a class="btn-line" href="<?php the_permalink(); ?>"><?php _e('Read More', 'archi') ?></a>
                            </div>
                            <div class="clearfix"></div>
                        </div>
                    </div>
                </div>
    
                <?php }else{ ?>
    
                <div data-delay="0" class="image-container col-md-5 col-md-offset-7 pull-right right0">
                    <div class="background-image" style="background-image:url('<?php the_post_thumbnail_url( 'full' ); ?>');"></div>
                </div>
    
                <div class="container">
                    <div class="row">
                        <div class="inner-padding">
                            <div data-wow-delay=".5s" class="col-md-6 wow fadeInLeft">
                                <h3 class="id-color"><a class="" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                                <?php the_excerpt(); ?>
                                <div class="spacer-single"></div>
                                <a class="btn-line" href="<?php the_permalink(); ?>"><?php _e('Read More', 'archi') ?></a>
                            </div>
                            <div class="clearfix"></div>
                        </div>
                    </div>
                </div>
                <?php } ?>
    
            </section>            
    
        <?php endwhile; wp_reset_postdata(); endif; ?>  
    
    <?php
        return ob_get_clean();
    }   
    // Cottages List end

    Here’s where I’m completely lost!:

    1. I want to have the option in Visual Composer to narrow the data source based on one or more categories (bearing in mind they’re custom post type categories). I know this has to go in my first block, but I’m stumped on exactly what that code should be.
    2. I believe I’ll also need additional code in my second block (php/html) to ensure only the chosen categories are displayed.

    Help! I’ve been doing the noob trick of cuting-n-pasting from whatever code I can find that might help, and after a day of this I’m still failing!

    And yes, I know the VC Post Grid element will filter very well, but I don’t like how on the front end those Post Grid blocks only load up a few seconds after the rest of the page has fully loaded.

    Thanks

    • This topic was modified 8 years, 4 months ago by Detour.
  • The topic ‘Visual composer, narrow data source, CPT categories’ is closed to new replies.