• Resolved Anonymous User 17976131

    (@anonymized-17976131)


    Anyone have any ideas on what sort of custom function could be added to functions.php to in essence, when a post doesn’t have a title, trim 8 words from the post’s content and use that in the <title> tag?

    • This topic was modified 4 years, 9 months ago by Anonymous User 17976131.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Anonymous User 17976131

    (@anonymized-17976131)

    (To clarify: it’s not so much the trimming words from the content that I worry about; it’s the conditional testing for the right things, and what it should be hooking into in order to work.)

    Thread Starter Anonymous User 17976131

    (@anonymized-17976131)

    FWIW, I don’t remember how I found this, but this is what worked.

    // Fake a title-tag for untitled posts
    add_filter( 'document_title_parts', function( $title_parts_array ) {
            if ( is_single() && empty($title_parts_array['title']) ) {
                    global $post;
                    $content = $post->post_content;
                    $trimmed = wp_trim_words( $content, 8, ' … ' );
                    $trimmed = esc_html( $trimmed );
                    $title_parts_array['title'] = $trimmed;
            }
            return $title_parts_array;
    } );
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘head/title tag for posts without titles?’ is closed to new replies.