posting image in worpdress only if they have * in caption - php

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.

Related

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

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

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

Wordpress portfolio page display only images from certain filter

I have been following many tutorials on a portfolio which uses quicksand.js to do a filter animation. In the function i can create filters (categories) with slugs. I can't, after endless hour of reading, figure out how to only display the thumbnails from a certain filter.
I've included the parts of the portfolio from my functions.php
I know this is a lot of code, but this is a last resort. I can't get it working so i am hoping some php / wordpress guru might point out something i've missed.
Through reading up on it, i did managed to pull out JUST the featured images posted in 'featured' by using:
<?php
$args = array(
'post_type' => 'portfolio',
'tax_query' => array(
array(
'taxonomy' => 'filter',
'field' => 'slug',
'terms' => 'featured'
)
)
);
$query = new WP_Query( $args );
?>
However, this just pulls out the image and it's title, without the formatted HTML i need. But applying this to what i already use is where i get completely lost.
Thanks in advance.
My Index.php
h2>Featured Work</h2>
<ul id="image_gallery" class="group index_gallery filterable-grid">
<?php
// Query Out Database
$wpbp = new WP_Query(array( 'post_type' => 'portfolio', 'posts_per_page' =>'9' ) );
?>
<?php
$terms = get_terms("filter");
$count = count($terms);
if ( $count > 0 ){
echo "<ul>";
foreach ( $terms as $term ) {
echo "<li>" . $term->name . "</li>";
}
echo "</ul>";
}
?>
<?php
// Begin The Loop
if ($wpbp->have_posts()) : while ($wpbp->have_posts()) : $wpbp->the_post();
?>
<?php
// Get The Taxonomy 'Filter' Categories
$terms = get_the_terms( get_the_ID(), 'filter' );
?>
<?php
$large_image = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'fullsize', false, '' );
$large_image = $large_image[0];
?>
<?php
//Apply a data-id for unique indentity,
//and loop through the taxonomy and assign the terms to the portfolio item to a data-type,
// which will be referenced when writing our Quicksand Script
?>
<li class="gallery_image" data-id="id-<?php echo $count; ?>" data-type="<?php foreach ($terms as $term) { echo strtolower(preg_replace('/\s+/', '-', $term->name)). ' '; } ?>">
<?php
// Check if wordpress supports featured images, and if so output the thumbnail
if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) :
?>
<?php // Output the featured image ?>
<a rel="prettyPhoto[gallery]" class="zoom" href="<?php echo $large_image ?>">
<img class="mag" src="<?php bloginfo('template_url'); ?>/imgs/mag.png"/><div class="thumb_bg"></div><?php the_post_thumbnail('portfolio'); ?>
</a>
<?php endif; ?>
<!--<?php // Output the title of each portfolio item ?>
<p><?php echo get_the_title(); ?></p>-->
</li>
<?php $count++; // Increase the count by 1 ?>
<?php endwhile; endif; // END the Wordpress Loop ?>
<?php wp_reset_query(); // Reset the Query Loop?>
</ul>
<div class="gallery_control">
<span class="icon-search"></span>View more
</div>
<?php
$taxonomy = 'filter';
$terms = get_terms( $taxonomy, '' );
if ($terms) {
foreach($terms as $term) {
echo '<p>' . '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> has ' . $term->count . ' post(s). </p> ';
}
}
?>
Portfolio workings:
// function: post_type BEGIN
function post_type()
{
$labels = array(
'name' => __( 'Portfolio'),
'singular_name' => __('Portfolio'),
'rewrite' => array(
'slug' => __( 'portfolio' )
),
'add_new' => _x('Add Item', 'portfolio'),
'edit_item' => __('Edit Portfolio Item'),
'new_item' => __('New Portfolio Item'),
'view_item' => __('View Portfolio'),
'search_items' => __('Search Portfolio'),
'not_found' => __('No Portfolio Items Found'),
'not_found_in_trash' => __('No Portfolio Items Found In Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array(
'title',
'editor',
'thumbnail'
)
);
register_post_type(__( 'portfolio' ), $args);
} // function: post_type END
// function: portfolio_messages BEGIN
function portfolio_messages($messages)
{
$messages[__( 'portfolio' )] =
array(
0 => '',
1 => sprintf(('Portfolio Updated. View portfolio'), esc_url(get_permalink($post_ID))),
2 => __('Custom Field Updated.'),
3 => __('Custom Field Deleted.'),
4 => __('Portfolio Updated.'),
5 => isset($_GET['revision']) ? sprintf( __('Portfolio Restored To Revision From %s'), wp_post_revision_title((int)$_GET['revision'], false)) : false,
6 => sprintf(__('Portfolio Published. View Portfolio'), esc_url(get_permalink($post_ID))),
7 => __('Portfolio Saved.'),
8 => sprintf(__('Portfolio Submitted. <a target="_blank" href="%s">Preview Portfolio</a>'), esc_url( add_query_arg('preview', 'true', get_permalink($post_ID)))),
9 => sprintf(__('Portfolio Scheduled For: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Portfolio</a>'), date_i18n( __( 'M j, Y # G:i' ), strtotime($post->post_date)), esc_url(get_permalink($post_ID))),
10 => sprintf(__('Portfolio Draft Updated. <a target="_blank" href="%s">Preview Portfolio</a>'), esc_url( add_query_arg('preview', 'true', get_permalink($post_ID)))),
);
return $messages;
} // function: portfolio_messages END
// function: portfolio_filter BEGIN
function portfolio_filter()
{
register_taxonomy(
__( "filter" ),
array(__( "portfolio" )),
array(
"hierarchical" => true,
"label" => __( "Filter" ),
"singular_label" => __( "Filter" ),
"rewrite" => array(
'slug' => 'filter',
'hierarchical' => true
)
)
);
} // function: portfolio_filter END
add_action( 'init', 'post_type' );
add_action( 'init', 'portfolio_filter', 0 );
add_filter( 'post_updated_messages', 'portfolio_messages' );
I am not sure what you are doing here but change this
<?php
$large_image = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'fullsize', false, '' );
$large_image = $large_image[0];
?>
to this
<?php
$large_images[] = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'fullsize', false, '' );
$large_image = $large_images[0];
?>
I found a solution:
<?php
$args = array(
'post_type' => 'project',
'tax_query' => array(
array(
'taxonomy' => 'filter',
'field' => 'slug',
'terms' => 'featured'
)
)
);
$query = new WP_Query( $args );
?>
Only displays my filtered item with a name of featured.
This is the code I use to get full image and thumb of attached images:
$args = array(
'order' => 'ASC',
'post_mime_type' => 'image',
'post_parent' => $post->ID,
'post_status' => null,
'post_type' => 'attachment',
);
$upload_dir = wp_upload_dir();
$upload_url = $upload_dir['baseurl'];
// Get img data
$attachments = get_children( $args );
$images = array();
// Loop through attached images and get thumb + large img url
foreach($attachments as $attachment) {
$image_attributes = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' );
$img['thumb'] = $image_attributes[0];
$image_attributes = wp_get_attachment_image_src( $attachment->ID, 'large' );
$img['large'] = $image_attributes[0];
array_push($images,$img);
}
// Get the image that you have set as the featured image in the post
$featured_img = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
$featured_img_thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' );

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