A 3rd party plugin that I use adds a shortcode that displays a list of adverts (CPT posts). The shortcode function includes (with an include
statement) a template - the list.php
file, that displays a search form and, in its turn, includes a second template - the list-item.php
file, that displays the list of adverts itself.
Each entry of this list includes only a post thumbnail, the post title, the post date, and the price of the advertised item. By changing the list-item.php code I added also the excerpt of the post. The problem is that excerpts are displayed properly when we browse the main adverts list page and not displayed when we browse a category page.
How the adverts list is build when we browse a category page is partially influenced by a function applied to the the_content()
filter. The question is: Why excerpts are not displayed on category pages?
This is the shortcode function that displays a list of adverts (and include the list.php template):
add_shortcode('adverts_list', 'shortcode_adverts_list');
/**
* Generates HTML for [adverts_list] shortcode
*
* @param array $atts Shorcode attributes
* @since 0.1
* @return string Fully formatted HTML for adverts list
*/
function shortcode_adverts_list( $atts ) {
wp_enqueue_style( 'adverts-frontend' );
wp_enqueue_style( 'adverts-icons' );
wp_enqueue_script( 'adverts-frontend' );
extract(shortcode_atts(array(
'name' => 'default',
'category' => null,
'columns' => 2,
'paged' => adverts_request("pg", 1),
'posts_per_page' => 20,
), $atts));
$taxonomy = null;
$meta = array();
$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,
),
);
}
$loop = new WP_Query( array(
'post_type' => 'advert',
'post_status' => 'publish',
'posts_per_page' => $posts_per_page,
'paged' => $paged,
's' => $query,
'meta_query' => $meta,
'tax_query' => $taxonomy
) );
$paginate_base = get_the_permalink() . '%_%';
$paginate_format = stripos( $paginate_base, '?' ) ? '&pg=%#%' : '?pg=%#%';
// adverts/templates/list.php
ob_start();
include_once ADVERTS_PATH . 'templates/list.php';
return ob_get_clean();
}
This is the code of the list.php
that includes the list-item.php
template:
<div class="adverts-list">
<?php if( $loop->have_posts() ): ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php include ADVERTS_PATH . 'templates/list-item.php' ?>
<?php endwhile; ?>
<?php else: ?>
<div class="adverts-list-empty"><em><?php _e("There are no ads matching your search criteria.", "adverts") ?></em></div>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
This is the full code of customized list-item.php
:
<div class="advert-item advert-item-col-<?php echo (int)$columns ?>">
<?php $image = adverts_get_main_image( get_the_ID() ) ?>
<div class="advert-img">
<?php if($image): ?>
<img src="<?php esc_attr_e($image) ?>" alt="" class="advert-item-grow" />
<?php endif; ?>
</div>
<div class="advert-post-title">
<span title="<?php esc_attr_e( get_the_title() ) ?>" class="advert-link"><?php the_title() ?></span>
<a href="<?php the_permalink() ?>" title="<?php esc_attr_e( get_the_title() ) ?>" class="advert-link-wrap"></a>
</div>
<!-- THIS IS WHAT WAS CUSTOMIZED -->
<div class="advert-post-excerpt">
<span class="advert-excerpt"><?php echo get_the_excerpt(); ?></span>
</div>
<!-- END OF CUSTOMIZATION -->
<div class="advert-published ">
<span class="advert-date"><?php echo date_i18n( get_option( 'date_format' ), get_post_time( 'U', false, get_the_ID() ) ) ?></span>
<?php $price = get_post_meta( get_the_ID(), "adverts_price", true ) ?>
<?php if( $price ): ?>
<div class="advert-price"><?php esc_html_e( adverts_price( get_post_meta( get_the_ID(), "adverts_price", true ) ) ) ?></div>
<?php endif; ?>
</div>fun
</div>
This is the function applied to the the_content()
filter when we browse single posts and also category pages (in this case again the first shortcode function is called):
add_filter('the_content', 'adverts_the_content');
function adverts_the_content($content) {
global $wp_query;
if (is_singular('advert') && in_the_loop() ) {
ob_start();
$post_id = get_the_ID();
include ADVERTS_PATH . 'templates/single.php';
$content = ob_get_clean();
} elseif( is_tax( 'advert_category' ) && in_the_loop() ) {
$content = shortcode_adverts_list(array(
"category" => $wp_query->get_queried_object_id()
));
}
return $content;
}
UPDATE
This is the adverts_request()
function, called by the shortcode_adverts_list()
function:
function adverts_request($key, $default = null) {
if(isset($_POST[$key])) {
return $_POST[$key];
} elseif(isset($_GET[$key])) {
return $_GET[$key];
} else {
return $default;
}
}
the_content
on a category page might help you out.is_singular('advert')
is clearly not true on a category/tax page. Are you sure theelseif
is true? Make an echo on that condition in the page template, to make sure.elseif
, is really true. Maybe the shortcode_adverts_list() needs a missing attribute?adverts_request()
does. Can you add that function, too? or, better yet, add$query = '';
inside the conditionif($category)
insideshortcode_adverts_list()
.