以防万一Rodolfo\'s answer 不清楚,以下是(我认为)他的建议-顺便说一下,这是一个很好的主意:
创建一个名为Snippets的自定义post类型,我们将在其post中存储代码片段。
我们将使用supports
中的参数register_post_type
对我们有利。我们不希望编辑屏幕上的内容编辑器用于自定义帖子类型,因为我们将在自定义元框中存储内容(本质上是一个自定义字段),所以我们将隐藏它;以及其他不必要的字段,因为这将是一种自定义帖子类型。
下面是一个示例,让您了解如何做到这一点:
/*
* Register Custom Post Types
* DON\'T FORGET TO FLUSH PERMALINKS
*
* http://justintadlock.com/archives/2013/09/13/register-post-type-cheat-sheet
* https://gist.github.com/justintadlock/6552000
* http://core.trac.wordpress.org/browser/trunk/src/wp-includes/post.php
*/
add_action( \'init\', \'itsme_register_post_types\' );
function itsme_register_post_types() {
// Post Type: Snippets
register_post_type(
// Keeping it unique so that it never conflicts with a plugin or theme
\'itsme_snippet\',
array(
\'description\' => \'Code snippets for sharing in posts.\',
\'public\' => false, // NOTE!
\'publicly_queryable\' => false, // NOTE!
\'exclude_from_search\' => true, // NOTE!
\'show_in_nav_menus\' => false,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_in_admin_bar\' => false,
\'menu_position\' => 5, // Your choice.
\'menu_icon\' => null,
\'can_export\' => false,
\'delete_with_user\' => false,
\'hierarchical\' => false,
\'taxonomies\' => array(),
\'has_archive\' => false, // NOTE!
\'query_var\' => false, // NOTE!
\'capability_type\' => \'post\',
\'rewrite\' => array(
\'slug\' => \'snippets\',
\'with_front\' => false,
\'pages\' => false,
\'feeds\' => false, // NOTE!
),
// IMPORTANT! At least make sure that the \'editor\' (Content Editor) is hidden - i.e. remove it from the array.
\'supports\' => array( \'title\', \'revisions\', ),
\'labels\' => array(
\'name\' => __( \'Snippets\' ),
\'singular_name\' => __( \'Snippet\' ),
\'menu_name\' => __( \'Snippets\' ),
\'name_admin_bar\' => __( \'Snippets\' ),
\'add_new\' => __( \'Add New\' ),
\'add_new_item\' => __( \'Add New Snippet\' ),
\'edit_item\' => __( \'Edit Snippet\' ),
\'new_item\' => __( \'New Snippet\' ),
\'view_item\' => __( \'View Snippet\' ),
\'search_items\' => __( \'Search Snippets\' ),
\'not_found\' => __( \'No Snippets found\' ),
\'not_found_in_trash\' => __( \'No Snippets found in trash\' ),
\'all_items\' => __( \'All Snippets\' ),
\'parent_item\' => __( \'Parent Snippet\' ),
\'parent_item_colon\' => __( \'Parent Snippet:\' ),
\'archive_title\' => __( \'Snippets\' ),
)
)
);
}
Related Links: (因为我没有在代码中给出任何解释。)
http://codex.wordpress.org/Post_Types
http://codex.wordpress.org/Function_Reference/register_post_type
http://core.trac.wordpress.org/browser/trunk/src/wp-includes/post.php
创建一个“textarea”类型的元框(本质上是一个自定义字段),该元框仅显示在自定义帖子类型片段的编辑屏幕上。如果你不知道how to code one, 您可以使用以下插件轻松创建一个Advanced Custom Fields. 下面是一个屏幕截图,只为您提供一个想法:
创建一个自定义短代码,该代码接受代码段帖子的id,并在<pre>
标签;当然,所有HTML特殊字符都已正确转义。
以下是一个好主意的示例:
/*
* Custom Shortcode: Snippet
* USAGE: [snippet id="188"], where id = Snippet (CPT) post\'s ID
*
* http://wpquestions.com/question/showChrono?id=8901
*/
add_shortcode( \'snippet\', \'bbcode_snippet\' );
function bbcode_snippet( $atts ) {
extract( shortcode_atts( array(
\'id\' => null,
), $atts ) );
$post = get_post( $id );
$content = $post->post_content;
$content = trim( htmlspecialchars( clean_pre( $content ) ) );
return \'<pre><code>\' . $content . \'</code></pre>\';
}
Related Links:- 要稍微简化一些,请创建一个自定义元框,其中显示要使用的快捷码(例如。
[snippet id="120"]
) 在自定义帖子类型(代码段)的编辑屏幕上。因此,一旦发布了代码片段,就可以复制短代码,以便在帖子中嵌入代码。add_action( \'add_meta_boxes\', \'itsme_custom_meta_boxes\' );
function itsme_custom_meta_boxes() {
add_meta_box(
// HTML \'id\' attribute of the edit screen section (AKA meta box)
\'itsme-snippet-shortcode-box\',
// Title of the meta box, visible to user
esc_html__( \'Snippet Shortcode\' ),
// Callback function
\'itsme_snippet_shortcode_box\',
// Custom post type on whose edit screen to show meta box
\'aahank_vault\',
// The part of edit screen where the meta box should be shown
\'side\',
// Priority within the context where the meta box should show
\'high\'
);
}
// Function to display the meta box
function itsme_snippet_shortcode_box() {
global $post;
$snippet_id = $post->ID;
echo \'<textarea rows="1" readonly="readonly" placeholder="Publish post first!" onclick="this.focus();this.select();">[snippet id="\' . $snippet_id . \'"]</textarea>\';
}
这就是我们所需要的。语法突出显示和其他内容都是额外的,所以我不想深入了解更多细节。Footnotes:
<就像鲁道夫说的那样,插件和主题很容易搞乱the_content
. 因为我们想保持代码片段不变(除非WE 我们使用一个自定义字段来存储数据,即代码片段。此外,默认情况下,WordPress上有很多过滤器the_content
, 使做我们想做的事情变得非常复杂。相反,使用自定义字段可以使整个过程更加简单和直接。
Ideas:
<您可以使用名为language
或者组织自定义帖子类型的帖子,甚至用它来语法突出显示代码