• Hi there,

    I am trying to add banner adds to the top and bottom of ad category listings dependent on what category is being viewed.

    So for example if viewing the “Motorbikes” category I would show banners related to motorbikes at the top and bottom of category.

    I’ve copied list.php to my child theme and tried various things but nothing is working?

    I’ve added this to the top of the file:

    <?php $advert_category = get_the_terms( get_the_ID(), 'advert_category' ) ?>

    and then tried:

    <?php if ($advert_category = 'Motorcycles') { mycodehere } ?>
    and
    <?php if( $advert_category[0]->name == "Motorcycles") { my code here } ?>

    at various places in list.php where I want the code to work and nothing happens?

    Thanks in advance…

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    the easiest way to add the ads above the Adverts list is to use adverts_sh_list_before action.

    
    add_action( "adverts_sh_list_before", function( $params ) {
      if( ! is_tax( "advert_category" ) ) {
        return;
      }
    
      $category_id = get_queried_object_id();
      $term = get_term( $category_id );
    
      if( $term->slug == "motorcycles" ) {
        echo "[motorcycles ad here]";
      } else if( $term->slug == "..." ) {
        echo "[...]";
      }
    } );
    

    We do not have action to add the ads below the Adverts list though, if you want to add them there as well then you would need to use the approach with the list.php child template file.

    I understand you are following the directions from this article https://wpadverts.com/doc/child-themes-and-templates/, if so then make sure you also have an adverts_template_load filter that will load your custom template as by default WPAdverts does not search for child-templates.

    Thread Starter twdtony

    (@twdtony)

    Thanks Greg,

    Using your code I have sorted it, I’ve created 2 new template files in the child theme (category-top.php & category-bottom.php) and added your code in there and then included those new files in list.php – keeps things neater than way

    Sorry another question – If I wanted to inject an ad inbetween the listings on the category page (after the 3rd listing for example) is that possible – jQuery maybe?

    Thanks again

    Plugin Author Greg Winiarski

    (@gwin)

    You could create a child-template for the index-item.php file with a code like the one below

    
    <?php
    
    global $ad_counter;
    if( $ad_counter === null ) {
      $ad_counter = 0;
    }
    
    if( $ad_counter == 2 && is_tax( "advert_category" ) ) {
      echo "[Your ad here]";
    }
    
    include ADVERTS_PATH . "/templates/list-item.php";
    
    $ad_counter++;
    

    I am outside of the office i did not test it so the code might require some polishing.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add custom code to list.php depending on category?’ is closed to new replies.