Support » Plugin: Column Posts » Facebook Like button in Excerpt

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author lebasca

    (@lebasca)

    Hi Pharaoh145,

    Yes, it is possible but you might need to choose two columns instead of three columns in my plugin options because part of the Facebook button messages go beyond of the column width. Or you might want to adjust the width of the columns. It all depends on your web page layout.

    This is the solution that I offer you:
    1. Open the header.php file located in your theme folder.
    2. Add the following code after the body tag:
    <?php my_body_function() ?>
    3. Open the functions.php file located in your theme folder.
    4. Add the following code after the <?php line:
    remove_filter(‘get_the_excerpt’, ‘wp_trim_excerpt’);
    add_filter(‘get_the_excerpt’, ‘custom_trim_excerpt’);

    function custom_trim_excerpt($text = ”)
    {
    $raw_excerpt = $text;
    if ( ” == $text ) {
    $text = get_the_content(”);
    $excerpt_length = apply_filters(‘excerpt_length’, 55);
    $excerpt_more = apply_filters(‘excerpt_more’, ‘ ‘ . ‘[…]’);
    $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    }
    //I’m assuming that your are putting my plugin in your front page but you can change the function “is_front_page” for another one depending on your needs.
    if ( is_front_page() ) return ‘<div class=”fb-like” data-href=”‘ .get_permalink() .'” data-send=”true” data-width=”450″ data-show-faces=”true”></div>’ .$text;
    else return apply_filters(‘wp_trim_excerpt’, $text, $raw_excerpt);
    }

    function my_body_function(){
    do_action(‘my_body_function’);
    }
    add_action(‘my_body_function’,’add_facebook_sdk’);
    function add_facebook_sdk(){
    echo ‘<div class=”fb-root”></div>’.
    ‘<script>(function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = “//connect.facebook.net/en_US/all.js#xfbml=1”;
    fjs.parentNode.insertBefore(js, fjs);’.
    “}(document, ‘script’, ‘facebook-jssdk’));</script>”;
    }

    Hope it works for you.

    Plugin Author lebasca

    (@lebasca)

    Also, you can adjust the width of the Facebook button by modifying the value for data-width inside of the div tag with class=”fb-like”. The code is in the function custom_trim_excerpt provided in my previous reply.

    This is the line of code to look for:
    if ( is_front_page() ) return ‘<div class=”fb-like” data-href=”‘ .get_permalink() .'” data-send=”true” data-width=”450” data-show-faces=”true”></div>’ .$text;
    else return apply_filters(‘wp_trim_excerpt’, $text, $raw_excerpt);

    @lebasca thanks for this. Unfortunately I have been unable to get this working. I would like to add this to all my excerpts not just the front page (I am manually calling posts on various pages. How would I go about changing the script so that the Fb like appears on all excerpts in addition to the front page? Thanks in advance!

    Plugin Author lebasca

    (@lebasca)

    Hi there,

    In the code above (my first reply), look for the function “is_front_page” and remove the following code:
    if ( is_front_page() ) return ‘<div class=”fb-like” data-href=”‘ .get_permalink() .'” data-send=”true” data-width=”450″ data-show-faces=”true”></div>’ .$text;
    else return apply_filters(‘wp_trim_excerpt’, $text, $raw_excerpt);

    and replace with this code:
    return ‘<div class=”fb-like” data-href=”‘ .get_permalink() .'” data-send=”true” data-width=”450″ data-show-faces=”true”></div>’ .$text;

    By doing this, the FB buttons will appear in any page. But if you want to show the FB buttons in specific pages, you can use these functions: is_front_page(), is_page(), is_category() or is_archive().

    Hope this helps you,
    Lebasca

    Thread Starter Pharaoh145

    (@pharaoh145)

    Hi Lebasca,

    I know its been months but i wanted to say thanks for your lengthy reply.

    I had given up on this but I stumbled back upon this yesterday and tried your code above.

    It did indeed put the LIKE button into my post excerpt (the ONLY plugin so far that has succeeded at this for me).

    However, the button was placed after the title and before the excerpt text.
    I thought i could fix this with CSS and seemingly exhausted my options.

    Is this something that PHP can fix? I want the button (and ideally other social share buttons) to be at the end of the excerpt text.

    Thank you in advance for your help.

    Plugin Author lebasca

    (@lebasca)

    To place the button at the end of the excerpt, change the function custom_trim_excerpt given lines above:
    return $text .'<div class=”fb-like” data-href=”‘ .get_permalink() .'” data-send=”true” data-width=”450″ data-show-faces=”true”></div>’;

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Facebook Like button in Excerpt’ is closed to new replies.