How do I exclude the current post from a filter hook function?
-
Hi there,
I am adding a class to the title, except the backend:
function add_notypo_to_title( $title ) { return "<span class='noTypo'>$title</span>"; } if (! is_admin() ) { add_filter( 'the_title', 'add_notypo_to_title', 10, 1 ); }
I want to exclude the current post title, the title of the post that is currently being viewed.
I have tried the following:
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.
Something else that would achieve the same result is to limit adding the class to only my related posts:
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 results in a critical error.
Excluding the current post would be a more generic solution, I guess.
What is the correct way to exclude the current post?
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
- The topic ‘How do I exclude the current post from a filter hook function?’ is closed to new replies.