I need to get video and content from recent posts in specific category called "Video" and show it on my sidebar.

The problem is when I need to have limited text content. so, when I use the_content(); I can see video within sidebar, but when I use the_excerpt(); to limit text, video is gone.

The code I have is doing what I need with posts and categories but I'm stuck here:

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

<?php endwhile; ?>

Tho, I have one more content limit function that I can use in my function.php but as I am new at WordPress and PHP, I don't know what to do next and could use any help that you can offer.

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); 
    $content = implode(" ",$content); 
    echo ($content) . "...";

with <?php content(8); ?> that I'm calling in my Loop. This do what I need with content limitation, but still doesn't show video.

  • 1
    Your function is stripping all of the tags from the content, including images and video. That is, your function appears to be working correctly, but it is not the function you need for this purpose. You will need to rewrite that function so that it preserves the videos. Please edit the question to include your precise requirements for this function.
    – s_ha_dum
    Apr 20, 2013 at 20:52

3 Answers 3


If you're video embeds are always the same, I like the solution proposed by 5wpthemes, but if you want to avoid having to use a custom field ( and more specifically, remembering to do it), you could also try the code below ( which also requires the code to be very similar in every post ).

<?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; ?>
  • DON'T use query_posts Apr 26, 2014 at 6:43
  • Indeed, the use of query_posts is discouraged. Edited my answer.
    – bsoist
    Apr 28, 2014 at 14:17

In youy loop just past this snippet of code.

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

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

<?php endwhile; ?>

Replace 10 with any number you want.

Tell me if it works.

  • 3
    What you suggest sounds very much like what the OP is already doing. Read the last sentence of the question.
    – s_ha_dum
    Apr 20, 2013 at 20:29
  • And DON'T use query_posts -1 Apr 26, 2014 at 6:43

Adding Video from youtube.(I didn't try from other chanels)

  1. Add a new custom field and name it for ex. VIDEO

  2. Add the code to incorporate video in the value Field like this:

If your code is:

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

Extract the src=”value” and paste this to your custom field value.

  1. In your loop call the custom field value like this
<?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;?>

Here is an working sample: http://5wpthemes.com/blog/how-to-add-video-in-sidebars-posts-pages-with-custom-fields/

Also you can use excerpt custom field to call the excerpt in your loop and limit how much you want.

  • May I ask you to fix the curly quotes? :)
    – fuxia
    Apr 21, 2013 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.