Ph: 786-539-6996

The_excerpt() wordpress function as a variable

We can use this function in many different ways, specifically to extract the information from the post. The most typical use is to do the loop:

Case 1. Assign the content of the WordPress post to a variable instead of print it in a page to show it.

But what if we want to assign its value to a variable? For example, suppose we have a set of posts related to the category Home Videos. We want the client write the code of the video (from youtube) in the post and our code has to extract this content put it inside of the youtube container:

Post number 1- Name: Video Home 1. Content: vtPk5IUbdH0. Category: Home Videos.

Post Number 2- Name Video Home 2. Content: Category: Home Videos

In order to extract the content of the post we have to do the loop:

<?php query_posts(‘category_name=Home Videos&posts_per_page=3’);
while ( have_posts() ) : the_post();$valorvideo= get_the_excerpt();/*here we assign the value to the variable. We cannot do this: $valorvideo=the_excerpt(); */
endwhile; wp_reset_query();}?>

/* now we can write the variable in order to rotate the value if the user select another video to see:
<div id=”show-video”>
<iframe src=”<?php echo $valorvideo;?>?rel=0&autoplay=1;wmode=transparent” height=”360″ width=”640″ allowfullscreen=”” frameborder=”0″ ></iframe>

One example of this you can see it here:

and the reference here:

Case 2. Suppouse we want to reorder how we want to display the posts

Usually the posts come out by date. But we can change this. In order to preserve the original parameter information, we can add some parameters before the Loop:


global $query_string;
query_posts( $query_string . '&order=ASC' );

When using query_posts() in this way, the quoted portion of the parameter must begin with an ampersand (&).
You can see more information of this interesting idea here:

January 2nd, 2016 | aesio

Leave a Reply

Your email address will not be published. Required fields are marked *

Time Programmer Corp Since 2014 ®