个性化阅读
专注于IT技术分析

WordPress的页面slug与媒体库项冲突

我创建了一个分配了自定义模板的新页面。当我访问该页面的URL时, 我看到的似乎是默认页面布局(不是我的模板), 并且管理工具栏显示了与媒体有关的选项(例如:编辑媒体)。

经过一番挠头之后, 我推断出该url必须以某种方式指向媒体项目。我编辑了页面标签, 然后”宾果式”实际页面按预期显示。当我访问原始URL(从第一个子段开始)时, 我看到了相同的媒体项目。

底线:似乎页面和媒体项恰好具有相同的名称, 这以某种方式导致WP的连线交叉。

我的问题:有人可以帮助我了解这种情况如何/为什么发生吗? WordPress是否会创建到媒体库中所有内容的魔术永久链接(wp-content / uploads / …中的位置除外)?

注意:媒体项目通常已上传到媒体库(不是FTP到根目录等)


#1


是的, 在WordPress中, 你不能有重复的子弹/类别/分类法/标签。因此, 如果你的主题允许媒体文件和永久链接拥有自己的页面, 并且该段与另一个页面相同, 则该段通常会附加一个数字, 因为数据库不喜欢它。

媒体”示例”

页面slug” example”将不起作用, 因为该slug已经存在, 如果在管理员中完成, 它将自动将slug更改为” example-1″。


#2


我只是有这个问题, 并像这样解决它:

$post_s=get_posts('posts_per_page=-1');
        foreach($post_s as $p){
            $atts = get_posts('post_type=attachment&name='.$p->post_name.'&posts_per_page=-1&post_status=inherit');
            foreach($atts as $att){
                echo 'found!! '.$p->post_name;
                // Update post 37
                $my_post = array(
                      'ID'           => $atts->ID, 'post_name' => $att->post_name.'-image'
                );
                // Update the post into the database
                wp_update_post( $my_post );
            }
        }

#3


这是一个较晚的答案, 但我想给出alesub给出的答案的简洁版本。

function wp21418_append_to_post_name() {

    // Checks to see if the option images_updated has been set or has a value of true.
    if ( get_option( 'images_updated' ) === 'true' ) :
        return;
    endif;

    // get all attachment posts.
    $attachments = get_posts([
        'post_type' => 'attachment', 'post_status' => 'inherit', 'name' => $p->slug, 'posts_per_page' => -1, ]);

    // For each attachment, loop and update the post_name
    foreach($attachments as $p){

        $attachment = array(
            'ID'           => $p->ID, 'post_name' => $p->post_name.'-image'
        );
        // Update the post into the database
        wp_update_post( $attachment );

    }

    // Once everything is looped, add the option to the database.
    add_option( 'images_updated', 'true' );
}

add_action( 'after_setup_theme', 'wp21418_append_to_post_name' );

设置主题之后, 此功能将在动作挂钩上运行。第一行检查以查看数据库images_updated中是否有一个选项。如果存在该选项, 则我们对该函数进行保释, 并且不进行任何处理。否则, 如果该选项不存在, 它将运行该函数并在最后设置该选项。

这样就可以只运行一次。刷新后不必删除该功能。如果要再次运行它, 只需删除顶部的if语句。需要注意的是:这样做将在post_names的末尾添加另一个-image, 即使它们已经具有-image了(例如-image-image)

对于这种情况, 可能会有更多的文件名检查。如果有人真的需要, 将使用该信息来更新答案。

赞(0)
未经允许不得转载:srcmini » WordPress的页面slug与媒体库项冲突

评论 抢沙发

评论前必须登录!