如何在Wordpress上删除图像附件生成的页面?

时间:2019-01-20 07:40:22

标签: database wordpress phpmyadmin

图像附件页面显示在网站的搜索结果中。我需要知道如何删除当前存在的那些。如果只能在数据库中执行此操作,那么如何确保删除实际的附件页面而不是原始页面本身。

here is where the problem is

2 个答案:

答案 0 :(得分:0)

您可以使用此小片段-将其添加到您的子主题的functions.php中:

function remove_attachments_from_search() {
    global $wp_post_types;
    $wp_post_types['attachment']->exclude_from_search = true;
}
add_action('init', 'remove_attachments_from_search');

这将从搜索结果中排除附件帖子类型。

希望这会有所帮助

答案 1 :(得分:0)

如果已安装,则可以在Yoast SEO上禁用媒体附件页面。或者您可以将其粘贴到您的functions.php

function myprefix_redirect_attachment_page() {
if ( is_attachment() ) {
    global $post;
    if ( $post && $post->post_parent ) {
        wp_redirect( esc_url( get_permalink( $post->post_parent ) ), 301 );
        exit;
    } else {
        wp_redirect( esc_url( home_url( '/' ) ), 301 );
        exit;
    }
  }
}
add_action( 'template_redirect', 'myprefix_redirect_attachment_page' );
相关问题