繁体   English   中英

如何在 wordpress 中自定义 get_media_embedded_in_content() function 并添加自定义类?

[英]How to customize get_media_embedded_in_content() function in wordpress and add custom classes?

首先,对于任何语法错误,我深表歉意,希望您能理解问题所在。

我正在将 html 站点转换为 wordpress 主题,并且博客页面上有一个视频帖子格式,我已经使主题支持视频和音频格式,所以我使用了 do_shortcode()get_media_embedded_in_content()函数来嵌入视频和视频。

我使用 Fancybox 库将视频显示为同一页面上的弹出窗口。

现在问题是嵌入视频文件,因为我希望最终的 output 采用 html 格式,如下所示:

<div class="video">
<div class="inner-box">
    <figure class="image-box">
        <img src="<?php echo CONSULTY_THEME_DIR_URI . '/src/images/pages/blog/news-19.jpg' ?>" alt="news-19">
        <span class="category">business</span>
        <div class="video-btn">
            <a href="**videos sites links or uploaded video link**" class="lightbox-image" data-caption=""><i
                    class="fas fa-play"></i></a>
        </div>
    </figure>
    <div class="lower-content">
        <ul class="post-info clearfix">
            <li><i class="far fa-user"></i><a href="blog.html">Admin</a></li>
            <li><i class="far fa-calendar-alt"></i>March 19, 2020</li>
            <li><i class="far fa-comment-alt"></i><a href="blog.html">97
                    Comments</a></li>
            <li><i class="far fa-heart"></i><a href="blog.html">1.5k Likes</a></li>
        </ul>
        <h2><a href="blog-details.html">Taking Action For Benefits Of Business</a></h2>
        <p>Eabore etsu dolore magn aliqua enim veniam quis nostrud exercitas reprehenderit
            voluptate sed bvelit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
            occaecat cupidatat non.</p>
        <div class="read-more-link"><a href="blog-details.html"><i class="fas fa-arrow-right"></i><span>Read
                    More</span></a></div>
    </div>
</div>

在 index.php 文件中,我添加了以下代码:

<?php get_header('page');

// See if "posts page" is set in Settings > Reading
$page_for_posts = get_option( 'page_for_posts' ); 
if ($page_for_posts) { ?>
<section class="page-title centerd"
    style="background-image: url(<?php echo CONSULTY_THEME_DIR_URI . '/src/images/pages/blog/page-title-5.jpg'?>)">
    <div class="container">
        <div class="content-box clearfix">
            <h1><?php echo get_queried_object()->post_title; ?></h1>
            <ul class="bread-crumb clearfix">
                <?php 
                echo '<li><a href="'. home_url() .'" rel="nofollow">Home</a></li>'; 
                echo    '<li>' . get_queried_object()->post_title . '</li>';
            ?>
            </ul>
            <!-- start breadcrumbs -->
            <?php //the_breadcrumb(); ?>
            <!-- end breadcrumbs -->
        </div>
    </div>
</section>
<?php } ?>

<!-- =========== **************** =============== -->

<!-- Start Sidebar Page Container -->

<section class="sidebar-page-container">
    <div class="container">
        <div class="row clearfix">
            <div class="col-lg-9 col-md-12 col-sm-12 content-side">
                <div class="blog-content">
                    <?php 
                    
                    if( have_posts() ){
                        
                        while( have_posts() ){ 
                            
                            the_post();
                        
                            get_template_part( 'template-parts/content', get_post_format() );
                        
                        }
                        
                    }
                    
                    ?>
                </div><!-- /.blog-content -->
                <!-- <div class="pagination-wrapper centerd">
                    <ul id="pagi" class="pagination clearfix">
                        <li><a class="active" href="blog.html">1</a></li>
                        <li><a href="blog.html">2</a></li>
                        <li><a href="blog.html">3</a></li>
                        <li><a href="blog.html"><i class="fas fa-arrow-right"></i></a></li>
                    </ul>
                </div> -->
                <div class="pagination-wrapper centerd">
                    <?php echo consulty_numbering_paginate(); ?>
                </div>
                <!--pagination-wrapper-->
            </div><!-- /.col-lg-9 col-md-12 col-sm-12 .content-side - index.php -->
            <div class="col-lg-3 col-md-12 col-sm-12 sidebar-side">
                <div class="sidebar">
                    <?php dynamic_sidebar('consulty_sidebar'); ?>
                </div>
            </div><!-- /.col-lg-3 col-md-12 col-sm-12 .sidebar-side -->
        </div><!-- /.row -->
    </div><!-- /.container -->
</section><!-- /section .sidebar-page-container -->

<!-- End Sidebar Page Container -->

<?php get_footer(); ?>

在 functions.php 文件中,我添加了以下代码:

function consulty_get_embeded_media( $type = array() ) {
$content    =   do_shortcode( apply_filters( 'the_content', get_the_content() ) );
$embed      =   get_media_embedded_in_content( $content, $type );

if  ( in_array ( 'audio', $type ) ) {
    $output     =   str_replace( '?visual=true', '?visual=false', $embed[0] );
} else {
    $output     =  $embed[0];
}

return $output;

}

然后我在 content-video.php 文件中添加了以下代码:

    <?php 
/**
 * -- Video Post Format
 */
?>
<div class="video">
    <div class="inner-box">
        <figure class="image-box">
            <?php the_post_thumbnail(); ?>
            <!-- <img src="<?php //echo CONSULTY_THEME_DIR_URI . '/src/images/pages/blog/news-19.jpg' ?>" alt="news-19"> -->
            <span class="category"><?php the_category( ', ' ); ?></span>
            <div class="video-btn">
            <!-- <div class="embed-responsive embed-responsive-16by9 centerd"> -->
                <?php 
                echo consulty_get_embeded_media( array( 'video', 'iframe' ) );
                ?>
                <!-- <a href="videos sites links or uploaded video link" class="lightbox-image" data-caption=""><i
                        class="fas fa-play"></i></a> -->
            </div>
        </figure>
        <div class="lower-content">
            <ul class="post-info clearfix">
                <li>
                    <i class="far fa-user"></i><a
                        href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>"><?php the_author(); ?></a>
                </li>
                <li><i class="far fa-calendar-alt"></i><?php the_time('F jS, Y') ?></li>
                <li><i class="far fa-comment-alt"></i>
                    <?php //comments_popup_link(); 
                            comments_popup_link( 'No Comment', '1 Comment', '% Commentston', '', 'Comments Off' );
                            ?>
                </li>
                <li>
                    <i class="far fa-folder"></i>
                    <?php
                                                $categories     =   get_the_category();
                                                $separator      =   ', ';
                                                $output         =   '';

                                                if ( $categories ) {
                                                    foreach ( $categories as $category) {
                                                        $output .=  '<a href="' . get_category_link( $category->term_id ) . '">' . $category->cat_name . '</a>' . $separator;
                                                    }
                                                    echo trim($output, $separator);
                                                }
                                            ?>
                </li>
            </ul><!-- /ul /.post-info -->
            <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <p>
                <?php the_excerpt(); ?>
            </p>
            <div class="read-more-link">
                <a href="<?php the_permalink(); ?>">
                    <i class="fas fa-arrow-right"></i>
                    <span><?php _e('Read More'); ?></span>
                </a>
            </div>
        </div>
    </div>
</div><!-- /.video -->

这是预览如何打开视频的链接: https://consulty.hatemfrere.com/blog.html

有没有办法实现这个想法?

提前致谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM