在自定义帖子类型上显示自定义字段的信息

时间:2013-09-27 08:53:15

标签: wordpress loops custom-post-type

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

我有一个功能,在wordpress后端的视频帖子类型页面上显示自定义元框。用户可以输入YouTube视频的ID或vimeo视频的ID - 然后wordpress会在自定义帖子类型页面上显示ID的视频。当用户向视频自定义帖子类型添加新帖子并将其分配给我指定的任何类别时,我希望wordpress显示不同的视频。我目前的代码没有做我想做的事情,因为它在每个帖子上显示相同的视频,即使在其中一些帖子上没有指定ID。例如,在音乐帖子页面上,我已经为它分配了类别音乐,并且在前端显示了一个vimeo视频ID,但是然后显示了相同的视频用于促销和商业广告,我不希望这种情况发生。我运行它的循环是(在single-videos.php中):

<?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();

?>

1 个答案:

答案 0 :(得分: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();

    ?>