0

I'm working on a Wordpress theme. The theme is Classifieds theme from premiumpress. The theme has a shortcode to list all the listings. The corresponding shortcode is [LISTINGS].

The function for the shortcode is as follows

/* =============================================================================
    [LISTINGS] - SHORTCODE
    ========================================================================== */
function wlt_page_listings( $atts, $content = null ) {

global $userdata, $wpdb, $CORE; $STRING = ""; $extra=""; $i=1; $stopcount = 4;

extract( shortcode_atts( array( 'query' => '', 'show' => '', 'type' => '',  'cat' => '', 'orderby' => '', 'order' => '', 'grid' => "no", 'featuredonly' => "no"), $atts ) );

   // SETUP DEFAULTS
if(!isset($atts['show']) || (isset($atts['show']) && $atts['show'] == "") ){    $atts['show'] = 5; }
 if($atts['type'] == ""){   $atts['type'] = THEME_TAXONOMY.'_type'; }
if($atts['orderby'] == ""){     $atts['orderby'] = "post_title"; }
if($atts['order'] == ""){   $atts['order'] = "desc"; }

// DEFAULT FOR LIST STYLE
if($grid == "yes"){ 
    $sstyle = "grid_style";  
    $STRING .= '<script language="javascript">jQuery(window).load(function() { equalheight(\'.grid_style .item .thumbnail\');});</script>'; 
}else{ 
    $sstyle = "list_style";
} 

$query= str_replace("#038;","&",$query);

if(strlen($query) > 1){
    // ADD ON POST TYPE FOR THOSE WHO FORGET
    if(strpos($query,'post_type') == false){
    $args = $query ."&post_type=".THEME_TAXONOMY."_type";
    }else{
    $args = $query;
    }

 }elseif($featuredonly == "yes"){ 
 $args = array('posts_per_page' => $atts['show'], 
'post_type' => $atts['type'], 'orderby' => $atts['orderby'], 'order' => $atts['order'],
'meta_query' => array (
        array (
          'key' => 'featured',
          'value' => 'yes',
        )
      ) 
 );

}else{
/*** default string ***/
$args = array('posts_per_page' => $atts['show'], 'post_type' => $atts['type'], 'orderby' => $atts['orderby'], 'order' => $atts['order'] );
}

/*** custom category ***/
if(strlen($atts['cat']) > 1){   
$args = array('tax_query' => array( array( 'taxonomy' => str_replace("_type","",$atts['type']) ,'field' => 'term_id','terms' => array( $atts['cat'] ))), 'posts_per_page' => $atts['show'] );
} 

// BUILD QUERY
$the_query = new WP_Query( hook_custom_queries($args) );

 if ( $the_query->have_posts() ) {

    $STRING .= '<div class="_searchresultsdata"><div class="wlt_search_results row '.$sstyle.'">'; 

    while ( $the_query->have_posts() ) {  $the_query->the_post();  $post = get_post();

    $STRING .= '<div class="item '.hook_gallerypage_item_class('col-md-4').$CORE->FEATURED($post->ID).'">'.hook_item_cleanup(hook_gallerypage_item($CORE->ITEM_CONTENT($post))).'</div>';

    }

    $STRING .= '</div></div><div class="clearfix"></div>'; 

} 
// END QUERY
wp_reset_postdata();

return $STRING;     

}
add_shortcode( 'LISTINGS', array($this,'wlt_page_listings') );  

The shortcode does not have an attribute to hide certain categories. I need to display all listings, except the ones in wedding category, which is a custom taxonomy. Is there any way to do that with the above code?

Will something like this work?

if ( is_tax( 'listing', 'wedding' ) )  {
do not display the wedding listings and display the rest}

Any suggestions?

EDITS:

This my online site url : http://webzer.comxa.com/ The main page shows the all the products.I like to have all but not one that is from wedding category coz i have separate page to list wedding category.

i have tried this where 51 is the page id of my home store page

if ( is_page( 51 ) && is_tax( 'listing', 'wedding' ) )  {
?><style>.caption {display:none!important;}</style>
<?php } ?>

this also didn't work

  • I don't have time to code now, but for starters, you need to alter your tax query inside the shortcode. I would suggest creating a child theme and creating your own custom shortcode from the original. Have a look at the codex page of the `WP_Query` class on tax queries. – Pieter Goosen Sep 19 '14 at 12:32

1 Answers1

0

Consider this :

change

function wlt_page_listings( $atts, $content = null ) { 

to

function wlt_page_listings( $atts, $content = null, $exclude=array(99) ) { // 99 is wedding cat id

where $exclude is an optional array of excluded cat names (99 in there for ease of testing/use)

Then in the

while ( $the_query->have_posts() ) { 

add something like this:

$post = get_post(); // get post obj, will use the ID attr
$cats = wp_get_post_categories( $post->ID )); // returns array of IDs for all cats
foreach($exclude as $x){ // loop on the excluded cat ids
    if(in_array($x, $cats))continue; // if excluded skip
}

http://codex.wordpress.org/Function_Reference/wp_get_post_categories

I think this will work or at least got you close.

display:none is bad mojo as the content will still be in your source code for others to see.

I hope I addressed the problem correctly for you, cheers.

Marc
  • 1,895
  • 18
  • 25
  • i added this but its showing some warnings.I tried $post = get_post(); echo $cats = wp_get_post_categories( $post->ID ); inside the while but its printing as array –  Sep 20 '14 at 05:00
  • hmm, get_post() should default to object (http://codex.wordpress.org/Function_Reference/get_post). What does $post look like? It should be an object (post a var_dump()). The custom taxonomy should not get in the way. Are categories returned form wp_get_post_categories() using a valid post id for the arg? – Marc Sep 20 '14 at 22:33
  • hi,I dont know atualy.Please help –  Sep 22 '14 at 04:44
  • Love to see the var_dump() http://php.net/manual/en/function.var-dump.php of $post and ultimately the results from wp_get_post_categories(). You could be in an edge case, even so, my comprehension of WP best practice should allow these things to work correctly (custom taxonomy, generally speaking, is not post or page but "what_ever_you_want" as a post type where the ID and other associations are kept). Regarding category filtering, the registration of custom taxonomy gets pretty intense opening up many options and features http://codex.wordpress.org/Function_Reference/register_taxonomy – Marc Sep 23 '14 at 17:05