Is there a way to exclude the current post in this filter?
I tried something like this:
function add_notypo_to_title( $title ) {
return "<span class='noTypo'>$title</span>";
}
$current_id = function_exists( 'vcex_get_the_ID' ) ? vcex_get_the_ID() : get_the_ID();
if $title !== get_the_title( $current_id ) ) {
add_filter( 'the_title', 'add_notypo_to_title', 10, 1 );
}
But it results in a critical error.
I got inspired from this snippet:
add_shortcode( 'bbpresscomments', function() {
$output = '';
$current_id = function_exists( 'vcex_get_the_ID' ) ? vcex_get_the_ID() : get_the_ID();
$topics = new WP_Query( array(
'post_type' => 'topic',
'posts_per_page' => -1,
'fields' => 'ids',
) );
if ( $topics->have_posts() ) {
foreach( $topics->posts as $topic ) {
if ( get_the_title( $topic ) == get_the_title( $current_id ) ) {
$output .= do_shortcode( '[bbp-single-topic id="' . intval( $topic ) . '"]' );
}
}
}
return $output;
} );
Vcex is my theme prefix.
Then I tried something like this to limit adding the class to the title in my related posts (that would be the goal – but they are the same post type as the posts where I do want to widowing):
function add_notypo_to_title( $title ) {
return "<span class='noTypo'>$title</span>";
}
$referer = wp_get_referer();
$post_id = wp_doing_ajax() ? url_to_postid( $referer ) : $post->ID;
$related = get_post_meta( $post_id, 'related_post_ids', true );
if (! is_admin() ) {
if ( empty( $related ) ) {
add_filter( 'the_title', 'add_notypo_to_title', 10, 1 );
}
}
But this also didn’t work.
I have also tried this to exclude the current post:
function add_notypo_to_title( $title ) {
return "<span class='noTypo'>$title</span>";
}
global $wp_query;
$exclude = $wp_query->post->ID;
if( $exclude != get_the_ID() ) {
add_filter( 'the_title', 'add_notypo_to_title', 10, 1 );
}
But this code will not add the class anywhere.