I've been wrestling with some code cobbled together for a function I found, first published circa 2009 and my inexperience with PHP has me at a loss.
The function is designed to count episodes in a podcast category, thus:
function Get_Post_Number($postID){
$temp_query = $wp_query;
$postNumberQuery = new WP_Query(array (
'orderby' => 'date',
'order' => 'ASC',
'post_type' => 'any',
'category_name' =>'podcast-episodes',
'posts_per_page' => '-1'
));
$counter = 1;
$postCount = 0;
if($postNumberQuery->have_posts()) :
while ($postNumberQuery->have_posts()) : $postNumberQuery->the_post();
if ($postID == get_the_ID()){
$postCount = $counter;
} else {
$counter++;
}
endwhile; endif;
wp_reset_query();
$wp_query = $temp_query;
return $postCount;
}
As far as I can tell, this works fine. I created a shortcode to return the value in the query, which then resulted in the excerpt not showing. I then created a filter to do the same and the result was the same. No excerpt. Here I'll just post the shortcode:
/**
* Create shortcode to display Episode count.
*/
function episodeNo() {
$epID = get_the_ID();
$epNumber = Get_Post_Number($epID);
echo 'Episode ' . $epNumber;
}
add_shortcode('episode','episodeNo');
Here is my query:
$args = array(
'posts_per_page' => 3,
'category_name' => 'podcast-episodes'
);
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts()){
echo '<ul class="entries">';
while ( $query->have_posts() ) {
$query->the_post();
echo '<div class="entries-post">';
echo '<a href="' . get_permalink() . '">';
echo '<h1 class="entries-title">' . get_the_title() . '</h1>';
echo '</a>';
do_shortcode('[episode]');
echo '<div class="entries-excerpt">';
the_excerpt();
echo '</div>';
echo '</div>';
}
}
echo '</ul>';
}
/* Restore original Post Data */
wp_reset_postdata();
Now, if the shortcode runs before the_excerpt, it correctly displays the episode number but no excerpt.
Title
Episode 11
Title
Episode 10
Title
Episode 9
If it runs after the the_excerpt, it correctly displays the excerpt and the episode number:
Title
This week I cry into my coffee
Episode 11
Title
This week I approach a new design project
Episode 10
Title
This week I mostly play video games
Episode 9
Unfortunately, the design is such that episode must be displayed before excerpt.
The exact same occurs were I to use a filter that performed the same duty as the shortcode.
Does anyone have any idea why the_excerpt fails to display if called after this shortcode? I'm certain it's the way in which the episode number is calculated or returned, but know too little to correct it myself. Believe me, I have attempted to. At length.