元框
您是否考虑过使用帖子元框?
然后,您可以将所需的所有文本添加到页面,然后添加 2 个帖子元框以添加 vimeo 链接和库中图像的功能。
我以前使用它们在帖子旁边的侧栏中设置链接和其他数据,以及添加到 Wordpress 中已有的功能。
它们使用起来超级简单,例如:
告诉 Wordpress 添加新的元框:
/**
* Adds a meta box to the post editing screen
*/
function prfx_custom_meta() {
add_meta_box( 'prfx_meta', __( 'Meta Box Title', 'prfx-textdomain' ), 'prfx_meta_callback', 'post' );
}
add_action( 'add_meta_boxes', 'prfx_custom_meta' );
向框中添加内容/表单:
/**
* Outputs the content of the meta box
*/
function prfx_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$prfx_stored_meta = get_post_meta( $post->ID );
?>
<p>
<label for="meta-text" class="prfx-row-title"><?php _e( 'Example Text Input', 'prfx-textdomain' )?></label>
<input type="text" name="meta-text" id="meta-text" value="<?php if ( isset ( $prfx_stored_meta['meta-text'] ) ) echo $prfx_stored_meta['meta-text'][0]; ?>" />
</p>
<?php
}
将内容保存在元框中,因为 Wordpress 无法自行识别表单:
/**
* Saves the custom meta input
*/
function prfx_meta_save( $post_id ) {
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// Checks for input and sanitizes/saves if needed
if( isset( $_POST[ 'meta-text' ] ) ) {
update_post_meta( $post_id, 'meta-text', sanitize_text_field( $_POST[ 'meta-text' ] ) );
}
}
add_action( 'save_post', 'prfx_meta_save' );
在前端显示数据(按帖子ID)
<?php
// Retrieves the stored value from the database
$meta_value = get_post_meta( get_the_ID(), 'meta-text', true );
// Checks and displays the retrieved value
if( !empty( $meta_value ) ) {
echo $meta_value;
}
?>
您只需要根据您的需要和需要进行修改,这将比在帖子中使用preg_match 简单得多,如果您想在帖子中添加内嵌图片怎么办?
简码
如果您只想更改帖子的内联内容,您是否查看过帖子简码?然后您可以将内容放入短代码中,并告诉 wordpress 在该代码显示在前端之前添加您想要的任何代码。
举个例子:
//[foobar]
function foobar_func( $atts ){
return "foo and bar";
}
add_shortcode( 'foobar', 'foobar_func' );
将打印出来:
foo and bar
这也将比preg_match 方式更简单。
在这里阅读更多信息:
短代码:http://codex.wordpress.org/Shortcode_API
元框:http://codex.wordpress.org/Function_Reference/add_meta_box