Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

是否可以优化随机图片的显示 #437

Open
cxocto opened this issue Aug 21, 2024 · 1 comment
Open

是否可以优化随机图片的显示 #437

cxocto opened this issue Aug 21, 2024 · 1 comment
Labels
功能 新功能或需求

Comments

@cxocto
Copy link

cxocto commented Aug 21, 2024

  • 是否为已有功能:是
  • 建议阐述:
  1. 是否可以自动判断random文件夹里的图片数量或文件名,然后随机显示
  2. 能否优化随机数,我试过自己手动增加到50张图片,mt_rand(1, 8)改到mt_rand(1, 50),但文章列表中仍是容易出现连续相同图片,不太美观.建议可以判断一下已经出现过的图片编号,然后在后续显示时判断与剔除,例如建立一个1/2图片数量的数组,随机数生成时判断与数组里不一样的才可以放进数组,并进行先进先出的操作,这样就可以有效避免相近的文章会出现相同的图片了
@cxocto cxocto added the 功能 新功能或需求 label Aug 21, 2024
@cxocto
Copy link
Author

cxocto commented Aug 23, 2024

function get_post_images($_post = null)
{
    global $post;
    global $ram;
    global $pr_img;
    if (empty($ram) || $pr_img==55)
    {
    $ram = range (1,55);
    shuffle ($ram);
    $pr_img=0;
    }
    
    if ($_post != null) {
        $post = $_post;
    }
    $post_id = $post->ID;
    // 如果有封面图取封面图
    if (has_post_thumbnail()) {
        $res = get_the_post_thumbnail_url($post, 'large');
        if ($res != null) {
            return $res;
        }
    }
    if ($post_id == null && $post) {
        $content = $post->post_content;
    } else {
        $content = get_post($post_id)->post_content;
    }
    preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
    if ($matches && $matches[1]) {
        $res = $matches[1][0];
    } else {
        $new_post=$ram[$pr_img];
        $pr_img=$pr_img+1;
        $res = get_stylesheet_directory_uri() . '/assets/img/random/' . $new_post . '.jpg';
    }
    return $res;
}

这是我自己修改的随机图不重复的方法

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
功能 新功能或需求
Projects
None yet
Development

No branches or pull requests

1 participant