• The wp-Typography plugin cannot use any classes from the HTML to exclude elements from the wp-Typography rules, because it doesn’t see the HTML.

    So the span with a class must be in the_title, as meant by the_title hook.
    The post title must be included in that span.

    And then I can declare that span at the exclusion rules of wp-Typography.

    This is what I have currently:

    function add_span($title, $id) {
    
        return $title .' <span class="title_span">' .get_post_meta($post_id, $key, $single) ."</span>";
    
    }
    
    add_filter('the_title', 'add_span', 10, 2);

    But this code assumes that I store my span as a meta value, but I am not.
    I just want to put the title in a span with a class with the the_title hook, after it’s retrieved from the database.
    What would be the correct code for that?

    Thanks!

    Ah, would this be correct?

    function add_span($title) {
    
        return $title .' <span class="title_span">'."</span>";
    
    }
    
    add_filter('the_title', 'add_span', 10, 1);
    • This topic was modified 2 years, 9 months ago by berry metal.
    • This topic was modified 2 years, 9 months ago by berry metal.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter berry metal

    (@erikalleman)

    I found something else:

    function the_title( $before = '<span class="wpt">', $after = '<span>', $echo = true ) {
        $title = get_the_title();
     
        if ( strlen( $title ) == 0 ) {
            return;
        }
     
        $title = $before . $title . $after;
     
        if ( $echo ) {
            echo $title;
        } else {
            return $title;
        }
    }

    Which is the better code, the first or the second?

    Your result should look like this: <span class="noTypo">Your Title</span>. You need to use add_filter.

    Thread Starter berry metal

    (@erikalleman)

    Thanks.

    Thread Starter berry metal

    (@erikalleman)

    is this correct?

    function noTypo_title ( $before = '<span class="noTypo">', $after = '<span>', $echo = true ) {
        $title = get_the_title();
     
        if ( strlen( $title ) == 0 ) {
            return;
        }
     
        $title = $before . $title . $after;
     
        if ( $echo ) {
            echo $title;
        } else {
            return $title;
        }
    }
    
    add_filter( 'the_title', noTypo_title );
    
    Thread Starter berry metal

    (@erikalleman)

    I realized I need to include a conditional so that only titles in a certain grid are getting wrapped in the span,
    and this is my current code:

    function noTypo_title ( $before = '<span class="noTypo">', $after = '</span>', $echo = true, $grid_name ) {
    
    $title = get_the_title();
    
    if ( strlen( $title ) == 0 ) {
    
    return;
    
    }
    
    if ( 'related' === $grid_name && ! empty( $related ) ) {
    
    $title = $before . $title . $after;
    
    if ( $echo ) {
    
    echo $title;
    
    } else {
    
    return $title;
    
    }
    
    }
    
    }
    
    add_filter( 'the_title', noTypo_title, 2 );

    But i think this is wrong.
    Could someone help me out with a correction?

    Thanks in advance!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How do I put the post title in a span that has a class using the the_title hook?’ is closed to new replies.