在WordPress中,根据文章分类创建文章管理菜单

3

我正在尝试创建一个新的管理员菜单,其中包含所有文章功能。因此,我正在尝试创建一个新菜单,其中包含一个名为“新闻”的文章类别和文章类型-文章。 因此,我添加了下面的函数,但它仍然会更改原始文章菜单的命名和功能。

function custom_post_type() 
{
    $labels = array(
        'name'                => _x( 'News', 'Post Type General Name', 'texdomain' ),
        'singular_name'       => _x( 'News', 'Post Type Singular Name', 'texdomain' ),
        'menu_name'           => __( 'News', 'texdomain' ),
        'parent_item_colon'   => __( 'Parent News', 'texdomain' ),
        'all_items'           => __( 'All News', 'texdomain' ),
        'view_item'           => __( 'View News', 'texdomain' ),
        'add_new_item'        => __( 'Add New News', 'texdomain' ),
        'add_new'             => __( 'Add New', 'texdomain' ),
        'edit_item'           => __( 'Edit News', 'texdomain' ),
        'update_item'         => __( 'Update News', 'texdomain' ),
        'search_items'        => __( 'Search News', 'texdomain' ),
        'not_found'           => __( 'Not Found', 'texdomain' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'texdomain' ),
    );

    // Set other options for Custom Post Type

    $args = array
    (
        'label'               => __( 'News', 'texdomain' ),
        'description'         => __( 'News news and reviews', 'texdomain' ),
        'labels'              => $labels,

        // Features this CPT supports in Post Editor

        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', 'category'),

        // You can associate this CPT with a taxonomy or custom taxonomy. 

        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */  
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => false,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );

    // Registering your Custom Post Type
    register_post_type( 'Post', $args );

}

/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/

add_action( 'init', 'custom_post_type', 0 );
function custom_post_news_search( $query ) {
    global $wp_query;

    if( is_admin() && $query->is_main_query() && isset( $_GET['post_type'] ) ) {

        //   $strSearchUrl = esc_attr($_GET['post_type']);
        $wp_query = new WP_Query(
        array(
            'post_type' => 'post',
            'category_name'    => 'News',
            )
        );
    }
} 

抱歉,您的问题不够清晰。为什么要覆盖全局 $wp_query 对象?您是想添加一个管理菜单,当点击时,仅加载“新闻”类别下的文章吗? - Sally CJ
@Sally CJ 我需要创建一个管理员菜单,具有与文章菜单相同的功能,仅适用于特定的文章类别,并且具有文章类型和文章。 - Saurabh Gujarani
1
post是一个内置/内部的文章类型,因此您应该使用不同的slug,例如new_post,如当前答案中建议的那样。我相信该答案已经为您提供了所有必要的信息,以便您至少可以按照正确的方式进行操作。但是,您还可以创建一个指向wp-admin/edit.php?news=1的管理菜单,并在该页面上通过pre_get_posts过滤WP_Query,以便帖子列表表格仅显示来自“新闻”类别的帖子,这意味着管理菜单将充当使用该页面上的“类别”下拉菜单过滤帖子的快捷方式...干杯。 - Sally CJ
将来如果您在 SO 上提问,请按照我现在调整的方式格式化代码。 - T.Todua
2个回答

4
“Post”已经被使用了,所以您不能再用这个名称注册其他的文章类型。因此,您还有三个选项。
选项1:
创建一个自定义文章类型,命名为“新文章”,或者作为slug命名为“new_posts”,并将其分类为“新闻”。
更多信息:
如何注册自定义文章类型(Codex Site
如何注册自定义文章类型(Developer Site
如何注册自定义分类法(Codex Site
如何注册自定义分类法(Developer Site
选项2:
如果您坚持使用文章类型作为“post”,您可以创建一个名为“新闻”的新分类法,并将其附加到默认文章中。

更多信息:

选项3

这个选项比其他的略微复杂一些。

  • 为文章创建一个名为“新闻”的新分类。
  • 创建一个管理员页面。
  • 使用WP_List_Table获取与默认文章和自定义文章类型相同的视图。
  • 查询具有您的“新闻”类别的文章并在此处列出它们。
  • (可选) 为目标文章创建编辑页面,以防您不想使用默认页面。

更多信息:

希望这对你有用。编程愉快。


4

在“文章”下创建一个新的子菜单,以列出(或筛选)其下的某个类别非常容易。只需将以下代码放置在您的主题的“functions.php”中即可。

add_action('admin_menu', 'my_admin_menu'); 
function my_admin_menu() { 
    add_submenu_page('edit.php', 'News', 'News', 'manage_options', 'edit.php?category_name=news'); 
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接