【问题标题】:wordpress: Split images videos and text in postwordpress:在帖子中拆分图像视频和文本
【发布时间】:2014-12-15 16:25:45
【问题描述】:

您好,对于我正在设置的 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 嵌入式视频的方法。如果视频和图像实际上混合在一起就可以了,因为它们会在彼此之下。

提前致谢!

【问题讨论】:

    标签: wordpress post video vimeo


    【解决方案1】:

    元框

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      • 2016-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多