最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

permalinks - the_permalink on the homepage vs posts for sharing links

programmeradmin0浏览0评论

Instead of weighing the site down with plugins I've hardcoded a couple of sharing links.

I've used the_permalink to create the links, in the sidebar, which work ok on posts and pages, but on the homepage it shows the link of the last post, not the homepage.

The code I'm using is pretty basic:

<a href=".php?u=<?php the_permalink();?>&amp;t=<?php the_title(); ?>" target="_blank">Share this on Facebook</a>

How can I get the homepage sharing link to use the homepage url, while also working for the posts and pages?

Instead of weighing the site down with plugins I've hardcoded a couple of sharing links.

I've used the_permalink to create the links, in the sidebar, which work ok on posts and pages, but on the homepage it shows the link of the last post, not the homepage.

The code I'm using is pretty basic:

<a href="http://www.facebook/sharer.php?u=<?php the_permalink();?>&amp;t=<?php the_title(); ?>" target="_blank">Share this on Facebook</a>

How can I get the homepage sharing link to use the homepage url, while also working for the posts and pages?

Share Improve this question asked Mar 29, 2015 at 14:25 Alex_TJAlex_TJ 11 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 1

This code displays takes get_permalink() on a single page and get_home_url() on the homepage. If a link is found the Share-Link will be displayed.

So the Link wouldn't be displayed right now on a category-page for example.

<?php
    if( is_home() ){
        $link = urlencode( get_home_url() );
        $title = urlencode( get_bloginfo( 'title' ) );
    }elseif( is_single() || is_page() ){
        $link = urlencode( get_permalink() );
        $title = urlencode( get_the_title() );
    }

    if( isset( $link ) ):
?>
<a href="http://www.facebook/sharer.php?u=<?php 
    echo $link;?>&amp;t=<?php echo $title; 
?>" target="_blank">Share this on Facebook</a>
<?php
    endif;
?>

Info:

  • get_home_url(): https://codex.wordpress/Function_Reference/get_home_url
  • is_home(): https://codex.wordpress/Function_Reference/is_home
  • is_single(): https://codex.wordpress/Function_Reference/is_single

I would turn this into a function to account for all conditions. Take the following:

function generate_facebook_sharer_link( $label = 'Share this on Facebook', $classes = '', $return = false ){
    if( is_singular() ){
        // If is a Single CPT, Post, Page, etc.
        $url   = get_permalink();
        $title = get_the_title();
    } else if( is_front_page() ){
        // If this is the "Main Page"
        $url   = home_url();
        $title = get_bloginfo( 'title' );
    } else {
        // All other Accounts (Archives, Blog Page, etc.)
        $url   = ( is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $title = get_bloginfo( 'title' );
    }

    $output = sprintf( '<a class="%s" href="http://www.facebook/sharer.php?u=%s&t=%s" target="_blank" rel="noopener noreferrer">%s</a>', $classes, urlencode( $url ), urlencode( $title ), $label );

    // Allow returning, default to echoing
    if( $return == true )
        return $output;

    echo $output;
}

This will output a link wherever generate_facebook_sharer_link(); is called. The is_singular() should catch all Custom Post Types, Posts, Pages, etc. is_front_page() will catch if it's the "main page", regardless of what the Reading Settings/Blog Page are, and then it falls back to building the current URI with appropriate protocol and setting the blog title.

This could also very simply be turned into a shortcode:

add_shortcode( 'facebook_share_link', function( $atts ){
    // Extract and Define Defaults
    extract( shortcode_atts( array(
        'label'   => 'Share this on Facebook',
        'classes' => '',
    ), $atts ) );

    if( is_singular() ){
        // If is a Single CPT, Post, Page, etc.
        $url   = get_permalink();
        $title = get_the_title();
    } else if( is_front_page() ){
        // If this is the "Main Page"
        $url   = home_url();
        $title = get_bloginfo( 'title' );
    } else {
        // All other Accounts (Archives, Blog Page, etc.)
        $url   = ( is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $title = get_bloginfo( 'title' );
    }

    $output = sprintf( '<a class="%s" href="http://www.facebook/sharer.php?u=%s&t=%s" target="_blank" rel="noopener noreferrer">%s</a>', $classes, urlencode( $url ), urlencode( $title ), $label );

    return $output;
} );

Which would allow you to plop [facebook_share_link] wherever you'd like it to show up.

发布评论

评论列表(0)

  1. 暂无评论