WordPress自定义帖子类型和分类

时间:2018-11-23 作者:Gore

我需要创建一个简单的房地产列表分类法,为我将在网站上列出的房地产创建自定义的帖子类型。

我不需要你在一些房地产网站上看到的任何花哨的东西,我想学习如何做到这一点,以便在将来我掌握更多的编码技能时加以改进。

我正在附上一张关于属性单页和归档布局需要的外观的图片property display我想知道我从哪里开始学习来实现这一目标。

提前谢谢你。

1 个回复
SO网友:Bob

您需要执行以下操作:

--首先,如果尚未创建子主题,请创建子主题。如果你的主题需要更新,这将避免你失去所有的努力。有关如何执行此操作的更多信息,请参见:https://codex.wordpress.org/Child_Themes

--然后,转到您的函数。php并创建自定义帖子类型。它可以是这样的:

//custom post type
/*
* Creating a function to create our CPT
*/

function my_custom_post_type() {

// Set UI labels for Custom Post Type
$labels = array(
    \'name\'                => _x( \'Properties\', \'Post Type General Name\', \'custompsotype\' ),
    \'singular_name\'       => _x( \'Property\', \'Post Type Singular Name\', \'custompsotype\' ),
    \'menu_name\'           => __( \'Properties\', \'custompsotype\' ),
    \'parent_item_colon\'   => __( \'Parent Property\', \'custompsotype\' ),
    \'all_items\'           => __( \'All Properties\', \'custompsotype\' ),
    \'view_item\'           => __( \'View Property\', \'custompsotype\' ),
    \'add_new_item\'        => __( \'Add New Property\', \'custompsotype\' ),
    \'add_new\'             => __( \'Add New\', \'custompsotype\' ),
    \'edit_item\'           => __( \'Edit Property\', \'custompsotype\' ),
    \'update_item\'         => __( \'Update Property\', \'custompsotype\' ),
    \'search_items\'        => __( \'Search Property\', \'custompsotype\' ),
    \'not_found\'           => __( \'Not Found\', \'custompsotype\' ),
    \'not_found_in_trash\'  => __( \'Not found in Trash\', \'custompsotype\' ),
);

// Set other options for Custom Post Type

$args = array(
    \'label\'               => __( \'properties\', \'custompsotype\' ),
    \'description\'         => __( \'Properties\', \'custompsotype\' ),
    \'labels\'              => $labels,
    // Features this CPT supports in Post Editor
    \'supports\'            => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'comments\', \'revisions\', \'custom-fields\', ),
    // 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\'   => true,
    \'menu_position\'       => 5,
    \'can_export\'          => true,
    \'has_archive\'         => true,
    \'exclude_from_search\' => false,
    \'publicly_queryable\'  => true,
    \'menu_icon\'           => \'dashicons-format-aside\',
    \'capability_type\'     => \'page\',
);

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

}

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

add_action( \'init\', \'my_custom_post_type\', 0 );
--以下内容将创建新的职位类型。现在,您可以向其添加属性,以及属性类别(例如,“海上属性”),与您为普通帖子添加属性的方式完全相同

--您可以通过创建新的php文件来修改新帖子类型的外观/代码/布局,其中一个文件用于属性列表,名为archive-properties.php 一个用于单个属性,名为single-properties.php. 您可以在此处找到更多信息:https://codex.wordpress.org/Post_Type_Templates .

--为了向属性中添加自定义字段,我建议使用高级自定义字段(https://www.advancedcustomfields.com/), 在我看来,它是初学者最简单的选择,有很多选择和很棒的文档。

结束

相关推荐