News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

How put some html at the bottom of only the first entry of a topic?

Started by JollyGuru, May 06, 2015, 03:24:26 PM

Previous topic - Next topic

JollyGuru

I am trying to install 'Shareaholic - Sharebuttons':
https://shareaholic.com/publishers/sharing

I am requested to 1: insert some html "right before the closing </head> tag". And I think I can figure that out.

But then I am also instructed to 2: ' Insert this code snippet where you want your Share Buttons to appear:'
<div class='shareaholic-canvas' data-app='share_buttons' data-app-id='12341234'></div>

I want the buttons to appear only at the bottom of the firs entry of topics.
So, how can I make it so that this second code-snippet places buttons only at the bottom of the first entry of a topic?

Pipke

You want it on messageindex.template (on every first ) topic (inside) and not on the replies posts, right?
"If something is wrong, fix it if you can. But train yourself not to worry: Worry never fixes anything."

Click here to view my mods for SMF

Hey 👋 Did i helped... you like what i do. You can now buy me a coffee! ☕

margarett

Ads Management MOD can do that for you (IIRC, it's been some years since I used it...)
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

JollyGuru

Quote from: Pipke on May 07, 2015, 01:20:31 PM
You want it on messageindex.template (on every first ) topic (inside) and not on the replies posts, right?

Yes, this is correct.

Pipke

I posted earlier that it was on the messageindex.template, but that has to be the display.template file!

ok here you go:

In ../Themes/{your_theme}/Display.template.php

Code (find) Select

// Get all the messages...
while ($message = $context['get_message']())


Code (replace with) Select

// Get all the messages...
        $first = true;
while ($message = $context['get_message']())


then

Code (find) Select

echo '
<div class="inner" id="msg_', $message['id'], '"', '>', $message['body'], '</div>
</div>';


Code (replace with) Select

echo '
<div class="inner" id="msg_', $message['id'], '"', '>', $message['body'], '</div>';
if (($first) && $context['start'] == 0)
{
echo'<br><div class="shareaholic-canvas" data-app="share_buttons" data-app-id="12341234"></div>';
$first = false;
}
echo '</div>';
"If something is wrong, fix it if you can. But train yourself not to worry: Worry never fixes anything."

Click here to view my mods for SMF

Hey 👋 Did i helped... you like what i do. You can now buy me a coffee! ☕

Shambles

Or you could use $message['counter']

Code (replace) Select
echo ' <div class="inner" id="msg_', $message['id'], '"', '>', $message['body'], '</div>';
if ($message['counter'] == 0)
{
echo'<br><div class="shareaholic-canvas" data-app="share_buttons" data-app-id="12341234"></div>';
}
echo '</div>';

Pipke

Quote from: Shambles on May 08, 2015, 09:51:58 AM
Or you could use $message['counter']

Code (replace) Select
echo ' <div class="inner" id="msg_', $message['id'], '"', '>', $message['body'], '</div>';
if ($message['counter'] == 0)
{
echo'<br><div class="shareaholic-canvas" data-app="share_buttons" data-app-id="12341234"></div>';
}
echo '</div>';


even better :)
"If something is wrong, fix it if you can. But train yourself not to worry: Worry never fixes anything."

Click here to view my mods for SMF

Hey 👋 Did i helped... you like what i do. You can now buy me a coffee! ☕

JollyGuru

Thanks guys!  :D

As mentioned in my first post, I should also insert another code-snippet:

'This is your code. Insert it before the closing </head> tag of every page:'

<script type="text/javascript">
//<![CDATA[
  (function() {
    var shr = document.createElement('script');
    shr.setAttribute('data-cfasync', 'false');
    shr.src = '//dsms0mj1bbhn4.cloudfront.net/assets/pub/shareaholic.js';
    shr.type = 'text/javascript'; shr.async = 'true';
    shr.onload = shr.onreadystatechange = function() {
      var rs = this.readyState;
      if (rs && rs != 'complete' && rs != 'loaded') return;
      var site_id = '1234567';
      try { Shareaholic.init(site_id); } catch (e) {}
    };
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(shr, s);
  })();
//]]>
</script>

However, the way I tried it I got a syntax/parse -error.
So I guess I could use your help with this as well.

How should I do this properly?

margarett

You have to escape every single quote with a backslash *and* that code should be inside an "echo"

If you use a good editor like Notepad++ you should clearly see if the change goes sour ;)
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Shambles

.. and as I'm a little bored, here's your snippet recoded as per margarett's instructions:

Code (Select Me To Win) Select

<script type="text/javascript">
//<![CDATA[
  (function() {
    var shr = document.createElement(\'script\');
    shr.setAttribute(\'data-cfasync\', \'false\');
    shr.src = \'//dsms0mj1bbhn4.cloudfront.net/assets/pub/shareaholic.js\';
    shr.type = \'text/javascript\'; shr.async = \'true\';
    shr.onload = shr.onreadystatechange = function() {
      var rs = this.readyState;
      if (rs && rs != \'complete\' && rs != \'loaded\') return;
      var site_id = \'1234567\';
      try { Shareaholic.init(site_id); } catch (e) {}
    };
    var s = document.getElementsByTagName(\'script\')[0];
    s.parentNode.insertBefore(shr, s);
  })();
//]]>
</script>

JollyGuru

It works great!!  :D
Again, mindblowing amazing support guys!

Also an extra thanks for the edit Shambles!  :D


JollyGuru

Quote from: margarett on May 12, 2015, 09:30:59 AM
You have to escape every single quote with a backslash *and* that code should be inside an "echo"

1: How do I put something inside an "echo" and what does it do?

2: I just copied the code Shambles had edited for me, but is that code "echoed"?

3: Do I need to escape the quotes and "echo" the code-snippet mentioned in my other thread at this link also?:
http://www.simplemachines.org/community/index.php?topic=536413.msg3816682#msg3816682

margarett

1: That's PHP 1-0-1, so there is no other way to explain it...
echo 'content here between single quotes (that\'s why you have to escape literal quotes)';
http://php.net/echo

2: No, it isn't. But again, you just need echo and an opening and closing single quote.

3: Yes
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Advertisement: