【问题标题】:Add theme support for post thumbnail isn't working from theme class为帖子缩略图添加主题支持在主题类中不起作用
【发布时间】:2023-04-03 13:56:02
【问题描述】:

我有一个主题类,我在其中引导我的主题并添加主题支持,但特色图像元框没有出现。

类文件内部:

class WCS_THEME {

    private function __construct() {
        $this->setup_hooks();
    }

    // Singleton design code here

    protected function setup_hooks() {
        add_action( 'after_theme_setup', [ $this, 'setup_theme' ] );
    }

    public function setup_theme() {
        add_theme_support( 'post-thumbnails' );
    }
}
\WCS_THEME\Inc\WCS_THEME::get_instance();

functions.php 内部:

require WCS_DIR_PATH . '/inc/classes/class-wcs-theme.php';

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    首先,简单替换

    \WCS_THEME\Inc\WCS_THEME::get_instance();
    

    $instance=new \WCS_THEME\Inc\WCS_THEME();
    

    另外, 上面的代码没有出现致命错误,这有点奇怪。 将调试设置为 TRUE 以查看 PHP 警告。

    我认为 WCS_DIR_PATH 存在一些问题。 使用

    打印完整路径
    echo WCS_DIR_PATH . '/inc/classes/class-wcs-theme.php';
    

    以确保它是您文件的正确路径。

    【讨论】:

    • 我替换了上面的行,但我得到一个错误,因为该类有一个私有构造函数。奇怪的是,这门课给了我发布,因为我的风格被排在一个在主题中工作的类中。 :(
    • 是的,如果不需要,您可以从那里删除私有。或者您可以使用此答案中给出的 get_instanse 方法:stackoverflow.com/a/12553217/701666
    • 特色图片部分仍未显示 :(
    • 我看不到整个结构,但如果你不能解决它,那么为什么不使用 add_theme_support( 'post-thumbnails' );在你的functions.php中?只需将那一行粘贴到那里即可。
    • 我把它放在 functions.php 中:function wcs_setup() { add_theme_support( 'post-thumbnails' ); } add_action( 'after_theme_setup', 'wcs_setup' ); 但它仍然没有出现。有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多