Ask your WordPress questions! Pay money and get answers fast! (more info)

Add Excerpt to custom Wordpress Gallery? WordPress

  • SOLVED



$the_query = new WP_Query( $arrArgs );
$rows_img = $the_query->posts;
$numItemsPerRow = 100;
$strResultGallery = '';
$strResultGallery .= '<table class="cls-table-gallery'.$strGalleryClass.'">';
$strResultGallery .= '<tr>';
$strResultGallery .= $strPostContent ? '<td class="wpscls-post-content">'.$strPostContent.'</td>' : '';

if (count($rows_img) > 0 && !is_page())
{
foreach ($rows_img as $numKey01 => $row_img)
{
$strFullImg = $row_img->guid;
$numTmpPos = strpos($strFullImg, '.', strlen($strFullImg) - 5);
$strExt = strtolower(substr($strFullImg, $numTmpPos));
$strThumb = substr($strFullImg, 0, $numTmpPos).$strExt;
if ($strExt == '.jpg' || $strExt == '.jpeg' || $strExt == '.gif' || $strExt == '.png')
{
$strResultGallery .= '<td>'.'<img alt="'.$row_img->post_title.'" src="'.$strThumb.'" />';
$strResultGallery .= '<h5>'.'&nbsp;'.'</h5>';
$strResultGallery .= '<div class="capt"><h6>'.'$excerpt = get_the_excerpt();'.'</h6></div>';
$strResultGallery .= '</td>';
}
}
}
$strResultGallery .= '</tr>';
$strResultGallery .= '</table>';

if (count($rows_img) > 0 || $strPostContent) echo $strResultGallery;
break;
}
?>


I have updated this part:
<strong> $strResultGallery .= '<div class="capt"><h6>'.'$excerpt = get_the_excerpt();'.'</h6></div>';</strong>

However It's not working, can anyone suggest what to do?

Answers (4)

2013-09-24

Dbranes answers:

I'm not sure what kind of excerpts you mean and where you want to fetch them from.

But if you are looping throught image attachments then you could try this:


/**
* Use the text you need a) or b):
*/

// a) Text from the image description:
$text = $row_img->post_content;
// b) Text from the image caption
$text = $row_img->post_excerpt;

// your excerpt
$excerpt = apply_filters( 'the_excerpt', $text );

// add it to the output
$strResultGallery .= sprintf( '<div class="capt"><h6>%s</h6></div>', $excerpt );


Emma comments:

Hey this is the full code:

Where do i place yours inside it?

The excerpt is from the post page

<?php if ( have_posts() ) while ( have_posts() ) {
the_post();
global $post;

$strGalleryClass = '';
$strPostContent = '';
if (trim($post->post_content))
{
$strGalleryClass .= ' wpscls-with-content';
$strPostContent .= '<h2 class="wpscls-field-title wpscls-'.$post->post_name.'">'. get_the_title() .'</h2>';
$strPostContent .= '<div class="wpscls-field-content">';
ob_start();
the_content();
$strPostContent .= ob_get_contents();
ob_end_clean();
$strPostContent .= '</div>';
}

$arrArgs = array (
'posts_per_page' => 0,
'post_parent' => $post->ID,
'post_status' => 'any',
'orderby' => 'menu_order',
'order' => 'ASC',
'post_type' => array('attachment'),
);

$the_query = new WP_Query( $arrArgs );
$rows_img = $the_query->posts;
$numItemsPerRow = 100;
$strResultGallery = '';
$strResultGallery .= '<table class="cls-table-gallery'.$strGalleryClass.'">';
$strResultGallery .= '<tr>';
$strResultGallery .= $strPostContent ? '<td class="wpscls-post-content">'.$strPostContent.'</td>' : '';

if (count($rows_img) > 0 && !is_page())
{
foreach ($rows_img as $numKey01 => $row_img)
{
$strFullImg = $row_img->guid;
$numTmpPos = strpos($strFullImg, '.', strlen($strFullImg) - 5);
$strExt = strtolower(substr($strFullImg, $numTmpPos));
$strThumb = substr($strFullImg, 0, $numTmpPos).$strExt;
if ($strExt == '.jpg' || $strExt == '.jpeg' || $strExt == '.gif' || $strExt == '.png')
{
$strResultGallery .= '<td>'.'<img alt="'.$row_img->post_title.'" src="'.$strThumb.'" />';
$strResultGallery .= '<h5>'.'&nbsp;'.'</h5>';
$strResultGallery .= '<div class="capt"><h6>'.'.$row_img->post_title.'.'</h6></div>';
$strResultGallery .= '</td>';
}
}
}
$strResultGallery .= '</tr>';
$strResultGallery .= '</table>';

if (count($rows_img) > 0 || $strPostContent) echo $strResultGallery;
break;
}
?>


Dbranes comments:


if you want the image caption, try replacing this line

$strResultGallery .= '<div class="capt"><h6>'.'$excerpt = get_the_excerpt();'.'</h6></div>';

with

$strResultGallery .= sprintf( '<div class="capt"><h6>%s</h6></div>', $row_img->post_excerpt );


Emma comments:

What does the SprintF bit do? This hasn't worked either


Emma comments:

I only want it to show if there is an excerpt, my div and h6 style is showing without any text in it.


Dbranes comments:

You have to edit the images in the media library to add the caption, so most likely it's empty in your case.

Here is a nice tutorial how you can do it:

http://en.support.wordpress.com/images/image-settings/

ps: sprintf() is just another way of constructing strings from variables.


Dbranes comments:

To check if the image caption is set, you can do:


if( ! empty( $row_img->post_excerpt ) ){
$strResultGallery .= sprintf( '<div class="capt"><h6>%s</h6></div>', $row_img->post_excerpt );
}


Emma comments:

Hey this works however it doesn't show on the image I place it on.


Dbranes comments:

ok, looks like we are getting there ;-)

You might try to add captions to other images as well to test.


ps: it's also handy to debug with echo, print_r or var_dump


Emma comments:

haha thanks, i will give it a try, it seems to show on any random image i place it on. Maybe it's my div?


Dbranes comments:

I installed this theme on my localhost and it seems to work with the above changes,
i.e. I created a post, attached few images, added the image caption during upload,
and they are displayed under each image on the front-end.

2013-09-24

zebra webdesigns answers:

Have you test the the_excerpt();

$strResultGallery .= '<div class="capt"><h6>'.'$excerpt = the_excerpt();'.'</h6></div>';

2013-09-25

Arnav Joy answers:

try this code

<?php

$the_query = new WP_Query( $arrArgs );

$rows_img = $the_query->posts;

$numItemsPerRow = 100;

$strResultGallery = '';

$strResultGallery .= '<table class="cls-table-gallery'.$strGalleryClass.'">';

$strResultGallery .= '<tr>';

$strResultGallery .= $strPostContent ? '<td class="wpscls-post-content">'.$strPostContent.'</td>' : '';



if (count($rows_img) > 0 && !is_page())

{

foreach ($rows_img as $numKey01 => $row_img)

{

$strFullImg = $row_img->guid;

$numTmpPos = strpos($strFullImg, '.', strlen($strFullImg) - 5);

$strExt = strtolower(substr($strFullImg, $numTmpPos));

$strThumb = substr($strFullImg, 0, $numTmpPos).$strExt;

if ($strExt == '.jpg' || $strExt == '.jpeg' || $strExt == '.gif' || $strExt == '.png')

{

$strResultGallery .= '<td>'.'<img alt="'.$row_img->post_title.'" src="'.$strThumb.'" />';

$strResultGallery .= '<h5>'.'&nbsp;'.'</h5>';

if( ! empty( $row_img->post_excerpt ) )

$strResultGallery .= '<div class="capt"><h6>'.$row_img->post_excerpt.'</h6></div>';

$strResultGallery .= '</td>';

}

}

}

$strResultGallery .= '</tr>';

$strResultGallery .= '</table>';



if (count($rows_img) > 0 || $strPostContent) echo $strResultGallery;

break;

}

?>

2013-09-27

Just Me answers:

try this:

$strResultGallery .= '<div class="capt"><h6>'.echo get_the_excerpt();.'</h6></div>';