如何从CSV文件导入带有说明的类别?

时间:2012-07-09 作者:Ruriko

如何从逗号分隔的值文件中导入新类别和类别描述的列表?

1 个回复
SO网友:brasofilo

非常简单的插件来实现这一点。

仅在激活时运行,因此,如果有5个CSV文件,插件必须激活/停用五次。

文件:

/wp-content/plugins/create-cats-from-csv/create-cats-from-csv.php

<?php
/*
    Plugin Name: Insert CSV Categories
    Version: 1.0
    Description: Reads a CSV file on plugin activation and insert the Categories/Description/Parent into WordPress as defined in the CSV
    Plugin URI: http://wordpress.stackexchange.com/q/57865/12615
    Author: brasofilo 
    Author URI: http://rodbuaiz.com
*/
register_activation_hook(__FILE__, \'wpse_57865_activation_run\');

function wpse_57865_activation_run()
{
    // THE PREG_SPLIT IS NOT ADEQUATE FOR COMPLEX STRINGS
    // Code snippet: http://stackoverflow.com/q/7502370/1287812
    $file = file_get_contents( plugin_dir_path( __FILE__ )  . \'categories.csv\' );
    $data = array_map( "str_getcsv", preg_split( \'/\\r*\\n+|\\r+/\', $file ) );

    if( count($data) < 1)
        return;

    // http://codex.wordpress.org/Function_Reference/wp_insert_category
    foreach( $data as $cat )
    {
        $cat_defaults = array(
          \'cat_name\' => $cat[0],
          \'category_description\' => $cat[1] 
        );

        wp_insert_category($cat_defaults);
    }

}
文件:

/wp-content/plugins/create-cats-from-csv/categories.csv

Category Title, Category Description
First Category, Lorem ipsum dolor sit amet
Second Category, Consectetur adipisicing elit
Third Category, Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua

结束

相关推荐

NEXT_POST_LINK()中的EXCLUDE_CATEGORIES参数行为异常

我有一个Wordpress模板。php页面。该页面有“下一页”和“上一页”箭头,允许浏览所有帖子。我想将某些类别的帖子排除在“下一个”和“上一个”计算中。我有以下代码: // in single.php next_post_link( \'%link\', \'&larr; Previous\', false, \'11 and 13 and 15\'); 这应该会显示到下一篇文章的链接。第11、13和15类的职位不应按照the $ignore_categories para