0

我将视频设置为自定义帖子类型,并将音乐、商业、促销设置为此帖子类型中的类别:

我有一个功能,可以在 wordpress 后端的视频帖子类型页面上显示自定义元框。用户可以输入 youtube 视频的 ID 或 vimeo 视频的 ID - wordpress 然后在自定义帖子类型页面上显示该 ID 的视频。当用户将新帖子添加到视频自定义帖子类型并将其分配给我指定的任何类别时,我希望 wordpress 显示不同的视频。我目前拥有的代码并没有做我想要它做的事情,因为它在每个帖子上显示相同的视频,即使其中一些没有指定 ID。例如,在音乐发布页面上,我为其分配了音乐类别并放置了一个 vimeo 视频 ID,该 ID 显示在前端,但随后显示相同的视频用于促销和商业,我不希望这种情况发生。

<?php

$args = array( 'post_type' => 'videos', 'posts_per_page' => 20, 'orderby' => 'date', 'order' => 'ASC' );
                    $loop = new WP_Query( $args );
                    while ( $loop->have_posts() ) : $loop->the_post();
//$args = array( 'post_type' => 'videos', 'posts_per_page' => 20, 'orderby' => 'date', 'order' => 'ASC' );
$ytubeID = get_post_meta($post->ID, '_youtubeID', true);
$vimID = get_post_meta($post->ID, '_vimeoID', true);
if ($ytubeID || $vimID){
if ($ytubeID){

echo '<iframe title="YouTube video player" class="youtube-player" type="text/html" src="http://www.youtube.com/embed/'.$ytubeID.'"  allowfullscreen="true" frameborder="0" width="640" height="390">';

echo '</iframe>';
} elseif ($vimID){
echo '<br />';
echo '<iframe src="http://player.vimeo.com/video/'.$vimID.'" width="640" height="390" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
}//end if yutbeID or vimIDthe_excerpt(); //excerpt added for information
}

endwhile;
                    wp_reset_query();

?>
4

1 回答 1

1

根据当前帖子类别显示视频

<?php

    $args = array( 'post_type' => 'videos', 'posts_per_page' => 20, 'orderby' => 'date', 'order' => 'ASC' );

    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();

        //$args = array( 'post_type' => 'videos', 'posts_per_page' => 20, 'orderby' => 'date', 'order' => 'ASC' );
        $ytubeID = get_post_meta($post->ID, '_youtubeID', true);
        $vimID = get_post_meta($post->ID, '_vimeoID', true);

        $videos_categories = array();           // ARRAY CONTAINING ALL CATEGORY ID ASSIGNED TO THIS POST
        $videos_cat_id = get_the_category(); // GET ALL CATEGORIES OBJECT ASIGNED TO CURRENT POST

        foreach($videos_cat_id as $videos_catid){
            $videos_categories[] = $catid->cat_ID;
        }
        $videos_cat_to_check = get_cat_ID( $videos_cat_name  ) // EXAMPLE get_cat_ID( 'music'  ) 



        if ($ytubeID || $vimID){
            if ($ytubeID && in_array($videos_cat_to_check,$videos_categories)){ // CHECK IF CURRENT POST HAS CATEGORY MUSIC 

                echo '<iframe title="YouTube video player" class="youtube-player" type="text/html" src="http://www.youtube.com/embed/'.$ytubeID.'"  allowfullscreen="true" frameborder="0" width="640" height="390">';

                echo '</iframe>';
                } elseif ($vimID){
                echo '<br />';
                echo '<iframe src="http://player.vimeo.com/video/'.$vimID.'" width="640" height="390" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
                }//end if yutbeID or vimIDthe_excerpt(); //excerpt added for information
        }

    endwhile;
    wp_reset_query();

    ?>
于 2013-09-27T10:35:21.807 回答