0
votes

Im using custom WP_Query on a single post page (it shows a list of recent posts):

$recent = new WP_Query($args);
while ($recent->have_posts()) {
    $recent->the_post();
    get_template_part('template-parts/content', get_post_format());
}

The problem is: in template I check is_single() condition, which always returns true (with no arguments passed). When I try is_single($post), sometimes it returns true, sometimes false.

UPD 1:

I use template-parts/content.php template in my theme folder. The code is something like that:

    if ( is_single($post) ) :
        the_title( '<h1 class="entry-title pb-1 '.ta_content_paragraphs_paddings().'">', '</h1>' );
    else :
        the_title( '<h2 class="entry-title pb-1 '.ta_content_paragraphs_paddings().'"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
    endif; ?>

UPD 2: I found why it returns true sometimes with $post given as argument, it happens when my single post title is the same as the recent post title. This part of code in class-wp-query, function is_single:

} elseif ( in_array( $post_obj->post_title, $post ) ) { // returns here
            return true;

Is it normal to check post title for telling me its a single page?

1
where and how you used is_single? provide that code, too. - Samvel Aleqsanyan
update your question with this part of code. and what is your '$post'? where you defined it? - Samvel Aleqsanyan
$post is a global variable, it contains a single post object (on the page I am currently in) - Magnus TechApple

1 Answers

0
votes

I was able to fix this with providing not $post, but $post->ID to is_single:

is_single($post->ID);

I dont think matching $post->post_title in a core of wordpress is a good idea to tell its a single post page. Maybe I am wrong and misunderstand this.