• Hi,

    I’d like to automatically add read more tags after x amount of words/characters to my blog with the exception of one category.

    The category (social) will contain embedded Tweets and automatically adding a read more tag breaks the Twitter card and stops it from showing up in full on the post listing page. When the blog page is set to show full content, the card shows up well on the post listing page. See screenshot here.

    Any ideas on how I can do this or any alternative methods suggested are welcome.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi ososoba,

    Try this code

    Add this to your function.php file

    function ncej_excerpt_length() { return 40; }
    function ncej_excerpt_more() { return '...'; }
    
    function ncej_excerpt() {
    
    add_filter('excerpt_length',  'ncej_excerpt_length');
    add_filter('excerpt_more', 'ncej_excerpt_more');
    
    $content = get_the_excerpt();
    $content = apply_filters('wptexturize', $content);
    apply_filters('convert_chars', $content);
    
    echo $content;
    }

    Now use this in your loop

    if (have_posts()) {
    while (have_posts()) { the_post;
    
    ncej_excerpt();
    }
    if (!is_single() && strlen(get_the_content()) > 0 ) {
    // your readmore link goes here.
    }
    
    }

    Cheers.

    Thread Starter ososoba

    (@ososoba)

    Thank you for the snippet @ncej

    But the snippet doesn’t take into account the social category. I’d like to show the full content for any post that is in the category “social”

    Any idea how I can do this?

    Moderator bcworkz

    (@bcworkz)

    Modify your loop to include a conditional that checks the post categories against your social category’s ID.

    $cats = wp_get_post_categories();
    if ( is_single() || in_array( $social_id, $cats )) {
      the_content();
    } else {
      ncej_excerpt();
      // your readmore link goes here.
    }

    Either hardcode the social ID in place of $social_id or assign the ID to the variable before the loop starts, or use a constant, or…

    Hi ososoba,

    Make a duplicate from your category.php or archive.php if no category.php. Rename the duplicate to “category-social.php” and use the_content() in your loop.

    That’s all!

    Thread Starter ososoba

    (@ososoba)

    Thanks @ncej and @bcworkz

    I’d be trying this in the next few hours, hopefully it works just fine.

    Will feedback here when I do.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Automatic "Read more" except for one category’ is closed to new replies.