wordpress:在帖子中分割图像视频和文本

时间:2014-12-15 16:25:45

标签: wordpress post video vimeo

嗨,对于一个wordpress网站我设置我想在帖子中发布所有媒体(vimeo链接和媒体库中的图像)。我希望在显示时拆分它们,因为我需要在页面上的不同位置使用这3项。

我设法只显示文字:

ob_start();
                            the_content('Read the full post',true);
                            $postOutput = preg_replace('/<img[^>]+./','', ob_get_contents());
                             ob_end_clean();
                             ob_start();
                            echo $postOutput;

仅显示图像:

preg_match_all("/(<img [^>]*>)/",get_the_content(),$matches,PREG_PATTERN_ORDER);
                    for( $i=0; isset($matches[1]) && $i < count($matches[1]); $i++ ) {
                     echo $beforeEachImage . $matches[1][$i] . $afterEachImage;}

现在我正在寻找一种只发布vimeo嵌入式视频的方法。如果视频和图像实际混合是可以的,因为它们会在彼此之下。

提前致谢!

1 个答案:

答案 0 :(得分:0)

Meta Boxes

您是否考虑使用帖子元框?

然后,您可以将所需的所有文本添加到页面,然后添加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

Meta Boxes:http://codex.wordpress.org/Function_Reference/add_meta_box