Support » Fixing WordPress » Social Share for post in index, always wrong URL

  • Hi. I’m attempting to make a social share (in this case a G+) button work for the individual posts on my WP home (index) page. For the individual pages themselves, this works fine:

    <g:plusone annotation=”inline” <?php echo the_permalink() ?> ></g:plusone>

    But of course this is wrong for the posts when displayed on the home page. I’ve tried the various formats
    suggested in other posts, including:

    <?php echo get_permalink();?>

    and various combinations using php specifications like:

    <?php echo $_SERVER[“HTTP_HOST”] . $_SERVER[“REQUEST_URI”] ?>

    None of them work. I must be missing something obvious. Any clues would be much appreciated. Thanks!

Viewing 13 replies - 1 through 13 (of 13 total)
  • Try this :
    <g:plusone annotation=”inline” data-href="<?php the_permalink() ?>"></g:plusone>

    Thanks. That just results in the LT/RT codes showing up on the rendered page, and it still doesn’t work.
    Here’s some more detail. On individual pages, this code works to display the G+ button and Follow badge:

    <g:plusone annotation=”inline” <?php echo the_permalink() ?> ></g:plusone>
    <!– Place this tag where you want the badgev2 to render. –>
    <div class=”g-follow” data-annotation=”bubble” data-height=”24″ data-href=”//plus.google.com/+[User]” data-rel=”author”></div>

    For the home page, this code does successfully display the follow badge at the end of each post on the home page:

    <!– Place this tag where you want the badgev2 to render. –>
    <div class=”g-follow” data-annotation=”bubble” data-height=”24″ data-href=”//plus.google.com/+[User]” data-rel=”author”></div>

    For the home page, if I provide an explicit URL as in:

    <g:plusone annotation=”inline” href=”http://www.blog.com/page001.html” ></g:plusone>

    Then the G+ button and count (for that one particular URL) is properly rendered at the end of every post
    displaying on the home page.

    The problem is getting the call to return the individual page URL into that construct on the home page, rather than an explicit URL, so the G+ call is correct for each individual page being displayed on the home page.

    Thanks again.

    • This reply was modified 2 years, 3 months ago by  ht39300.

    the the_permalink() will return the current page url unless you are using the the_post() function inside your posts loop, so we need to see the whole code of the LOOP so we can provide the correct answer.

    The code is being inserted via the Ad Inserter plugin — so I’m unsure of what you need to see from there. Thanks.

    You’re listing some posts on your homepage and you want each-one to have its own “G+” button, if this correct — I need to see the code you are using for listing posts in your HP.

    All I have is the code shown above. It’s in an Ad Inserter plugin tab, set to display “After Content”.

    I’d skip Ad Inserter for this and just put the code in my child theme index.php (or wherever) if I knew how — but I really don’t know enough about the WP architecture at this point.

    Good Luck 😉

    Remarkable that such a basic functionality is so clouded in unhelpful mystery and such a seemingly opaque architectural framework.

    Moderator Jan Dembowski

    (@jdembowski)

    Brute Squad and Volunteer Moderator

    @ht39300 I’ve closed and removed your other topic. Please do not create topics about your other topic, duplicate topics get closed or deleted when found.

    As mentioned above, the current URL is a function of The Loop and it works.

    https://developer.wordpress.org/reference/functions/get_permalink/

    But of course this is wrong for the posts when displayed on the home page.

    Just to clarify: you want the URL for insertion in the head of a post?

    Jan, my apologies for the dupe posting. What I’m trying to accomplish is having the same pair of items at the bottom of each post, irrespective of whether it’s being viewed single page or as a full item on the home page. One of these is a G+ Follow badge. It works fine in both contexts because it uses a static Google URL that is independent of the post URL. The other is the plusone button. It needs to act based on the URL of the current post. I have that working fine in the single page view instance. It’s the home page view instance where I have not been able to find a way to get the correct post URL to pass on to the G+ API as I do in the single page view instance. Thanks!

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    @ht39300 : The function “the_permalink” is what’s known as a “Loop” function. It will output the permalink for the current post when you are inside “The Loop”.

    https://codex.wordpress.org/The_Loop

    The Loop, in WordPress, is what displays posts. In a theme, it looks something like this:

    
    if ( have_posts() ) {
    	while ( have_posts() ) {
    		the_post(); 
    		//
    		// Post Content here
    		//
    	} // end while
    } // end if
    

    That place inside the while is the Loop. As you can see, it’s just looping through every post, calling the_post(), and then doing stuff to display that post.

    When you’re inside the Loop, then functions like “the_permalink” output stuff relative to that post. So yes, using your code will work there. It won’t work if you use the_permalink somewhere else than inside The Loop, or rather, it will give you unpredictable results.

    What the_post() does is to set up a global variable (specifically called $post) with information about the current post being displayed in The Loop. Then, those loop functions use that global $post variable to do their thing. So if you use a loop function outside the Loop, then you get whatever was last in that global $post variable.

    So, that’s the short of it. It’s not that you’re using the wrong code. You’re probably just putting it in the wrong place.

    Thanks. In fact, one of the things I tried earlier was declaring the global $post and then calling permalink with $post->ID. Unfortunately, that didn’t work either!

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Social Share for post in index, always wrong URL’ is closed to new replies.