0

I found this code that will share my post in social medias but right now it share the whole page and not the individual post. How do I make it share only the individual post? I know its something with the url i the code but dont know what to change it to.

This is the loop for the post.

<!--<the loop>-->
            <?php $wp_query = new WP_Query( array( 
                                                "cat" => "1", 
                                                "posts_per_page" => "15", 
                                                "post_type" => "post",
                                                "paged" => get_query_var('paged') ? get_query_var('paged') : 1
                                                )                                               
                                        );
            while( $wp_query->have_posts() ) : $wp_query->the_post(); ?>

            <div <?php post_class(); ?>>

                <!--<cat title>-->
                <div class="tm_catpost_titles">
                        <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
                            <h1 class="tmpost-<?php the_ID(); ?>">
                            <?php $tit = the_title('','',FALSE); echo substr($tit, 0, 150); if (strlen($tit) > 150) echo " ..."; ?>
                            </h1>
                        </a>
                </div>
                <!--</cat title>-->     
<!--<cat item>-->
                <div class="tm_cat_item">
                    <div class="tmpost-<?php the_ID(); ?>-item">
                        <div id="catpostup">    
                        <div class="tm_catpost_item_1">
                        <?php _e('Av', 'tm_myid_text'); ?> <?php the_author(); ?>
                        </div>

                        <div class="tm_catpost_item_2">
                        I <?php $category = get_the_category(); echo $category[0]->cat_name; ?>
                        </div>

                        <div class="tm_catpost_item_3">
                        <?php the_time('M jS, Y') ?>
                        </div>


                    </div>
</div>
                </div>
                <!--</cat item>-->

                <!--<cat image>-->
                <div class="tm_cat_image">
                <?php if (has_post_thumbnail()) { ?>
                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                    <?php the_post_thumbnail( 'Full' ); ?>
                    </a>
                <?php } else { ?>
                <?php } ?>
                </div>
                <!--</cat image>-->

                <div class="tm_cat_desc_home5">
                    <div class="tmpost-desc">
                    <?php the_content(); ?>
                    </div>

                    <div class="tm_cat_readmore">

                        <div class="tmpost-readmore">
                            <a href="<?php the_permalink() ?>" target="_blank">
                                 <?php comments_number(__('(0) Kommentarer ', 'tm_myid_text'), __('(1) Kommentar', 'tm_myid_text'), __( '(%) Kommentarer', 'tm_myid_text') );?> 
                            </a> 


                        </div>

                    </div>
                <!-- I got these buttons from simplesharebuttons.com -->
<div id="share-buttons">

<!-- Facebook -->
<a href="https://www.facebook.com/sharer/sharer.php?u=<?php the_permalink(); ?>&text=<?php the_title(); ?>"><img src="http://alskadehem.se/wp-content/uploads/2014/04/facebook.png" alt="Facebook" /></a>

<!-- Twitter -->
<a href="http://twitter.com/share?url=<?php the_permalink(); ?>&text=<?php the_title(); ?>" target="_blank"><img src="http://www.simplesharebuttons.com/images/somacro/twitter.png" alt="Twitter" /></a>

<!-- Google+ -->
<a href="https://plus.google.com/share?url=<?php the_permalink(); ?>&text=<?php the_title(); ?>" target="_blank"><img src="http://www.simplesharebuttons.com/images/somacro/google.png" alt="Google" /></a>

<!-- Tumblr+ -->
<a href="https://plus.google.com/share?url=http://www.alskadehem.se" target="_blank"><img src="http://alskadehem.se/wp-content/plugins/simple-share-buttons-adder/buttons/somacro/tumblr.png" alt="Tumblr" /></a>

<!-- Pinterest -->
<a href="javascript:void((function()%7Bvar%20e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','http://assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)%7D)());"><img src="http://www.simplesharebuttons.com/images/somacro/pinterest.png" alt="Pinterest" /></a>

<!-- Email -->
<a href="mailto:?Subject=Simple Share Buttons&Body=I%20saw%20this%20and%20thought%20of%20you!%20 <?php the_permalink(); ?>&text=<?php the_title(); ?>"><img src="http://www.simplesharebuttons.com/images/somacro/email.png" alt="Email" /></a>

</div>
                </div>
            </div>
<?php if ($count == 1) : ?>
<div id="ad"></div>
<?php endif; $count++; ?>
            <?php endwhile; ?>
            <!--</the loop>-->
7
  • Please trim your code to highlight just the post-sharing. Commented Apr 23, 2014 at 14:37
  • Did you mean like that?
    – bear
    Commented Apr 23, 2014 at 14:45
  • Much better, thanks. Now: assuming you want to replace "url=http://www.alskadehem.se" with a post-specific URL, what have you tried? Commented Apr 23, 2014 at 14:49
  • There is a more elegant way to do this. Have a look at my answer here. Commented Apr 23, 2014 at 14:54
  • 1
    I would suggest that you need to have a look at a social sharing plugin. This would really make your job easier. There are a couple out there, you will just need to download a few and test which one works best Commented Apr 23, 2014 at 16:39

1 Answer 1

1

You need to replace the URL paths with:

<?php the_permalink(); ?>

Also consider replacing the title or description text for each button with:

<?php the_title(); ?>

Here's an example:

<!-- Twitter -->
<a href="http://twitter.com/share?url=<?php the_permalink(); ?>&text=<?php the_title(); ?>" target="_blank"><img src="http://www.simplesharebuttons.com/images/somacro/twitter.png" alt="Twitter" /></a>

This code has been tested within single.php with a standard loop.

8
  • HOw do I change the one for pinterest and mail to? I want them to be able to mail the post not open a blank email?
    – bear
    Commented Apr 23, 2014 at 14:50
  • I tried your code for facebook and twitter but i just get an error and it dosent work.
    – bear
    Commented Apr 23, 2014 at 14:58
  • "HOw do I change the one for pinterest and mail to" - that question is not specific to WordPress. Commented Apr 23, 2014 at 15:01
  • What error message are you getting? + What @ChipBennett said.
    – Sam
    Commented Apr 23, 2014 at 15:02
  • the error message i get is the page cannot be found. oh sorry i thought it was wordpress related if i wanted to be able to email the post. my bad
    – bear
    Commented Apr 23, 2014 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.