web-dev-qa-db-ja.com

サイドバーに特定のカテゴリのビデオを表示する方法

最近の投稿から「ビデオ」という特定のカテゴリのビデオとコンテンツを取得し、それをサイドバーに表示する必要があります。

問題は、テキストの内容を制限する必要があるときです。そのため、the_content();を使用するとサイドバー内にビデオが表示されますが、the_excerpt();を使用してテキストを制限すると、ビデオは表示されなくなります。

私が持っているコードは私が投稿とカテゴリで必要なことをしています、しかし私はここで立ち往生しています:

<?php query_posts('cat=6&showposts=2'); ?>
<?php while (have_posts()) : the_post(); ?>

<?php endwhile; ?>

それで、私は私のfunction.phpで使うことができるもう一つの内容制限機能を持っています、しかし私がWordPressとPHPで新しいので、私は次に何をすべきかわからず、あなたが提供できるどんな助けでも使うことができます。

function content($num, $more_link_text = '(more...)') { 
    $theContent = get_the_content($more_link_text); 
    $output = preg_replace('/<img[^>]+./','', $theContent); 
    $output = strip_shortcodes($output);
    $output = strip_tags($output);
    $output = preg_replace("/\[caption.*\[\/caption\]/", '', $output);
    $limit = $num+1; 
    $content = explode(' ', $output, $limit); 
    array_pop($content); 
    $content = implode(" ",$content); 
    echo ($content) . "...";
}

loopで呼び出している<?php content(8); ?>を使って。これは私がコンテンツ制限で必要とすることをします、それでもビデオを見せません。

2
ecoLogic

ビデオの埋め込みが常に同じであれば、私は5wpthemesが提案している解決策が好きですが、カスタムフィールドを使用しないようにしたい場合(具体的にはそれを忘れないように)これはまた、すべての投稿でコードが非常に似ていることを要求します。

<?php $my_query = new WP query(array('cat'=>6, 'showposts'=> '2')); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    $theContent = get_the_content();
    $parts = explode("iframe",$theContent);
    ?><iframe<? echo $parts[1]; ?>iframe><?php
<?php endwhile; ?>
1
bsoist

Youyループで、このコードの断片を過ぎたところで。

<?php query_posts('cat=6&showposts=2'); ?>
<?php while (have_posts()) : the_post(); ?>

<?php content('10'); ?>


<?php endwhile; ?>

10を任意の番号に置き換えます。

うまくいったら教えてください。

0
5wpthemes

YouTubeから動画を追加する(他のチャンネルから試したことはありません)

  1. 新しいカスタムフィールドを追加して、exという名前を付けます。ビデオ

  2. 次のように、値Fieldにビデオを組み込むコードを追加します。

あなたのコードが

<iframe width=”300″ height=”180″ src=”http://www.youtube.com/embed/9bZkp7q19f0” frameborder=”0″ allowfullscreen></iframe>

Src =” value”を抽出して、これをカスタム項目値に貼り付けます。

  1. あなたのループでこのようなカスタムフィールド値を呼び出します
<?php $video_field=get_post_meta($post->ID,'VIDEO',true); ?>

<iframe width="300″ height="180" src="<?php echo $video_field;?>" frameborder="0" allowfullscreen="true"></iframe>

<?php endif;?>

これが作業サンプルです: http://5wpthemes.com/blog/how-to-add-video-in-sidebars-posts-pages-with-custom-fields/

また、抜粋カスタムフィールドを使用して、抜粋をループ内で呼び出し、必要な金額を制限することもできます。

0
5wpthemes