I'm using the following code to pull all images uploaded to a post in a specific category. The idea is to have the images populate a slider as list elements but currently they're populating like so:
<li>
<img...>
<img...>
<img...>
<img...>
</li>
Instead of:
<li>
<img...>
</li>
<li>
<img...>
</li>
<li>
<img...>
</li>
Here's the code I'm using:
<?php
/*
Template Name: template_home
*/
get_header();
?>
<script src="<?php echo get_template_directory_uri(); ?>/js/jquery.bxslider.js"></script>
<ul class="bxslider" id="home-slider">
<?php
/*
this gets all the posts from a specific category
and all the attached images (uploaded to that post)
it will add in the "featured" image as the first image
*/
$category_name = 'home_slider';
query_posts('category_name='.$category_name.'&order=ASC&showposts=-1');
while (have_posts()) : the_post();
$media_array = array();
// GET FEATURED IMAGE /////////////////////////
if (has_post_thumbnail( $post->ID ) ){
$img_src_array = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
$img_src = $img_src_array[0];
$featured_img = $img_src;
array_push($media_array, $img_src);
}
//get other images ///////////////////////////
$args = array(
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => -1,
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
$img_array = wp_get_attachment_image_src( $attachment->ID, 'full');
$img_src = $img_array[0];
if($featured_img != $img_src){
array_push($media_array, $img_src);
}
}
}
//START OF PROJECT ///////////////////////////
//displays all images ///////////////////////
foreach($media_array as $image_src){
echo '<img src="'.$image_src.'" />';
}
/*
reset the query after we are done
*/
?>
<li><img src="<?php echo $img_src; ?>" alt="home-slide"></li>
<?php
endwhile;
?>
</ul>
<?php
get_footer();
?>
Thank you in advance
Please change your code in foreach loop, add LI in foreach loop.
Please check below code, I have changed in your code
<script src="<?php echo get_template_directory_uri(); ?>/js/jquery.bxslider.js"></script>
<ul class="bxslider" id="home-slider">
<?php
/*
this gets all the posts from a specific category
and all the attached images (uploaded to that post)
it will add in the "featured" image as the first image
*/
$category_name = 'home_slider';
query_posts('category_name='.$category_name.'&order=ASC&showposts=-1');
while (have_posts()) : the_post();
$media_array = array();
// GET FEATURED IMAGE /////////////////////////
if (has_post_thumbnail( $post->ID ) ){
$img_src_array = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
$img_src = $img_src_array[0];
$featured_img = $img_src;
array_push($media_array, $img_src);
}
//get other images ///////////////////////////
$args = array(
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => -1,
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
$img_array = wp_get_attachment_image_src( $attachment->ID, 'full');
$img_src = $img_array[0];
if($featured_img != $img_src){
array_push($media_array, $img_src);
}
}
}
//START OF PROJECT ///////////////////////////
//displays all images ///////////////////////
foreach($media_array as $image_src){
echo '<li><img src="'.$image_src.'" /></li>';
}
/*
reset the query after we are done
*/
?>
<li><img src="<?php echo $img_src; ?>" alt="home-slide"></li>
<?php
endwhile;
?>
</ul>
<?php
get_footer();
?>
you can insert li tag as below mention
foreach($media_array as $image_src){
echo '<li><img src="'.$image_src.'" /></li>';
}
Related
Hi I am making an wordpress website where I am going to upload amount of pictures.
Problem is if I upload more than 1 image on the same post or page, wordpress makes a slideshow automatically 'and makes a slider thumbnail also'.
I don't want any slidershows on the post or page and thumbnail also shouldn't slide.
this is my slider code:
function rowling_flexslider($size) {
if ( is_page()) :
$attachment_parent = $post->ID;
else :
$attachment_parent = get_the_ID();
endif;
if($images = get_posts(array(
'post_parent' => $attachment_parent,
'post_type' => 'attachment',
'numberposts' => -1, // show all
'post_status' => null,
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC',
))) { ?>
<?php if ( !is_single() ) : // Make it a link if it's an archive ?>
<a class="flexslider" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php else : // ...and just a div if it's a single post ?>
<div class="flexslider">
<?php endif; ?>
<ul class="slides">
<?php foreach($images as $image) {
$attimg = wp_get_attachment_image($image->ID,$size); ?>
<li>
<?php echo $attimg; ?>
<?php if ( !empty($image->post_excerpt) && is_single()) : ?>
<p class="post-image-caption"><span class="fa fw fa-camera"></span><?php echo $image->post_excerpt; ?></p>
<?php endif; ?>
</li>
<?php }; ?>
</ul>
<?php
if ( !is_single() )
echo '</a>';
else
echo '</div>';
}
}
How should I do? I tried to delete php codes but it wasn't a good way.
i want to display all the attached images of a page in the main slider of my home page. i have been using following code..
<div class="carousel-inner">
<?php if(have_posts()) : while (have_posts()) : the_post();?>
<?php
$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 ) {?>
<div class="item active">
<img src="<?php echo wp_get_attachment_src( $attachment_id, 'full' ); ?>" alt="First slide">
</div>
<?php
}
}
?>
<?php endwhile;else: ?>
<?php echo "No slider Images found" ?>
<?php endif; ?>
</div>
But its not loading the images . what is it that i am doing wrong . please help me out
This is how im doing it
$args = array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => 'any',
);
$attachments = get_posts($args);
foreach ($attachments as $att)
{
$imgThumb = image_downsize($att->ID, 'slider-image');
echo $img = '<img src="'.$imgThumb[0].'" alt="'.$att->postitle.'" />';
}
This is part of the code (its more complicated at its later put the image in <li> elements and sorts them and so on - but this should tell you how you may get images.
image_downsize is wordpress command that takes attachment id and its size (named or array of width and height) and then it returns the path to the image with that size (it also scales the image if needed).
You are using wp_get_attachment_image_src() function wrong way.
Here is the corrected code
<div class="carousel-inner">
<?php if(have_posts()) : while (have_posts()) : the_post();?>
<?php
$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 ) {
$image_attributes = wp_get_attachment_image_src( $attachment_id, 'full'); // returns an array
?>
<div class="item active">
<img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>">
</div>
<?php
}
}
?>
<?php endwhile;else: ?>
<?php echo "No slider Images found" ?>
<?php endif; ?>
</div>
I'm trying create a function that will load image attachments of a post into a list for FlexSlider to use. To specify, I want an image slider that is specific to a certain post. There may be many sliders on one page depending on the post type (in this case, the image slider type) I want.
I've run into an issue, however. This is in my functions.php file, a variation of Marty Spellerberg's jQuery Slideshow article (I would post a link but I can't post more than 2 links with less than 10 rep):
function flexslider($post_id) {
global $post;
$images = get_children(array('post_parent' => $post_id,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID'
));
if ($images) :
foreach ($images as $attachment_id => $image) :
$img_title = $image->post_title;
$img_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
if ($img_alt == '') : $img_alt = $img_title; endif;
$big_array = image_downsize( $image->ID, 'large' );
$img_url = $big_array[0];
?>
<li><img src="<?php echo $img_url; ?>" alt="<?php echo $img_alt; ?>" title="<?php echo $img_title; ?>" /></li>
<?php endforeach; ?>
<?php endif;
}
And this is what's calling the function in index.php:
<div class="flexslider">
<ul class="slides">
<?php flexslider('large','$post->ID'); ?>
</ul>
</div>
The problem is, it's only showing up on the permalink page. I want it to show up wherever the post is shown.
An example can be seen here: permalink and homepage.
Edit: Updated double quotes to singular quotations as per suggestion from comment.
Here is my solution. I basically did something a bit similar, but compressed it into one function. You can find more details on my blog.
<?php
function add_flexslider() { // display attachment images as a flexslider gallery
$attachments = get_children(array('post_parent' => get_the_ID(), 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'attachment', 'post_mime_type' => 'image','caption' => $attachment->post_excerpt, ));
if ($attachments) { // see if there are images attached to posting ?>
<!-- Begin Slider -->
<div class="flexslider">
<ul class="slides">
<?php // create the list items for images with captions
foreach ( $attachments as $attachment_id => $attachment ) {
echo '<li>';
echo wp_get_attachment_image($attachment_id, 'large');
echo '<p>';
echo get_post_field('post_excerpt', $attachment->ID);
echo '</p>';
echo '</li>';
} ?>
</ul>
</div>
<!-- End Slider -->
<?php } // end see if images
} // end add flexslider
?>
My current WordPress theme has a built in gallery. Each gallery displays the first / main thumbnail and once clicked opens the entire content of this specific gallery in a shadowbox. My ideal goal now is to find a way to alter this that instead of displaying the shadowbox directly it would direct to a single page with the specific galleries content.
As below my current gallery code:
<div id="maincontentwrap">
<div class="main-sep">
</div><!-- .main-sep -->
<div id="contentwrap">
<div id="content-gallery" role="main">
<?php
$wp_query = new WP_Query();
$wp_query->query( array( 'post_type' => 'galleries', 'posts_per_page' => of_get_option('le_gallery_items'), 'paged' => $paged, 'orderby' => 'menu_order', 'order' => 'ASC') );
$mod = 1;
while ( $wp_query->have_posts() ) : $wp_query->the_post();
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order') );
if(has_post_thumbnail()) {
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
$src = $src[0];
}
else{
foreach ( $attachments as $id => $attachment ) {
$src = wp_get_attachment_url( $id );
}
}
if ($mod % 3 == 0) {
echo ' <div class="gallery-entry-img-last">';
}
else{
echo ' <div class="gallery-entry-img">';
}
?>
<div class="gallery-entry-img-l"><a rel="next" href="<?php echo $src; ?>"><span class="rollover" ></span><img class="blog-entry-img" src="<?php echo get_bloginfo('stylesheet_directory'); ?>/library/tools/timthumb.php?src=<?php echo $src; ?>&w=270&h=198" alt="<?php get_the_title(); ?>"/></a>
<span class="gallery-entry-img-tab"></span>
</div>
<span class="gallery-index-title"><?php the_title(); ?></span>
<div class="hidden-gallery">
<?php
$pic_count = 0;
foreach ( $attachments as $id => $attachment ) {
$pic_count++;
$others_src = wp_get_attachment_url( $id );
?>
<a rel="next" href="<?php echo $others_src; ?>" title="<?php the_title(); ?>"></a>
<?php
}
?>
</div><!-- .hidden-gallery-->
</div>
<?php
$mod++;
endwhile;
?>
<div style="clear:both;"></div>
</div>
</div><!-- #contentwrap-->
</div><!-- #maincontentwrap-->
Would anybody have an idea how I can have this achieved? Some expert advise would be truly appreciated.
Use wp_get_attachment_link( $id, $size, $permalink, $icon, $text ); (see http://codex.wordpress.org/Function_Reference/wp_get_attachment_link). This outputs the link to the attachment page. So you just need to adapt your code with this function. Use value = 0 for the $permalink argument so your link brings you to the attachment page (value = 1 will bring you to the file itself).
Beware, though, that the "shadowbox" is probably triggered by event binding (clicking on the anchor) in a JavaScript, so you might have to adapt your HTML code for the links. If the selector in the script for shadowbox uses a class name or an ID to detect the click, you will probably need to change the ID or class name of the container of your link in order to be sure that your target page does not get displayed within the shadowbox.
You can also (and should, actually) use wp_deregister_script() to deregister the script that displays the shadowbox, considering you don't need it in this page.
i have this code to get some elements from wordpress
global $post;
$i=0;
$args = array( 'numberposts' => 5, 'category' =>5,'order'=>'DESC','orderby'=>'post_date','suppress_filters' => 0 );
$myposts = get_posts( $args );
$has_posts = true;
foreach( $myposts as $post ) : setup_postdata($post); ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<li>
<?php if($image){ ?>
<div class="news_left"><img src="<?php echo $image[0]; ?>" alt="" width="191" height="132" /></div>
<?php } ?>
<?php
$content = apply_filters('the_content', get_the_content());
$content = explode("</p>", $content);
?>
<div class="news_right">
<h2><?php the_title(); ?></h2>
<span class="date">Date: <?php the_time('j/m/Y') ?></span>
<?php echo $content[1] . "</p>";//echo String::content_limit(200,'<p>'); ?>
Read More
</div>
<div class="clear"></div>
</li>
<?php $i++; endforeach; ?>
i need to put a condition to return text message if category is empty.like no posts to display , please note that the post use WPML as the language switcher
<?php
$myposts = get_posts( $args );
if($myposts){
//found posts
}else{
//no posts
}
?>
UPDATE: PLEASE check if the code works probably and THEN compare it with your code, i have commented on the changes i made, so its your chance to learn:
$i=0;
$args = array( 'numberposts' => 5, 'category' =>5,'order'=>'DESC','orderby'=>'post_date','suppress_filters' => 0 );
$myposts = get_posts( $args );
//check if $myposts
if(!$myposts){
//the $myposts has no posts, print the error message
echo "<li>";
echo "This category has zero posts";
echo "</li>";
}else{
//the category has one more or more posts
$has_posts = true;
foreach( $myposts as $post ) : setup_postdata($post); ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<li>
<?php if($image){ ?>
<div class="news_left"><img src="<?php echo $image[0]; ?>" alt="" width="191" height="132" /></div>
<?php } ?>
<?php
$content = apply_filters('the_content', get_the_content());
$content = explode("</p>", $content);
?>
<div class="news_right">
<h2><?php the_title(); ?></h2>
<span class="date">Date: <?php the_time('j/m/Y') ?></span>
<?php echo $content[1] . "</p>";//echo String::content_limit(200,'<p>'); ?>
Read More
</div>
<div class="clear"></div>
</li>
<?php
$i++; endforeach;
}
?>
Thank you so much!! This solved my problem..
ref: howlingwolfmedia.com/site3/classes/class-categories
see 'Personalized Training'
my code here:
`
<?php
$args=array(
'child_of' => 4,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => '0'
);
$categories=get_categories($args);
foreach($categories as $category) {
echo '<h3>' . $category->name . '</h3>
<div>';
global $post;
$args = array( 'posts_per_page' => -1, 'category' => $category->term_id, 'orderby' => 'name', 'order' => 'ASC' );
//alternatively this also works: 'nopaging' => true
$myposts = get_posts( $args );
if (!$myposts) {
echo 'Sorry there are currently no classes in this category';
}
else {
foreach( $myposts as $post ) : setup_postdata($post);
echo '<p>' . get_the_title($post->ID) . '</p>';
endforeach;
}
echo '</div>
';
}
?>
`