I'm trying to add the image caption to the alt attribute of images in a gallery but my code doesn't work. Below is part of the gallery shortcode modified to implement a slideshow. On the bottom I am using wp_get_attachment_image() with $default_attr as the array of attributes containing the caption. The caption does not show in HTML.
$id = intval($id);
if ( 'RAND' == $order )
$orderby = 'none';
if ( !empty($include) ) {
$include = preg_replace( '/[^0-9,]+/', '', $include );
$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( !empty($exclude) ) {
$exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
} else {
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
}
if ( empty($attachments) )
return '';
if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $att_id => $attachment )
$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
return $output;
}
$i = 0;
$default_attr = array(
'src' => $src,
'class' => "attachment-$size",
'alt' => trim(strip_tags( $attachment->post_excerpt ))
);
foreach ( $attachments as $attachment ) {
<a href='".wp_get_attachment_url($attachment->ID)."'>".wp_get_attachment_image($attachment->ID, $size, false, $default_attr)."</a>
}
return $output;
You can use:
wp_get_attachment_url($attachment->post_title);
Came through this while searching for the same answer, found it and wanted to share it:
$attachment=get_post($attachment_id);
$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
$image_title = $attachment->post_title;
$caption = $attachment->post_excerpt;
$description = $image->post_content;
to output your caption formatted by wordpress (adding breaks and paragraphs) you can just use:
$caption = apply_filters('the_content', $caption);
Related
I have a brand page (as parent) with several products (children), product groups (also children) with products (grand children) as pages. I'm querying a lot which seems a bit ridiculous. Is there a better way to achieve this the following?
Brand (parent page)
Product (child page)
Product group (parent page)
Product (child page)
Product group (parent page)
Product (child)
Code I'm using to query the pages:
<?php
$args = array(
'cat' => 54,
'orderby' => 'menu_order',
'order' => 'ASC',
'hierarchical' => 1,
'post_parent' => $post->ID,
'parent' => -1,
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
get_template_part( 'strips/card-product' );
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
$args = array(
'category__not_in' => 54,
'order' => 'ASC',
'orderby' => 'menu_order',
'hierarchical' => 1,
'post_parent__in' => array($post->ID),
'parent' => -1,
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
// INSIDE PRODUCT GROUP
if ( in_category('product-groep') ) {
$args = array(
'category__not_in' => 54,
'order' => 'ASC',
'orderby' => 'menu_order',
'hierarchical' => 1,
'post_parent' => get_the_id(),
'parent' => -1,
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
// The Query
$query1 = new WP_Query( $args );
// The Loop
while ( $query1->have_posts() ) {
$query1->the_post();
if ( in_category('product-groep') ) {
$args = array(
'category__not_in' => array(54,8),
'order' => 'ASC',
'orderby' => 'menu_order',
'hierarchical' => 1,
'post_parent' => get_the_id(),
'parent' => -1,
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish',
'post__not_in' => array(692)
);
// The Query
$query2 = new WP_Query( $args );
// The Loop
while ( $query2->have_posts() ) {
$query2->the_post();
get_template_part( 'strips/card-product' );
}
wp_reset_postdata();
} else
{
}
}
wp_reset_postdata();
// OUTSIDE PRODUCT GROUP
}
else {
}
?>
<!-- END LOOP 1 -->
<?
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>
Use this WP function get_pages() and get_page_children()
function get_child_pages( $parent_page_ID ){
$all_pages = get_pages( array( 'post_type'=> 'page' ) );
$child_pages = get_page_children( $parent_page_ID, $all_pages );
if( !empty( $child_pages ) ){
$html .= '<ul>';
foreach ( $child_pages as $key => $child_page ) {
$html .= '<li>'.$child_page->post_title;
get_child_pages( $child_page->ID );
$html .= '</li>';
}
$html .= '</ul>';
}
return $html;
}
function list_pages(){
$html = '';
$parent_pages = get_pages( array( 'parent' => 0, 'post_type'=> 'page' ) );
$html.= '<ul>';
foreach ( $parent_pages as $parent_page ) {
$html .= '<li>'.$parent_page->post_title;
$html .= get_child_pages( $parent_page->ID );
$html .= '</li>';
}
$html.= '</ul>';
return $html;
}
add_shortcode( 'list_pages', 'list_pages' );
This is what I have now in my page-name.php:
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => null, // any parent
'post_mime_type' => 'image'
);
$attachments = get_posts($args);
if ($attachments) {
$attachment_meta = wp_get_attachment($attachment->ID);
foreach ($attachments as $post) {
if ($attachment_meta['caption'] == 'Ceahlau' ){
setup_postdata($post);
echo wp_get_attachment_image( $attachment->ID, 'full' );
the_attachment_link($post->ID, false);
}
}
}
?>
And this is what I have in functions.php:
function wp_get_attachment( $attachment_id ) {
$attachment = get_post( $attachment_id );
return array(
'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
'caption' => $attachment->post_excerpt,
'description' => $attachment->post_content,
'href' => get_permalink( $attachment->ID ),
'src' => $attachment->guid,
'title' => $attachment->post_title
);
}
$attachment_meta = wp_get_attachment($attachment->ID);
Can you please tell me what is wrong with my functions? I want to post image only if they have "Ceahlau" in caption.
I want to implement a variation of the code below. This code currently shows a post's attachment title.
How I can modify the code so if there is no attachment it shows the post title.?
This is code for pulling first attachment title.
<?php $args = array( 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'numberposts' => 1, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$image_title = $attachment->post_title;?>
<?php echo $image_title; ?><?php } } ?>
Something like this?
<?php
$args = array( 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'numberposts' => 1, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$image_title = $attachment->post_title;
if($image_title) {
echo $image_title;
} else {
the_title();
}
}
}
The best way to do this would be the empty() function and a ternary expression
$image_title = ( ! empty( $attachment->post_title) )?
$attachment->post_title: // use the attachment title if it is not empty
$post->post_title; // otherwise use the post title
How can I get images to have the alt set to "slider"?
This is my code:
$args = array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'orderby' => 'menu_order',
'order' => 'ASC',
'numberposts' => 5,
'post_mime_type' => 'image'
);
if ( $images = get_children( $args ) ) {
echo '<div id="sliderbody" style="width:640px; height:480px;"><div id="slider">';
foreach( $images as $image ) {
echo wp_get_attachment_image( $image->ID, 'trueslider' );
}
echo '</div></div>';
}
I guess you're trying to set the alt attribute to always be "slider" so try this
$args = array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'orderby' => 'menu_order',
'order' => 'ASC',
'numberposts' => 5,
'post_mime_type' => 'image'
);
if ( $images = get_children( $args ) ) {
echo '<div id="sliderbody" style="width:640px; height:480px;"><div id="slider">';
foreach( $images as $image ) {
echo wp_get_attachment_image( $image->ID, 'trueslider', false, array('alt' => 'slider') );
}
echo '</div></div>';
}
Basically I am trying to get the gallery of parent page, and I've been playing with query_posts to do that, the following code seems to be getting me closer to what I need but it is actually getting the attachments from other places rather than only parent page of current page, any one?:
<?php
$args = array('post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => 0,
'order_by' => 'menu_order',
'order' => 'ASC');
$attachments = get_posts($args);
if($attachments)
{
echo '<ul class="imagelist">';
foreach($attachments as $attachment)
{
echo '<li>';
$large = wp_get_attachment_image_src($attachment->ID, 'large');
$thumb = wp_get_attachment_image($attachment->ID, 'thumbnail');
echo '' . $thumb . '';
echo '</li>';
}
echo '</ul>';
}
?>
You should have set post_parent to the current post parent ID:
global $post;
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->post_parent,
'order_by' => 'menu_order',
'order' => 'ASC'
);