Title highlight removes link
-
After editing search.php changing the_title() to relevanssi_the_title and get_permalink() to relevanssi_get_permalink(), the document title in the search results highlights the search term in the title if it exists and in the excerpt if it exists; however, the title is no longer a link to the post. If I change relevanssi_the_title() back to the_title(), the link displays but the search term is no longer highlighted in the title. I am using the Twenty Seventeen theme. Is there a way to get the term highlighted in the title and keep the title functionality as a link? Thanks for your help.
-
Can you please show me the HTML code of the title when it’s working and when it’s not working?
Using relevanssi_the_title produces the following code for the first result returned:
<header class="entry-header"> <div class="entry-meta"> <span class="screen-reader-text">Posted on</span> <a href="https://192.168.2.100/annual-meeting-june-1-2005/" rel="bookmark"><time class="entry-date published" datetime="2005-06-01T12:04:45-08:00">June 1, 2005</time><time class="updated" datetime="2016-01-25T12:25:04-08:00">January 25, 2016</time></a><span class="edit-link"><a class="post-edit-link" href="https://192.168.2.100/wp-admin/post.php?post=5063&action=edit">Edit<span class="screen-reader-text"> "Annual Meeting – June 1, 2005"</span></a></span> </div><!-- .entry-meta --> Annual Meeting - June 1, 2005 </header><!-- .entry-header --> <div class="entry-summary"> <p>…is scheduled for Jun 7th at 7pm at which time the Cove will <span style='text-decoration: underline; color:red;'>learn</span> if its plans to develop the lots has the approval of the Commission. If, during a…</p> </div><!-- .entry-summary --> </article><!-- #post-## -->
whereas using the_title produces:
<header class="entry-header"> <div class="entry-meta"> <span class="screen-reader-text">Posted on</span> <a href="https://192.168.2.100/annual-meeting-june-1-2005/" rel="bookmark"><time class="entry-date published" datetime="2005-06-01T12:04:45-08:00">June 1, 2005</time><time class="updated" datetime="2016-01-25T12:25:04-08:00">January 25, 2016</time></a><span class="edit-link"><a class="post-edit-link" href="https://192.168.2.100/wp-admin/post.php?post=5063&action=edit">Edit<span class="screen-reader-text"> "Annual Meeting – June 1, 2005"</span></a></span> </div><!-- .entry-meta --> <h2 class="entry-title"><a href="https://192.168.2.100/annual-meeting-june-1-2005/?highlight=learn" rel="bookmark">Annual Meeting – June 1, 2005</a></h2> </header><!-- .entry-header --> <div class="entry-summary"> <p>…is scheduled for Jun 7th at 7pm at which time the Cove will <span style='text-decoration: underline; color:red;'>learn</span> if its plans to develop the lots has the approval of the Commission. If, during a…</p> </div><!-- .entry-summary --> </article><!-- #post-## -->
The difference being the creation of an
<h2
entry. Specifically,
Annual Meeting - June 1, 2005 </header><!-- .entry-header -->
becomes
<h2 class="entry-title"><a href="https://192.168.2.100/annual-meeting-june-1-2005/?highlight=learn" rel="bookmark">Annual Meeting – June 1, 2005</a></h2> </header><!-- .entry-header -->
Yeah, the link is completely missing there. Can you show me the PHP code that doesn’t work, please? There’s probably something wrong there.
Thanks for your help. I am using the content-excerpt.php file from the Twenty Seventeen theme. I copied it into my child theme and modified it changing the_title() and get_permalink() to their Relevanssi replacements.
<?php /** * Template part for displaying posts with excerpts * * Used in Search Results and for Recent Posts in Front Page panels. * * @link https://codex.www.ads-software.com/Template_Hierarchy * * @package WordPress * @subpackage Twenty_Seventeen * @since 1.0 * @version 1.2 */ ?> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <header class="entry-header"> <?php if ( 'post' === get_post_type() ) : ?> <div class="entry-meta"> <?php echo twentyseventeen_time_link(); twentyseventeen_edit_link(); ?> </div><!-- .entry-meta --> <?php elseif ( 'page' === get_post_type() && get_edit_post_link() ) : ?> <div class="entry-meta"> <?php twentyseventeen_edit_link(); ?> </div><!-- .entry-meta --> <?php endif; ?> <?php if ( is_front_page() && ! is_home() ) { // The excerpt is being displayed within a front page section, so it's a lower hierarchy than h2. the_title( sprintf( '<h3 class="entry-title"><a href="%s" rel="bookmark">', esc_url( relevanssi_get_permalink() ) ), '</a></h3>' ); } else { the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( relevanssi_get_permalink() ) ), '</a></h2>' ); } ?> </header><!-- .entry-header --> <div class="entry-summary"> <?php the_excerpt(); ?> </div><!-- .entry-summary --> </article><!-- #post-## -->
Ah, you can’t change
the_title()
torelevanssi_the_title()
– it’s not equivalent. It doesn’t have the same parameters, so the link won’t work if you userelevanssi_the_title()
.It would need to be used like this:
printf('<h2 class="entry-title"><a href="%s" rel="bookmark">%s</a></h2>', esc_url( relevanssi_get_permalink() ), relevanssi_the_title() );
My bad. I did not pickup on the fact that they weren’t equivalent. My apologies. I made the change you outlined above and the link now appears with appropriate highlighting, however the document title appears twice. The first appearance is before the <h2.
Using the_title the html produced is:
<header class="entry-header"> <div class="entry-meta"> <span class="screen-reader-text">Posted on</span> <a href="https://192.168.2.100/annual-meeting-may-25-2012/" rel="bookmark"><time class="entry-date published" datetime="2012-05-25T14:21:18-08:00">May 25, 2012</time><time class="updated" datetime="2016-02-01T14:26:22-08:00">February 1, 2016</time></a><span class="edit-link"><a class="post-edit-link" href="https://192.168.2.100/wp-admin/post.php?post=5377&action=edit">Edit<span class="screen-reader-text"> "Annual Meeting – May 25, 2012"</span></a></span> </div><!-- .entry-meta --> <h2 class="entry-title"><a href="https://192.168.2.100/annual-meeting-may-25-2012/?highlight=annual" rel="bookmark">Annual Meeting – May 25, 2012</a></h2> </header><!-- .entry-header -->
Using the code you provided, the HTML is:
<header class="entry-header"> <div class="entry-meta"> <span class="screen-reader-text">Posted on</span> <a href="https://192.168.2.100/annual-meeting-may-25-2012/" rel="bookmark"><time class="entry-date published" datetime="2012-05-25T14:21:18-08:00">May 25, 2012</time><time class="updated" datetime="2016-02-01T14:26:22-08:00">February 1, 2016</time></a><span class="edit-link"><a class="post-edit-link" href="https://192.168.2.100/wp-admin/post.php?post=5377&action=edit">Edit<span class="screen-reader-text"> "Annual Meeting – May 25, 2012"</span></a></span> </div><!-- .entry-meta --> <span style='text-decoration: underline; color:red;'>Annual</span> Meeting - May 25, 2012<h2 class="entry-title"><a href="https://192.168.2.100/annual-meeting-may-25-2012/?highlight=annual" rel="bookmark"><span style='text-decoration: underline; color:red;'>Annual</span> Meeting - May 25, 2012</a></h2> </header><!-- .entry-header -->
Ah, sorry, my bad. The correct form would be:
printf('<h2 class="entry-title"><a href="%s" rel="bookmark">%s</a></h2>', esc_url( relevanssi_get_permalink() ), relevanssi_the_title( false ) );
as
relevanssi_the_title()
echoes out the title by default, and we don’t want that here.Mikko, Thank you for your GREAT support. In case other users are looking to add pagination, the_posts_pagination() function does not work with custom queries. I tried using the WP-Paginate but it doesn’t play well with the results of the query. If you choose to put the pagination display below the results it is placed below the first result <h2>. Pagination by BestWebSoft works correctly. Thanks again for your help.
- The topic ‘Title highlight removes link’ is closed to new replies.