PHP - Show attachment title, if it's null, show post title - php

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

Related

List child page IDs not using certain template

Is it possible to get an array of all wordpress child page IDs that are no using a certain template?
I know I can get the template with:
'meta_key' => '_wp_page_template',
'meta_value' => 'template.php'
and I expect I could use get_pages, but not sure how I can put the query together.
This works, but is there a better way of doing it?
$args = array(
'post_parent' => $parent_ID,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'publish'
);
$children = get_children( $args );
$exclude_children = null;
foreach($children as $child) {
$metadata = get_post_meta($child->ID);
if($metadata['_wp_page_template'][0] == 'template.php') {
$args = array(
'post_parent' => $child->ID,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'publish'
);
$exclude_array = get_children( $args );
foreach($exclude_array as $exclude) {
$exclude_children .= $exclude->ID.',';
}
}
}

get_children() and set alt images = slider

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>';
}

WordPress: Display Post Images on Front Page

Hoping someone can help. I've put this code into a standard template but no images are displaying, despite the fact that I've got a bunch of posts with images.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$images =& get_children( array (
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image'
));
if ( empty($images) ) {
// no attachments here
} else {
foreach ( $images as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id, 'thumbnail' );
}
}
endwhile; endif; ?>
Thanks for helping!
change
echo wp_get_attachment_image( $attachment_id, 'thumbnail' );
to
echo wp_get_attachment_image( $attachment->ID, 'thumbnail' );
and
$images =& get_children( array (
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image'
));
to
$images =get_posts( array (
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image'
));
you should use get_posts()
You can do something like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo wp_get_attachment_image( $attachment->ID, 'full' );
}
}
endwhile; endif; ?>
This code will fetch all the 'large' images from the media library and display them
Hope this is what you were looking for

How to get attachments of parent page only with query_posts?

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'
);

wordpress get image caption with wp_get_attachment_image()

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);

Categories