How to share post?

时间:2014-04-23 作者:bear

我发现这段代码将在社交媒体上共享我的帖子,但现在它共享整个页面,而不是单个帖子。如何使其仅共享个人帖子?我知道它的一些东西与url我的代码,但不知道该改成什么。

这是帖子的循环。

<!--<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>-->

1 个回复
SO网友:Sam

您需要将URL路径替换为:

<?php the_permalink(); ?>
还可以考虑将每个按钮的标题或说明文本替换为:

<?php the_title(); ?>
下面是一个示例:

<!-- 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>
此代码已在single中进行了测试。带有标准循环的php。

结束

相关推荐

Query posts by content lenght

我使用下面的代码每6小时随机发布一篇我的网站帖子。if ( false === ( $social_trans_post_id = get_transient( \'$social_trans_post_id\' ) ) ) { $args = array(\'numberposts\' => 1, \'orderby\' => \'rand\'); $social_trans = get_posts($args)