浅生 发表于 2024-9-9 16:45:08

关于前端钩子 按钮显示的问题 大佬们帮我看看 哪个地方有有问题 New

关于前端钩子 按钮显示的问题 大佬们帮我看看 哪个地方有问题

最终想显示的是
发帖框顶部 发帖框中部 发帖框底部 还有 帖子和列表页的快速回复区域显示

xml文件里面的代码
<item id="hooks">    <item id="post_top">      <item id="qier_thread_top"><!]></item>    </item>    <item id="post_middle">      <item id="qier_thread_middle"><!]></item>    </item>    <item id="post_bottom">      <item id="qier_thread_button"><!]></item>    </item>    <item id="viewthread_fastpost_content">      <item id="qier_thread_viewthread_fastpost_content"><!]></item>    </item>    <item id="forumdisplay_postbutton_bottom">      <item id="qier_thread_forumdisplay_postbutton_bottom"><!]></item>    </item></item>class.php里面的代码
<?phpif(!defined('IN_DISCUZ')) {    exit('Access Denied');}class plugin_qier_thread {    public function post_top() {      if (!$this->is_plugin_enabled()) return '';      return $this->qier_thread_button('post_top');    }    public function post_middle() {      if (!$this->is_plugin_enabled()) return '';      return $this->qier_thread_button('post_middle');    }    public function post_bottom() {      if (!$this->is_plugin_enabled()) return '';      return $this->qier_thread_button('post_bottom');    }    public function viewthread_fastpost_content() {      if (!$this->is_plugin_enabled()) return '';      global $_G;      if($_G['cache']['plugin']['qier_thread']['show_in_fastpost']) {            return $this->_get_button_html();      }      return '';    }    public function global_header() {      if (!$this->is_plugin_enabled()) return;      global $_G;      $button_position = $this->get_button_position();      $button_text = $_G['cache']['plugin']['qier_thread']['custom_button_text'] ?: lang('plugin/qier_thread', 'qier_thread_generate_article');                // 只在发帖页面显示按钮      if(CURSCRIPT == 'forum' && (CURMODULE == 'post' || CURMODULE == 'viewthread')) {            include template('qier_thread:qier_thread_post');      }    }    public function forumdisplay_postbutton_bottom() {      if (!$this->is_plugin_enabled()) return '';      global $_G;      if($_G['cache']['plugin']['qier_thread']['show_in_fastpost']) {            return $this->_get_button_html();      }      return '';    }    private function qier_thread_button($position) {      global $_G;                $enable = $_G['cache']['plugin']['qier_thread']['enable'];      $button_position = $_G['cache']['plugin']['qier_thread']['button_position'];                if (!$enable) {            return '';      }                $position_map = [            'post_top' => '1',            'post_middle' => '2',            'post_bottom' => '3'      ];                if ($button_position !== $position_map[$position]) {            return '';      }                return $this->get_button_html();    }    private function _get_button_html() {      global $_G;      $button_text = $_G['cache']['plugin']['qier_thread']['custom_button_text'] ?: '这是个按钮';      include template('qier_thread:button');      return $return;    }    private function get_button_html() {      $css_url = 'source/plugin/qier_thread/static/css/style.css';      $js_url = 'source/plugin/qier_thread/static/js/qier_thread.js';                $output = '<link rel="stylesheet" type="text/css" href="' . $css_url . '" />';      $output .= '<script type="text/javascript" src="' . $js_url . '"></script>';                $button = '<button type="button" id="qier_thread_button" class="qier-thread-button">';      $button .= '<span class="qier-thread-icon"></span>';      $button .= '<span class="qier-thread-text">' . $button_text . '</span>';      $button .= '</button>';                return $output . $button;    }    private function get_position_for_hook($hook) {      switch ($hook) {            case 'post_top': return 1;            case 'post_middle': return 2;            case 'post_bottom': return 3;            default: return 0;      }    }    public function get_button_position() {      global $_G;      return $_G['cache']['plugin']['qier_thread']['button_position'];    }    private function is_plugin_enabled() {      global $_G;      return !empty($_G['cache']['plugin']['qier_thread']['enable']);    }}

试过好多方案就是不能正常的显示出按钮 查看网页源代码 也没用相关插件的js或者css文件路径

也就是说没启动插件

不知道什么原因【上面的代码是改了好几种方案的其中一种了 都不显示】

日志全开日志页抓不到。

我知道答案 回答被采纳将会获得1 贡献 已有0人回答

Discuz智能体 发表于 2025-3-20 07:27:50

你好!关于你在Discuz前端钩子中按钮显示的问题,我可以帮你分析一下。你提到的问题涉及到在前端的发帖框顶部、中部、底部以及快速回复区域显示按钮。以下是一些常见的排查步骤和建议:

### 1. **检查XML文件中的钩子定义**
   首先,确保你在XML文件中正确定义了钩子。钩子的定义应该类似于以下代码:

   <hook>
       <hookname>forumdisplay_postbutton_top</hookname>
       <include>source/plugin/yourplugin/yourplugin.class.php</include>
       <class>yourplugin</class>
       <method>hook_forumdisplay_postbutton_top</method>
   </hook>

   你需要确保钩子的名称(`hookname`)是正确的,并且与Discuz的钩子系统匹配。常见的钩子名称包括:
   - `forumdisplay_postbutton_top`:发帖框顶部
   - `forumdisplay_postbutton_middle`:发帖框中部
   - `forumdisplay_postbutton_bottom`:发帖框底部
   - `forumdisplay_fastpost_extra`:快速回复区域

### 2. **检查class.php文件中的钩子方法**
   在`class.php`文件中,你需要确保钩子方法正确定义,并且返回的内容是你想要显示的按钮。例如:

   class yourplugin {
       public function hook_forumdisplay_postbutton_top() {
         return '<button>我的按钮</button>';
       }

       public function hook_forumdisplay_postbutton_middle() {
         return '<button>我的按钮</button>';
       }

       public function hook_forumdisplay_postbutton_bottom() {
         return '<button>我的按钮</button>';
       }

       public function hook_forumdisplay_fastpost_extra() {
         return '<button>我的按钮</button>';
       }
   }

   确保每个钩子方法都返回了正确的HTML内容。

### 3. **检查插件是否启用**
   确保你的插件已经在Discuz后台启用。如果插件没有启用,钩子将不会生效。

### 4. **检查缓存**
   有时候Discuz会缓存模板和插件数据。如果你修改了插件代码或XML文件,但没有看到变化,尝试清除Discuz的缓存。你可以在Discuz后台的“工具” -> “更新缓存”中进行操作。

### 5. **检查模板文件**
   如果你在钩子中返回的内容没有显示,可能是因为模板文件没有正确调用钩子。你可以检查相关的模板文件(如`template/default/forum/forumdisplay.htm`),确保钩子被正确调用。

   例如,在`forumdisplay.htm`中,你应该看到类似以下的代码:

   <!--{hook/forumdisplay_postbutton_top}-->
   <!--{hook/forumdisplay_postbutton_middle}-->
   <!--{hook/forumdisplay_postbutton_bottom}-->
   <!--{hook/forumdisplay_fastpost_extra}-->

   这些代码会调用你在插件中定义的钩子方法。

### 6. **调试输出**
   如果以上步骤都没有问题,但你仍然看不到按钮,可以尝试在钩子方法中添加调试输出,例如:

   public function hook_forumdisplay_postbutton_top() {
       echo '钩子被调用了';
       return '<button>我的按钮</button>';
   }

   这样你可以确认钩子是否被正确调用。

### 7. **检查权限**
   确保你的按钮显示逻辑没有受到用户权限的限制。例如,某些按钮可能只对特定用户组显示。

### 8. **检查CSS和JS**
   如果你的按钮显示出来了,但没有正确的样式或行为,检查是否有CSS或JS冲突。确保你的按钮样式和脚本正确加载。

### 总结
通过以上步骤,你应该能够找到按钮不显示的原因。如果问题仍然存在,建议你提供更多的代码细节,我可以进一步帮你分析。

如果你有其他问题,欢迎继续提问!
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。
页: [1]
查看完整版本: 关于前端钩子 按钮显示的问题 大佬们帮我看看 哪个地方有有问题 New