删除基于类别的表单链接会扰乱父子行为

时间:2016-03-11 作者:dingo_d

我在wordpress中使用“标准”技巧删除类别。在permalinks中,我将permalinks结构设置为

/%category%/%postname%/
以及我的类别库

.
这很好,直到客户说他希望父类别出现在文章的标题中,子类别显示在主页/索引页上文章的页脚中。。。

我已经设置好了,但问题是我的子类别链接现在是:

http://www.example.com/./parent_category/child_category
当我单击子类别时,我会被发送到最新的文章。

我试着加入我的.htaccess

RewriteRule ^category/(.+)$ http://www.example.com/$1 [R=301,L]
但这没什么作用。

现在,当我删除圆点时,所有这些都起作用了,但我的链接如下所示:

http://www.example.com/category/parent_category/child_category
客户端明确不希望category/ 在链接中。。。

除了安装插件,我还能做什么?如何对此进行排序?我可以要吗

http://www.example.com/parent_category/child_category
作为我的永久链接,如果是,如何实现这一点?

2 个回复
SO网友:CK MacLeod

“.”方法总是一个不太可能的难题。

这个"Remove Category URL" plug-in 工作很好,以我的经验来看,就像广告上说的那样。我不确定出于这个目的而反对安装插件的原因是什么,但是,如果出于某种原因不想这样做,您可以随时检查它,并复制和删减插件开发人员使用的技术,尽管依赖于对内置规则的掌握的代码实际上并不十分复杂。整个主文件只有128行,核心功能位于其中大约100行中,包括四个操作和四个过滤器。

如果您不想将其作为插件安装,我想您可以去掉插件的便利性和安装函数/文件,然后将核心函数添加到主题函数文件中。我怀疑其他人在这里草拟的任何方法都可能只是在重新发明同一个轮子。

/**
 * Plugin Name: Remove Category URL
 * Plugin URI: http://valeriosouza.com.br/portfolio/remove-category-url/
 * Description: This plugin removes \'/category\' from your category permalinks. (e.g. `/category/my-category/` to `/my-category/`)
 * Version: 1.1
 * Author: Valerio Souza, WordLab Academy
 * Author URI: http://valeriosouza.com.br/
 */

/* hooks */
register_activation_hook( __FILE__,   \'remove_category_url_refresh_rules\' );
register_deactivation_hook( __FILE__, \'remove_category_url_deactivate\' );

/* actions */
add_action( \'created_category\', \'remove_category_url_refresh_rules\' );
add_action( \'delete_category\',  \'remove_category_url_refresh_rules\' );
add_action( \'edited_category\',  \'remove_category_url_refresh_rules\' );
add_action( \'init\',             \'remove_category_url_permastruct\' );

/* filters */
add_filter( \'category_rewrite_rules\', \'remove_category_url_rewrite_rules\' );
add_filter( \'query_vars\',             \'remove_category_url_query_vars\' );    // Adds \'category_redirect\' query variable
add_filter( \'request\',                \'remove_category_url_request\' );       // Redirects if \'category_redirect\' is set
add_filter( \'plugin_row_meta\',        \'remove_category_url_plugin_row_meta\', 10, 4 );

function remove_category_url_refresh_rules() {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}

function remove_category_url_deactivate() {
    remove_filter( \'category_rewrite_rules\', \'remove_category_url_rewrite_rules\' ); // We don\'t want to insert our custom rules again
    remove_category_url_refresh_rules();
}

/**
 * Removes category base.
 *
 * @return void
 */
function remove_category_url_permastruct() {
    global $wp_rewrite, $wp_version;

    if ( 3.4 <= $wp_version ) {
        $wp_rewrite->extra_permastructs[\'category\'][\'struct\'] = \'%category%\';
    } else {
        $wp_rewrite->extra_permastructs[\'category\'][0] = \'%category%\';
    }
}

/**
 * Adds our custom category rewrite rules.
 *
 * @param  array $category_rewrite Category rewrite rules.
 *
 * @return array
 */
function remove_category_url_rewrite_rules( $category_rewrite ) {
    global $wp_rewrite;

    $category_rewrite = array();

    /* WPML is present: temporary disable terms_clauses filter to get all categories for rewrite */
    if ( class_exists( \'Sitepress\' ) ) {
        global $sitepress;

        remove_filter( \'terms_clauses\', array( $sitepress, \'terms_clauses\' ) );
        $categories = get_categories( array( \'hide_empty\' => false, \'_icl_show_all_langs\' => true ) );
        add_filter( \'terms_clauses\', array( $sitepress, \'terms_clauses\' ) );
    } else {
        $categories = get_categories( array( \'hide_empty\' => false ) );
    }

    foreach ( $categories as $category ) {
        $category_nicename = $category->slug;
        if (  $category->parent == $category->cat_ID ) {
            $category->parent = 0;
        } elseif ( 0 != $category->parent ) {
            $category_nicename = get_category_parents(  $category->parent, false, \'/\', true  ) . $category_nicename;
        }
        $category_rewrite[ \'(\' . $category_nicename . \')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$\' ] = \'index.php?category_name=$matches[1]&feed=$matches[2]\';
        $category_rewrite[ \'(\' . $category_nicename . \')/page/?([0-9]{1,})/?$\' ] = \'index.php?category_name=$matches[1]&paged=$matches[2]\';
        $category_rewrite[ \'(\' . $category_nicename . \')/?$\' ] = \'index.php?category_name=$matches[1]\';
    }

    // Redirect support from Old Category Base
    $old_category_base = get_option( \'category_base\' ) ? get_option( \'category_base\' ) : \'category\';
    $old_category_base = trim( $old_category_base, \'/\' );
    $category_rewrite[ $old_category_base . \'/(.*)$\' ] = \'index.php?category_redirect=$matches[1]\';

    return $category_rewrite;
}

function remove_category_url_query_vars( $public_query_vars ) {
    $public_query_vars[] = \'category_redirect\';

    return $public_query_vars;
}

/**
 * Handles category redirects.
 *
 * @param $query_vars Current query vars.
 *
 * @return array $query_vars, or void if category_redirect is present.
 */
function remove_category_url_request( $query_vars ) {
    if ( isset( $query_vars[\'category_redirect\'] ) ) {
        $catlink = trailingslashit( get_option( \'home\' ) ) . user_trailingslashit( $query_vars[\'category_redirect\'], \'category\' );
        status_header( 301 );
        header( "Location: $catlink" );
        exit;
    }

    return $query_vars;
}

function remove_category_url_plugin_row_meta( $links, $file ) {
        if( plugin_basename( __FILE__ ) === $file ) {
            $links[] = sprintf(
                \'<a target="_blank" href="%s">%s</a>\',
                esc_url(\'http://wordlab.com.br/donate/\'),
                __( \'Donate\', \'remove_category_url\' )
            );
        }
        return $links;
    }

SO网友:YooWoo

将此代码放在函数中。php和您的好去处(即使使用WordPress 4.7.2)

/* hooks */
register_activation_hook(__FILE__,    \'yw_no_category_slug_refresh_rules\');
register_deactivation_hook(__FILE__,  \'yw_no_category_slug_deactivate\');

/* actions */
add_action(\'created_category\',  \'yw_no_category_slug_refresh_rules\');
add_action(\'delete_category\',   \'yw_no_category_slug_refresh_rules\');
add_action(\'edited_category\',   \'yw_no_category_slug_refresh_rules\');
add_action(\'init\',              \'yw_no_category_slug_permastruct\');

/* filters */
add_filter(\'category_rewrite_rules\', \'yw_no_category_slug_rewrite_rules\');
add_filter(\'query_vars\',             \'yw_no_category_slug_query_vars\');    // Adds \'category_redirect\' query variable
add_filter(\'request\',                \'yw_no_category_slug_request\');       // Redirects if \'category_redirect\' is set

function yw_no_category_slug_refresh_rules() {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}

function yw_no_category_slug_deactivate() {
    remove_filter( \'category_rewrite_rules\', \'yw_no_category_slug_rewrite_rules\' ); // We don\'t want to insert our custom rules again
    yw_no_category_slug_refresh_rules();
}

/**
 * Removes category base.
 *
 * @return void
 */
function yw_no_category_slug_permastruct()
{
    global $wp_rewrite;
    global $wp_version;

    if ( $wp_version >= 3.4 ) {
        $wp_rewrite->extra_permastructs[\'category\'][\'struct\'] = \'%category%\';
    } else {
        $wp_rewrite->extra_permastructs[\'category\'][0] = \'%category%\';
    }
}

/**
 * Adds our custom category rewrite rules.
 *
 * @param  array $category_rewrite Category rewrite rules.
 *
 * @return array
 */
function yw_no_category_slug_rewrite_rules($category_rewrite) {
    global $wp_rewrite;
    $category_rewrite=array();

    /* WPML is present: temporary disable terms_clauses filter to get all categories for rewrite */
    if ( class_exists( \'Sitepress\' ) ) {
        global $sitepress;

        remove_filter( \'terms_clauses\', array( $sitepress, \'terms_clauses\' ) );
        $categories = get_categories( array( \'hide_empty\' => false ) );
        //Fix provided by Albin here https://wordpress.org/support/topic/bug-with-wpml-2/#post-8362218
        //add_filter( \'terms_clauses\', array( $sitepress, \'terms_clauses\' ) );
        add_filter( \'terms_clauses\', array( $sitepress, \'terms_clauses\' ), 10, 4 );
    } else {
        $categories = get_categories( array( \'hide_empty\' => false ) );
    }

    foreach( $categories as $category ) {
        $category_nicename = $category->slug;

        if ( $category->parent == $category->cat_ID ) {
            $category->parent = 0;
        } elseif ( $category->parent != 0 ) {
            $category_nicename = get_category_parents( $category->parent, false, \'/\', true ) . $category_nicename;
        }

        $category_rewrite[\'(\'.$category_nicename.\')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$\'] = \'index.php?category_name=$matches[1]&feed=$matches[2]\';
        $category_rewrite["({$category_nicename})/{$wp_rewrite->pagination_base}/?([0-9]{1,})/?$"] = \'index.php?category_name=$matches[1]&paged=$matches[2]\';
        $category_rewrite[\'(\'.$category_nicename.\')/?$\'] = \'index.php?category_name=$matches[1]\';
    }

    // Redirect support from Old Category Base
    $old_category_base = get_option( \'category_base\' ) ? get_option( \'category_base\' ) : \'category\';
    $old_category_base = trim( $old_category_base, \'/\' );
    $category_rewrite[$old_category_base.\'/(.*)$\'] = \'index.php?category_redirect=$matches[1]\';

    return $category_rewrite;
}

function yw_no_category_slug_query_vars($public_query_vars) {
    $public_query_vars[] = \'category_redirect\';
    return $public_query_vars;
}

/**
 * Handles category redirects.
 *
 * @param $query_vars Current query vars.
 *
 * @return array $query_vars, or void if category_redirect is present.
 */
function yw_no_category_slug_request($query_vars) {
    if( isset( $query_vars[\'category_redirect\'] ) ) {
        $catlink = trailingslashit( get_option( \'home\' ) ) . user_trailingslashit( $query_vars[\'category_redirect\'], \'category\' );
        status_header( 301 );
        header( "Location: $catlink" );
        exit();
    }

    return $query_vars;
}

相关推荐

在WP搜索表单中是否有类似`wp_Dropdown_Categories()`的输入复选框选项?

我有自定义的帖子类型,当前正在使用wp_dropdown_categories() 在WP搜索表单中的功能,以便人们能够按薪资级别和工作类型进行筛选。是否有一个等效函数,以便用户可以使用输入复选框元素而不是<select> 元素,以便他们在进行搜索时可以选择多个工作类型或薪资水平?我似乎在开发者文档中找不到任何东西。我正在寻找的是允许复选框替换使用下面代码中的select选项的下拉元素的东西。<form method="get" action="<?p