“Continue Reading” link wrong in post excerpt – anyone know the actual solution?
-
I have a shortcode which lists entries from a particular post type, with a picture and excerpt. However, for some reason the ‘Continue Reading’ link doesn’t work properly – it just links to the top of the current page. I have absolutely no idea why, and I can’t seem to fix it, whatever I do.
The code in question is a shortcode, as follows:
add_shortcode('plants', 'plant_list_shortcode'); // define the shortcode function function plant_list_shortcode($atts) { extract(shortcode_atts(array( 'cat' => '', 'catexclude' => '', 'view' => '', 'featured' => '', 'hidetitle' => '', 'des' => '', 'maxdes' => '', ), $atts)); $tax_query = array ( "relation" => "AND", array( "taxonomy" => "plant_list_category", "field" => "slug", "terms" => array($cat) ), array( "taxonomy" => "plant_list_category", "field" => "slug", "terms" => array($catexclude), "operator" => "NOT IN" ), ); if( ! empty( $cat ) ) { $query = array( 'tax_query' => $tax_query, 'order' => 'ASC', 'orderby' => 'title', 'post_type' => 'plant_lists', 'post_status' => null, 'nopaging' => 1, 'posts_per_page' => -10); } else { $query = array( 'plant_list_category' => $cat, 'order' => 'ASC', 'orderby' => 'title', 'post_type' => 'plant_lists', 'post_status' => null, 'nopaging' => 1, 'posts_per_page' => -10); } $plant_lists = new WP_Query( $query ); $countPlants='0'; $plant_shortcode = ''; if ( !empty( $cat ) && $hidetitle != 'yes' ) { global $wpdb; $catname = $wpdb->get_var("SELECT name FROM $wpdb->terms WHERE slug = '$cat'"); $plant_shortcode .= '<div class="plant-catname">' . $catname . '</div>'; } $plant_shortcode .= '<table class="planttable"><tbody>'; while( $plant_lists->have_posts() ) : $plant_lists->the_post(); $countPlants++; setup_postdata( get_post( $id )); $theimage = wp_get_attachment_image_src( get_post_thumbnail_id($id) , 'medium'); $plant_shortcode .= '<tr><td width="50%" align="center"><a href="' . get_permalink() . '"><img src="'.$theimage[0].'" alt="" /></a></td>'; $plant_shortcode .= '<td><div class="plant-title"><a href="' . get_permalink() . '">'. get_the_title().'</a></div>'; $plant_shortcode .= '<div class="plant-excerpt">'; $family = get_post_meta( $id, '_plant_lists_family', true ); if($family != NULL) { $plant_shortcode .= '<p><strong>Family: '.$family.'</strong></p>'; } $plant_shortcode .= '<p>'. get_the_excerpt( $id ) .'</p>'; $plant_shortcode .= '</div></td></tr>'; endwhile; $plant_shortcode .= '</tbody></table>'; if ($countPlants == '0') { $plant_shortcode = 'No plants are currently posted in this category'; } wp_reset_postdata(); wp_reset_query(); $plant_shortcode = do_shortcode( $plant_shortcode ); return (__($plant_shortcode)); }//ends the plant_shortcode function
I then add the shortcode to a page or post as follows:
[plants cat="terrarium-madagascar" hidetitle="yes" featured="no" view="list" buttontext="Details about this plant..." des="yes" maxdes="500"]
The result is that the ‘Continue Reading’ links within each excerpt link back to the current page. The link on the title is correct, it’s only the continue reading link that’s wrong. You can see an example here:
https://www.pumpkinbeth.com/2017/04/madagascar-terrarium-planting-list/
Even more curiously, when I use the shortcode on a page instead of a post, the problem doesn’t happen:
https://www.pumpkinbeth.com/plants/
I suspect the issue is something around my use of the WP Loop, but I’m relatively new to WP development so must be missing something. Help! ??
I’ve seen this issue raised a couple of times around the forums, and on StackOverflow, but nobody seems to know the definitive answer to fix it. I’d really like to solve it. Alternatively, if somebody can show me how to hide the ‘Continue Reading’ link for excerpts – but only within the context of this shortcode – then I’d appreciate it.
- The topic ‘“Continue Reading” link wrong in post excerpt – anyone know the actual solution?’ is closed to new replies.