修改php函数get_custom_logo以返回随机图像

时间:2017-03-27 19:40:14

标签: php html wordpress

我对编码非常缺乏经验,所以对于任何与我有关的人都要提前感谢你们!

以下是我要做的事情:我想制作我的网站 - 目前正在显示我选择的徽标 - 显示随机我上传的其中一张图片每次页面重新加载时都会生成徽标。

现在,如果主题是用标题编码的话,这将相对容易 - 但对于徽标,我找不到这样做的插件,也无法设法从主题内添加此功能。

这就是我网站的wordpress主题需要徽标的方式:

<?php 
        if( $show_logo && has_custom_logo() ) {
        the_custom_logo();

这是 the_custom_logo()函数代码:

function the_custom_logo( $blog_id = 0 ) {
         echo get_custom_logo( $blog_id );
}

现在我想出一个解决方案如下:我改变了wordpress通过 get_custom_logo()返回图像的方式(我准备在每次有WP更新的时候对其进行编码)。< / p>

这是功能代码:

function get_custom_logo( $blog_id = 0 ) {
$html = '';
$switched_blog = false;

if ( is_multisite() && ! empty( $blog_id ) && (int) $blog_id !== get_current_blog_id() ) {
    switch_to_blog( $blog_id );
    $switched_blog = true;
}

$custom_logo_id = get_theme_mod( 'custom_logo' );

// We have a logo. Logo is go.
if ( $custom_logo_id ) {
    $html = sprintf( '<a href="%1$s" class="custom-logo-link" rel="home" itemprop="url">%2$s</a>',
        esc_url( home_url( '/' ) ),

        wp_get_attachment_image( $custom_logo_id, 'full', false, array(
            'class'    => 'custom-logo',
            'itemprop' => 'logo',
        ) )
    );
}

// If no logo is set but we're in the Customizer, leave a placeholder (needed for the live preview).
elseif ( is_customize_preview() ) {
    $html = sprintf( '<a href="%1$s" class="custom-logo-link" style="display:none;"><img class="custom-logo"/></a>',
        esc_url( home_url( '/' ) )
    );
}

if ( $switched_blog ) {
    restore_current_blog();
}

return apply_filters( 'get_custom_logo', $html, $blog_id );
}

我认为如果我更改$custom_logo_id = get_theme_mod( 'custom_logo' );从我的徽标文件夹中获取随机图像以确定它会起作用,我是错误的吗?如果我没有错,任何人都可以帮助我编写这段代码吗?

如果这是一个愚蠢的问题,我道歉。祝你有愉快的一周!

0 个答案:

没有答案