WordPress.org

Ready to get started?Download WordPress

Forums

Dynamic ads on Search Results page (1 post)

  1. jamiebrwr
    Member
    Posted 1 year ago #

    This is a business directory website.

    How can I display ads based on the posts category displayed in the search results?

    When a user visits say the Automotive page automotive ads are displayed in the sidebar and this works perfectly.

    BUT… when I do a search for say Automotive the sidebar shows nothing. I would like it to work as the normal posts do. In the url of the search page I see ?sq= which I assume I need to prepend to the args in my loop but I'm not sure how. I may even be completely wrong.

    Here is what my loop looks like inside the page-search.php file

    $args = array(
    	'post_type' => 'cn_ad',
    	'posts_per_page' => get_option( 'posts_per_page' ),
    	'orderby' => 'date',
    	'order' => 'DESC',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'category',
    			'field' => 'id',
    			'terms' => get_query_var( 'cat' ),
    			),
    		),
    	);
    	query_posts( $args ); ?>
    
    		<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    		<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
    		  the_post_thumbnail('medium', array('class' => 'thumbnail'));
    		}
    		?>
    
    		<?php endwhile; ?>
    
    		<?php else : ?>
    
    		<?php // TODO: do something else ?>
    
    		<?php endif; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic