Help

Help > Forum > 主题和帖子 > 在每个帖子旁边显示帖子编号

在每个帖子旁边显示帖子编号

您可以使用以下步骤在主题中的每篇文章旁边显示帖子编号。

当你想在回复中引用帖子时,通常最好使用帖子中的回复链接。这会自动包含指向原始帖子的链接,因此读者可以轻松跳转到该帖子。这种方法比提及帖子编号更可靠,因为如果以后删除任何帖子,帖子编号可能会发生变化。您还可以使用每篇文章中的共享图标来复制指向该帖子的直接链接。

  1. 登录您的Website Toolbox账户。
  2. 点击 整合 主菜单中的链接。
  3. 点击 HTML 链接。
  4. 将下面的 HTML 代码复制到 论坛头标记 HTML 代码 文本框:

    <!-- Add post numbers next to each post of a topic. ie: #1, #2, #3 -->
    <script>
    window.addEventListener("DOMContentLoaded", function () {
    	const container = document.querySelector('#posts-list');
    	if (!container || !window.location.href.match(/\/post\//)) return;
    	const observer = new MutationObserver(function (mutations) {
    		let shouldRenumber = false;
    		for (const m of mutations) {            
    			for (const node of m.addedNodes) {
    				if (node.nodeType !== 1) continue;
    				if (node.matches && node.matches('.post-body.pull-left')) shouldRenumber = true;
    				else if (node.querySelector && node.querySelector('.post-body.pull-left')) shouldRenumber = true;                
    				if (node.id && node.id.startsWith('post_row_')) {
    					const el = document.getElementById(node.id);
    					if (el && window.getComputedStyle(el).display === 'none') shouldRenumber = true;
    				}
    			}           
    			if (m.type === 'attributes' && m.target.id && m.target.id.startsWith('post_row_')) {
    				const el = m.target;
    				if (el && window.getComputedStyle(el).display === 'none') shouldRenumber = true;
    			}
    		}
    		if (shouldRenumber) renumberPosts();
    	});
        function renumberPosts() {
    		observer.disconnect();
    		let start = 1;
    		const pageText = jQ('.page-numbers').text() || '';
    		var paginationNum = pageText.replace(/,/g, ""); 
    		const matches = paginationNum.match(/[0-9]+/);        
    		if (matches) start = parseInt(matches[0], 10);
    		let postNumber = start;
    		if(start > 1) {
    			postNumber = start - 1;
    		}
    		jQ('#posts-list .post-body .post-options').each(function () {
    			const optionsElement = jQ(this);
    			if (optionsElement.closest('.post-body').is(':visible')) {
    				optionsElement.find('.post-number').remove();
    				if(optionsElement.closest('.first-post').length) {
    					optionsElement.append('#1');
    				} else {
    					optionsElement.append('#' + postNumber + '');                
    				}
    				postNumber++;
    			}
    		});
    		observer.observe(container, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });
        }    
        renumberPosts();
        observer.observe(container, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });
    });
    </script>
    
  5. 保存 这些变化。


If you still need help, please contact us.