Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Massl on February 27, 2019, 05:48:40 AM

Title: How insert code on last post
Post by: Massl on February 27, 2019, 05:48:40 AM
Hi, I'm trying to insert some code on every last message of the topic's pages.

There is a way or $context that I can use?
With $context['topic_last_message'] it is only displayed in the last message of the topic

Thanks
Title: Re: How insert code on last post
Post by: vii on February 27, 2019, 08:20:28 AM
Can't you put it directly after the message loop in Display.template.php? For an unmodded file, it should be on line 632. (Or just search for <a id="lastPost"></a>)

Otherwise, I suppose at the very top of the message loop in the template file, you could do:

Find:

    // Get all the messages...
    while ($message = $context['get_message']())
    {
        $ignoring = false;
        $alternate = !$alternate;


Replace with:
    global $messages_request, $smcFunc;

    $numMsgs = $smcFunc['db_num_rows']($messages_request);
    $curMsg = 0;

    // Get all the messages...
    while ($message = $context['get_message']())
    {
        $ignoring = false;
        $alternate = !$alternate;
        if ( (++$curMsg) == $numMsgs ) {
            $message['body'] .= '<br />last msg (num: ' . $numMsgs . ')';
        }



But FYI that is just a quickie example and not standard for SMF mods (counting rows should go in Display.php)
Title: Re: How insert code on last post
Post by: Massl on February 27, 2019, 08:56:27 AM
Quote from: Virginiaz on February 27, 2019, 08:20:28 AM
Can't you put it directly after the message loop in Display.template.php? For an unmodded file, it should be on line 632. (Or just search for <a id="lastPost"></a>)

Thanks for your reply, I forgot to write that I'm using 2.1 RC1 and I can't find lastPost on Display.template.php

After
<div class="inner" data-msgid="', $message['id'], '" id="msg_', $message['id'], '"', $ignoring ? ' style="display:none;"' : '', '>
', $message['body'], '
</div>
</div><!-- .post -->';


I add

if ($message['id'] == $context['topic_last_message'] and $message['id'] != $context['first_message'])
echo 'Code';


but as I said it is only displayed in the last message of the topic.
Title: Re: How insert code on last post
Post by: vii on February 27, 2019, 09:11:04 AM
If you want your code to come directly after all the messages for 2.1, put it in the </form></div> part of this:

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

    echo '
                </form>
            </div>';

    // Mobile action - moderation buttons (bottom)


As far as I know, the primary example I gave you with db_num_rows would still work but you need to tweak the code because 2.1 offloads template message display into another subroutine instead of doing it all in the loop.
Title: Re: How insert code on last post
Post by: SychO on February 27, 2019, 10:40:18 AM
do you mean the last post of every page or literally the last post of the whole topic ?
Title: Re: How insert code on last post
Post by: Massl on February 27, 2019, 11:20:11 AM
I mean last post of every page.

@Virginiaz thanks
Title: Re: How insert code on last post
Post by: Massl on February 28, 2019, 03:15:05 AM
I tried everything but nothing, I didn't imagine it was so difficult...  ???
Title: Re: How insert code on last post
Post by: SychO on February 28, 2019, 07:40:15 AM
Use this anywhere inside template_single_post function
if($context['topic_last_message'] == $message['id'] || ($message['counter']+1) % $context['messages_per_page'] == 0)
echo '<div class="errorbox">I\'m the last message of this page</div>';
Title: Re: How insert code on last post
Post by: vbgamer45 on February 28, 2019, 09:34:21 AM
Thanks very helpful.
Title: Re: How insert code on last post
Post by: Massl on February 28, 2019, 11:02:32 AM
Thanks SychO, without your help I would never have succeeded  ;)