Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to set up code to submit a post to StumbleUpon, from my Rothmania site.

The code reads href="http://www.stumbleupon.com/submit?url=<?php the_permalink(); ?>&title=<?php the_title(); ?>" alt="" title="">

and when on a single post view, it pulls the info just fine, offering to submit that particular post. When on the home page, I'd like it to submit the most recent, top post, right now it's loading the last one at the bottom of the page.

Note: this is hand coded, unrelated to the theme, and not using a plugin. Any help here will be appreciated.

Edit -

I now have this code -

<?php
$su_link = $su_title = '';

if (is_single()) {
    // We're showing a single blog post
    // => take the data and deal with it
    $su_link = get_permalink();
    $su_title = get_the_title();
} elseif (is_home()) {
    // We're showing the 'home' archive
    // => take the first post's data and deal with it
    $ID = $GLOBALS['posts'][0]->ID;
    $su_link = get_permalink($ID);
    $su_title = get_the_title($ID);
}

if ('' !== $su_link) {
    // We have data
    // => generate the link
    echo '<a href="http://www.stumbleupon.com/submit?url='.urlencode($su_link).'&title='.urlencode($su_title).'" '
        .'title="'.$su_title.'">'
        .'Link'
        .'</a>';
}
?>

<html>
<head>
  <title>CoffeeCup Image Mapper map file</title>
  <meta name="generator" content="CoffeeCup Image Mapper">
</head>
<body>

<!-- Created by CoffeeCup Image Mapper (www.coffeecup.com) -->

<!-- Beginning of Client Side Image Map -->
<img src="http://rothmania.net/wp-content/uploads/2013/02/sociallinks.jpg" USEMAP="#sociallinks" BORDER=0>
<map name="sociallinks">
  <area name="facebook" shape="rect" coords="19,0,76,48" href="https://www.facebook.com/joetaxpayer"  alt="" title="">
  <area name="twitter" shape="rect" coords="73,0,129,48" href="https://twitter.com/JoeTaxpayerBlog"  alt="" title="">
  <area name="stumble" shape="rect" coords="126,0,179,48" href="http://www.stumbleupon.com/submit?url=<?php the_permalink(); ?>&title=<?php the_title(); ?>"  alt="" title="">
  <area name="rss" shape="rect" coords="177,0,243,48" href="http://rothmania.net/feed/"  alt="" title="">
</map>
<!-- End of Client Side Image Map -->
</body>
</html>

When I temporarily loaded to the site it offers a 'link' that works perfectly, but I'm staring at this code, not understanding how to link from the proper icon, instead of the word 'link'. I appreciate the help here, and yes, I'm in over my head.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

The problem is that the functions the_title(), the_ID() etc. access the current post, which is the last post after the query has been handled completely.

Put the following either

  • in a text widget, wrapped in <?php and ?>, while having PHP execution enabled for text widgets (e.g., by using Exec-PHP and the like);
  • in a PHP Code Widget (again wrapped in <?php and ?>);
  • hard-coded into your sidebar.php (or whatever your sidebar template file may be named);
  • in your functions.php, wrapped in a function, then call the function wherever you want.

Share the Most Recent Post

// EDIT regarding your last comment:

<?php
    $su_link = $su_title = $su_href = '';

    if (is_single()) {
        // We're showing a single blog post
        // => take the data and deal with it
        $su_link = get_permalink();
        $su_title = get_the_title();
    } elseif (is_home()) {
        // We're showing the 'home' archive
        // => take the first post's data and deal with it
        $ID = $GLOBALS['posts'][0]->ID;
        $su_link = get_permalink($ID);
        $su_title = get_the_title($ID);
    }

    if ('' !== $su_link) {
        // We have data
        // => generate the link
        $su_href = 'http://www.stumbleupon.com/submit?url='.urlencode($su_link).'&title='.urlencode($su_title);
    }
?>


<map name="sociallinks">
    <area name="stumble" shape="rect" coords="126,0,179,48" href="<?php echo $su_href; ?>" title="<?php echo $su_title; ?>" />
</map>
share|improve this answer
    
Ok, I'll admit my ignorance. Does this code go in the same spot as the test widget, i.e. the area the images are referenced, etc. or in custom PHP? –  JoeTaxpayer Apr 20 '13 at 1:02
    
I just updated my answer for you didn't say before that you wanted to use it inside a widget/sidebar. Of course, you can (or even may have to) customize the above code. It's a valid starting point, though. –  t f Apr 20 '13 at 11:37
1  
PERFECT! - There is a special place in Heaven for coders with patience for those of us who are a bit ignorant. This was a fix I'd been seeking for some time now. Much appreciated. –  JoeTaxpayer Apr 23 '13 at 17:08

get_permalink will accept a post ID or post object, so with a bit of rewriting it should do what you want.

$perm = (is_home() && !empty($posts[0])) ? get_permalink($posts[0]) : get_permalink(); 
$title = (is_home() && !empty($posts[0])) ? get_the_title($posts[0]->ID) : get_the_title(); ?>

<a href="http://www.stumbleupon.com/submit?url=<?php echo urlencode($perm); ?>&title=<?php echo urlencode($title); ?>" alt="" title="">test</a>

$posts[0] should be the first post in the main query. If you have created a secondary query that part could be wrong.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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