Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1149718
    necxelos
    Participant

    Hello again

    I need a little help with making two pieces of code (which I got in my other topics) work together. Both pieces of code occupy functions.php file.

    1. First code is supposed to make the excerpt maintain all the formating the full post have. Here is the code:

    function wpse_allowedtags() {
        // Add custom tags to this string
            return '<script>,<style>,<br>,<em>,<i>,<ul>,<ol>,<li>,<a>,,<img>,<video>,<audio>';
        }
    
    if ( ! function_exists( 'wpse_custom_wp_trim_excerpt' ) ) : 
    
        function wpse_custom_wp_trim_excerpt($wpse_excerpt) {
        global $post;
        $raw_excerpt = $wpse_excerpt;
            if ( '' == $wpse_excerpt ) {
    
                $wpse_excerpt = get_the_content('');
                $wpse_excerpt = strip_shortcodes( $wpse_excerpt );
                $wpse_excerpt = apply_filters('the_content', $wpse_excerpt);
                $wpse_excerpt = str_replace(']]>', ']]>', $wpse_excerpt);
                $wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags()); /*IF you need to allow just certain tags. Delete if all tags are allowed */
    
                //Set the excerpt word count and only break after sentence is complete.
                    $excerpt_word_count = 75;
                    $excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);
                    $tokens = array();
                    $excerptOutput = '';
                    $count = 0;
    
                    // Divide the string into tokens; HTML tags, or words, followed by any whitespace
                    preg_match_all('/(<[^>]+>|[^<>\s]+)\s*/u', $wpse_excerpt, $tokens);
    
                    foreach ($tokens[0] as $token) { 
    
                        if ($count >= $excerpt_word_count && preg_match('/[\,\;\?\.\!]\s*$/uS', $token)) {
                        // Limit reached, continue until , ; ? . or ! occur at the end
                            $excerptOutput .= trim($token);
                            break;
                        }
    
                        // Add words to complete sentence
                        $count++;
    
                        // Append what's left of the token
                        $excerptOutput .= $token;
                    }
    
                $wpse_excerpt = trim(force_balance_tags($excerptOutput));
    
                    $excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . '&nbsp;&raquo;&nbsp;' . sprintf(__( 'Read more about: %s &nbsp;&raquo;', 'wpse' ), get_the_title()) . '</a>';
                    $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end); 
    
                    //$pos = strrpos($wpse_excerpt, '</');
                    //if ($pos !== false)
                    // Inside last HTML tag
                    //$wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); /* Add read more next to last word */
                    //else
                    // After the content
                    $wpse_excerpt .= $excerpt_end; /*Add read more in new paragraph */
    
                return $wpse_excerpt;   
    
            }
            return apply_filters('wpse_custom_wp_trim_excerpt', $wpse_excerpt, $raw_excerpt);
        }
    
    endif; 
    
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'wpse_custom_wp_trim_excerpt');

    2. Second code is supposed to remove “Read More” link at the bottom of a post and put “To read more, click the Featured Image or Post Title” text in the same place instead. Here is the code:

    function remove_x_excerpt_string() {
      remove_filter( 'excerpt_more', 'x_excerpt_string' );
    }
    
    add_filter('after_setup_theme', 'remove_x_excerpt_string');
    
    function new_excerpt_more($more) {
      global $post;
      return '<div class=”readmore_alternative”><strong>To Read More click article title or featured image!</strong></div>';
    }
    
    add_filter('excerpt_more', 'new_excerpt_more');

    3. Those two codes don’t work with each other (most likely due to the fact that they both replace previous excerpt-making functions). Could You please tell me how to incorporate second one into first one so they do work as a whole?

    Thanks in advance and have a good day 🙂

    #1149898
    Rad
    Moderator

    Hi there,

    Thanks for writing in.

    You’ll just need to replace this line,

    $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);

    with this

    $excerpt_more = 'To read more, click the Featured Image or Post Title';

    Cheers!

    #1149905
    necxelos
    Participant

    That doesn’t do anything BUT following Your logic I tried to replace:

    $excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . '&nbsp;&raquo;&nbsp;' . sprintf(__( 'Read more about: %s &nbsp;&raquo;', 'wpse' ), get_the_title()) . '</a>';

    With this:

    $excerpt_end = 'To read more, click the Featured Image or Post Title';

    a) And surprisingly it works. Any chance that this is what You meant, and just pasted wrong line here by mistake? 🙂

    Either way if what I did is correct (I hope it is) I only need now for this text to have it’s own CSS class to target (right now it has the same style as excerpt text which makes it very hard to distinguish if not impossible at all).

    b) [EDIT] Again by trial/error method I added CSS class to this text using joker tag. It looks like this now:

    ‘$excerpt_end = <span class=”Read_More_Text”> To read more, click the Featured Image or Post Title </span>’;

    Is this correct too? I will need to center it and bold it most likely…

    #1150154
    Lely
    Moderator

    Hi There,

    a.) Yes. what you did is correct.
    b.) You’ve got it:
    $excerpt_end = <span class="custom-readmore"> To read more, click the Featured Image or Post Title </span>'

    .custom-readmore{
       font-weight:bold;
    }

    To center it, we need to add text-align:center; on the container of the span, please share your site URL.

    Hope this helps.

    #1150312
    necxelos
    Participant

    b) I didn’t even need container. I just used paragraph tags, instead of joker tags to center it, and it works just fine.

    Thanks again Guys and Gals, You are the best! 🙂

    #1150313
    Christian
    Moderator

    You’re welcome. Glad we could help. 🙂

  • <script> jQuery(function($){ $("#no-reply-1149718 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>