Hi Friends, In this post I am going to write about getting images from wordpress post content, I want this because I want to show post content without images on home page for one of my client, So below is code by which we can get all images one by one from content of post.
// Start the Loop
<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
<?php
// Set the post content to a variable
$temppostcontent = $post->post_content;
// Define the pattern to search
$temppattern = '~<img alt="" />]*\ />~';
// Run preg_match_all to grab all the images and save the results in $resultpics
preg_match_all( $temppattern, $temppostcontent, $resultpics );
// Count the results
$iNumberOfPics = count($resultpics[0]);
// Check to see if we have at least 1 image
if ( $iNumberOfPics > 0 )
{
// Now here you would do whatever you need to do with the images
// For this example I'm just going to echo them
for ( $i=0; $i < $iNumberOfPics ; $i++ )
{
echo $resultpics[0][$i];
};
};
// ...finish the loop,
?>
// Start the Loop
<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
<?php
// Set the post content to a variable
$temppostcontent = $post->post_content;
// Define the pattern to search
$temppattern = '~<img alt="" />]*\ />~';
// Run preg_match_all to grab all the images and save the results in $resultpics
preg_match_all( $temppattern, $temppostcontent, $resultpics );
// Count the results
$iNumberOfPics = count($resultpics[0]);
// Check to see if we have at least 1 image
if ( $iNumberOfPics > 0 )
{
// Now here you would do whatever you need to do with the images
// For this example I'm just going to echo them
for ( $i=0; $i < $iNumberOfPics ; $i++ )
{
echo $resultpics[0][$i];
};
};
// ...finish the loop,
?>
0 comments:
Post a Comment