Customize the WPAdverts adverts_list to exclude category

After installing the WPAdverts plugin, you can add the shortcode [adverts_list] to any page (usually homepage) to display all active post ads. Once you have some ads posted the page should look similar to the one below:

Shortcode Parameters

  • name: string (default “default”) – this is mainly for future use, the name basically should identify shortcode.
  • redirect_to: string (default null) (since 1.0.7) – you can pass either page ID or an URL starting with http://, when searching user will be redirected to this page / URL, ideally the page to which user is redirected to should also have [adverts_list] shortcode.
  • search_bar: string (default “enabled”) (since 1.0.5) – one of “disabled”, “enabled”, allows to hide search bar.
  • show_results: int (default 1) (since 1.0.7) – change value to 0 if you only wish to show search form
  • show_pagination: int (default 1) (since 1.1.2) – change value to 0 if you want to hide pagination below Ads list.
  • columns: int (default 2) – number of columns on the list, it’s best to use either 2 or 3.
  • paged: int (default 1) – current page number, set it only if you want to start with page different than first.
  • posts_per_page: int (default: 20) – number of items per page
  • category: int (default null) – allows to display ads only from selected category
  • author: int (default: null) (since 1.0.8) – user ID, setting this parameter allows to list all Ads posted by selected user
  • display: string (default ‘grid’) (since 1.0.8) – allows to select if Ads should be displayed in a grid (2 or more per row) or as a list
  • switch_views: int (default 0) (since 1.0.8) –  setting this param value to 1 will show 2 icons in the search bar which will allow user to switch between grid and list view.

Example Usage

List with 3 columns and total of 40 items per page
[adverts_list columns=”3″ posts_per_page=”40″]

The exclude_category parameter

What if you want to exclude one category from being displayed? Here is the how-to, just open
/wp-content/plugins/wpadverts/includes/shortcodes.php

function shortcode_adverts_list( $atts ) {



    wp_enqueue_style( 'adverts-frontend' );

    wp_enqueue_style( 'adverts-icons' );



    wp_enqueue_script( 'adverts-frontend' );



    $params = shortcode_atts(array(

        'name' => 'default',

        'author' => null,

        'redirect_to' => '',

        'search_bar' => adverts_config( 'config.ads_list_default__search_bar' ),

        'show_results' => true,

        'category' => null,
         
        //added the new parameter
        'exclude_category' => null,

        'columns' => adverts_config( 'config.ads_list_default__columns' ),

        'display' => adverts_config( 'config.ads_list_default__display' ),

        'switch_views' => adverts_config( 'config.ads_list_default__switch_views' ),

        'allow_sorting' => 0,

        'order_by' => 'date-desc',

        'paged' => adverts_request("pg", 1),

        'posts_per_page' => adverts_config( 'config.ads_list_default__posts_per_page' ),

        'show_pagination' => true

    ), $atts, 'adverts_list' );

    

    extract( $params );

    

    if( is_numeric( $redirect_to ) ) {

        $action = get_permalink( $redirect_to );

    } else {

        $action = $redirect_to;

    }

    

    $taxonomy = null;

    $meta = array();

    $orderby = array('menu_order'=>'DESC');

    

    $query = adverts_request("query");

    $location = adverts_request("location");

    

    if($location) {

        $meta[] = array('key'=>'adverts_location', 'value'=>$location, 'compare'=>'LIKE');

    }

    

    if($category) {

        $taxonomy =  array(

            array(

                'taxonomy' => 'advert_category',

                'field'    => 'term_id',

                'terms'    => $category,

            ),

	);

    }

//added the new parameter
    if($exclude_category) {

        $taxonomy =  array(

            array(

                'taxonomy' => 'advert_category',

                'field'    => 'term_id',

                'terms'    => $exclude_category,

                'operator' => 'NOT IN'
            ),

	);

    }


Usage

[adverts_list exclude_category=”41″]

(Visited 50 times, 1 visits today)

Leave a Reply