Wrong image displayed, despite the correct link - css

Here is the very weird issue I'm dealing with at the moment: I am trying to implement a lightbox effect in my wordpress theme. Following this tutorial here (the CSS code used is this one: link). I tried it as it is, it works very well.
But I'm not looking for applying the effect to the featured image but to all images in a post. I have changed the code to match my need, it works if I have only one picture in the post but if I add another one (or more) that's when the problem starts:
when I click on a picture, no matter which one, it's only the first one that get displayed in the large version. But when I check the source code, the relevant picture is called for the large version of each picture.
Here's the bit of code I'am using for this lightbox effect:
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'post_mime_type'=> 'image',
'orderby' => 'ID',
'order' => 'ASC',
);
$attachments = get_posts( $args );
?>
<div class="nine columns">
<div class="about">
<h2><?php the_title(); ?></h2>
<?php foreach ( $attachments as $attachment ) :
$attachments = get_posts( $args );
$small_image = wp_get_attachment_image_src($attachment->ID, 'thumbnail');
$large_image = wp_get_attachment_image_src($attachment->ID, 'full');
?>
<img src="<?php echo $small_image[0]; >" alt="small">
<img src="<?php echo $large_image[0]; ?>" alt="large">
<?php endforeach ?>
</div>
</div>
Thanks in advance for looking into my request
With kind regards
Wanderer

Your code has three problems:
1. should not call $attachments = get_posts( $args ); in foreach
2. the href of the small image link is incorrect
3. the id of large image link is incorrect
Try this:
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'post_mime_type'=> 'image',
'orderby' => 'ID',
'order' => 'ASC',
);
$attachments = get_posts( $args );
?>
<div class="nine columns">
<div class="about">
<h2><?php the_title(); ?></h2>
<?php foreach ( $attachments as $attachment ) :
$small_image = wp_get_attachment_image_src($attachment->ID, 'thumbnail');
$large_image = wp_get_attachment_image_src($attachment->ID, 'full');
?>
<img src="<?php echo $small_image[0]; ?>" alt="small">
<img src="<?php echo $large_image[0]; ?>" alt="large">
<?php endforeach ?>
</div>
</div>

Related

custom Post content in WordPress

As you know, the following code publishes the latest content in a group in its WordPress
<?php
$my_query = new WP_Query('showposts=1&cat=172');
while ($my_query->have_posts()):
$my_query->the_post();
$do_not_duplicate = $post->ID;
?>
I want to make changes to this code، So that the last article in a group is not displayed and
, And the pre-final content of the same group will be displayed.
thank u
I think you might want to take a look at the offset and order parameters.
I'm guessing that the snapshot in your question is what you call a "group"...
Depending on what you call the last article (last published or first published) you might want to change the order from 'DESC' to 'ASC'.
<?php $query = new wp_query( array( 'post_type' => 'post', 'cat' => '172', 'post_status' => 'publish', 'posts_per_page' => '3', 'offset' => '1', 'order' => 'DESC' ) ); ?>
<?php if ( $query->have_posts() ): while ( $query->have_posts() ): $query->the_post(); ?>
<a aria-label="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endwhile; endif; ?>
gl.
EDIT: Just an idea... You could also simply go with pure CSS and targeting your last post with :last-child ... see https://developer.mozilla.org/fr/docs/Web/CSS/:last-child

Plugin Post 2 Posts: How to show the latest connected child post from parent post in loop?

I want to list latest child connected post ordered by parent post. Is it possible? Could anyone help me with that? I explain with an example: let's say I have Dramas custom post type and an Episode custom post type, that are connected to Posts 2 Posts. In the Drama archive, I want to list all the dramas by their latest episode and want to show only one episode in the loop.
I think it is maybe possible with each_connected(), but I don't know how to order the $wp_query array to get this done.
Thanks.
I am using with such kind of code now
<?php
global $post;
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$anime_series = array(
'post_type' => 'drama',
'posts_per_page' => 12,
'paged' => $paged,
'page' => $paged,
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'DESC'
);
$the_query = new WP_Query( $anime_series);
$extra = array(
'posts_per_page' => 1,
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'DESC'
);
p2p_type( 'drama_to_episode' )->each_connected( $the_query, $extra, 'episode' );
?>
</div>
<div class="last_episodes loaddub">
<ul class="items">
<?php if ( $the_query->have_posts() ) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<li>
<div class="img"> <img alt="<?php echo the_title(); ?>" itemprop="photo" src="<?php echo get_the_post_thumbnail_url(); ?>">
<div class="type"><img src="<?php echo get_stylesheet_directory_uri(); ?>/images/SUB.png"></div>
</div>
<?php
foreach ( $post->episode as $post ) : setup_postdata( $post ); ?>
<p class="name"> <?php echo get_the_title($items['parent']); ?> </p>
<p class="episode"> <?php echo the_field('short_name'); ?> </p>
<?php
endforeach;
?>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p>
<?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?>
</p>
<?php endif; ?>
</ul>
</div>
Try this easy example
?php
$args = array(
'post_parent' => 38,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'any'
);
$children = get_children( $args );
foreach($children as $post):
echo $post->post_title;
endforeach;
?>
Hope that work it in your requirement!!!
Luckily, I solved my problem myself. My primary concern was to get the latest connected child post with its parent. Before in default loop, all the episodes (old/new) were being retrieved. I made a custom select query for Posts to Posts plugin. This query returned the IDs of the latest connected post. The working code is given below. Kindly, correct me if I am using the wrong programming standards.
global $post, $items, $wpdb;
$table_name = $wpdb->prefix . "p2p";
$query = 'SELECT p2p_from as anime, max(p2p_to) as episode FROM '.$table_name;
$results = $wpdb->get_results( $query.' GROUP BY p2p_from ORDER BY episode DESC' );
Then, in foreach loop, I retreived the post title, permalink etc by the ID.

showing wordpress featured image in slider

I want show featured image in my slider. so what i've done in my function.php is like below:
<?php
function revconcept_get_images($post_id) {
global $post;
$thumbnail_ID = get_post_thumbnail_id();
$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_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); //alt
if ($img_alt == '') : $img_alt = $image->post_title; endif;
$big_array = image_downsize( $image->ID, 'large' );
$img_url = $big_array[0];
echo '<li>';
echo '<img src="';
echo $img_url;
echo '" alt="';
echo $img_alt;
echo '" />';
echo '</li><!--end slide-->';
endforeach; endif; }
?>
<div class="flexslider"> <!-- function called in index.php -->
<ul class="slides">
<?php revconcept_get_images("$post->ID"); ?>
</ul>
</div><!--end flexslider-->
Now i have two problem. no.1 My featured image is not showing in the slider. It's showing a broken link. but it can echo the post_id. no.2 I want show only 3/4 images from the latest post of cat=5. can anyone help me to do this ? note that i am using flexslider for my slider.
Ok, this is what I've tested, you may want to refine the query a bit, but for me it worked. In functions.php put
function revconcept_get_images($post_id) {
$images = get_posts( array(
'post_type' => 'attachment',
'hide_empty' => true,
'order' => 'ASC',
'orderby' => 'menu_order ID'
));
foreach ($images as $image) {
$image_url = $image->guid;
$img_alt = get_post_meta($image->ID, '_wp_attachment_image_alt', true); //alt
echo '<li><img src="'.$image_url.'" alt="'.$img_alt.'" /></li>';
}
}
And in index.php put (where you want)
<div class="flexslider"> <!-- function called in index.php -->
<ul class="slides">
<?php revconcept_get_images($post->ID); ?>
</ul>
</div><!--end flexslider-->
If you want to show all featured images from posts you have just put 'posts_per_page' => -1, in the get_posts() array.

wordpress query to get all images from a post type randomly

i have the following query to get all images from the "myportfoliotype" post type which works fine.
However i would like all images pulled in to be random when the page is loaded / refreshed.
I followed a few tutorials, and came up with the following code:
<?php
$query = new WP_Query( array( 'post_type' => 'myportfoliotype', 'posts_per_page' => -1, 'orderby' => 'rand' ) );
if( $query->have_posts() ){
while($query->have_posts()){
$query->the_post();
$do_not_duplicate = $post->ID;
$thumb_id = get_post_thumbnail_id( $post_id );
$image_query = new WP_Query( array('post__not_in' => array (MultiPostThumbnails::has_post_thumbnail('myportfoliotype', 'logo'), $thumb_id ), 'orderby' => 'rand' , 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => -1, 'post_parent' => get_the_ID() ) );
while( $image_query->have_posts() ) {
$image_query->the_post();
$do_not_duplicate = $post->ID;
//echo wp_get_attachment_image( get_the_ID() );
$image = wp_get_attachment_image_src(get_the_ID(), 'large');?>
<li> <a class="fancybox" rel="gallery1" href="<?php echo $image[0]; ?>"> <img src="<?php echo get_template_directory_uri(); ?>/js/timthumb.php?src=<?php echo $image[0]; ?>&w=137&h=137&f=2" alt="<?php the_title(); ?>" class="grey"/></a>
<img src="<?php echo get_template_directory_uri(); ?>/js/timthumb.php?src=<?php echo $image[0]; ?>&w=200&h=200" alt="<?php the_title(); ?>" class="color"/>
</li>
<?php
}
}
}
?>
I'm not quite sure if this is right? As mentioned before it pull in the images but not randomly....
Any help or guidance would be greatly appreciated.
Cheers, Dan
You probably have a plugin that hooks on posts_orderby filter.
If you don't want to disable this plugin, you can add 'suppress_filters'=>true to your WP_Query params.
I have solved my problem, and though i would post the answer incase anyone else was having the same problem using the "post type order" plugin.
In the setting page for "post type order" there is an option called "auto sort" you need to uncheck this this, and when making a query you should add the following:
$args = array(
'post_type' => 'post-type-here',
'orderby' => 'menu_order',
'order' => 'ASC'
);
As simple as that!
Thanks for all the help guys!

Wordpress: - Pull All attachment images including the Featured image

I'm having real difficulty pulling all atttachments including the image... The thing is, I need to be able to control how the content is displayed...
e.g.
<a href="(Path-to-full-image)" rel="customrel" class="aclasstodeine">
<img src="thethumbnailsize(defined in functions.php)" alt=""
class="aclasstodefine" />
</a>
REALLY hope someone can help
To get all attachment images you'll need to do a query.
$args = array(
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_status' => 'inherit',
'numberposts' => -1
);
$images = get_posts($args);
This will load any image attached to the post into the $images variable then do a foreach loop on them to get your images:
<?php foreach ($images as $i) : ?>
<a href="<?php wp_get_attachment_image_src($i->ID, 'full'); ?>" rel="customrel" class="aclasstodeine">
<img src="<?php wp_get_attachment_image_src($i->ID, 'thumbnail'); ?>" alt="" class="aclasstodefine" />
</a>
<?php endforeach; ?>
You can read up on wp_get_attachment_image_src here: http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src
More likely it would look like this:
$args = array(
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_status' => 'inherit',
'numberposts' => 1//-1
);
$images = get_posts($args);
foreach ($images as $i) :
$poza_thumb = wp_get_attachment_image_src($i->ID, 'thumbnail');
//$poza_fullsize = wp_get_attachment_image_src($i->ID, 'full');
//print_r($poza_thumb);//you will see that $poza_thumb is an array containing url, width, height
?>
<img src="<?php echo $poza_thumb[0];?>" width="<?php echo $poza_thumb[1];?>" height="<?php echo $poza_thumb[2];?>" rel="customrel" class="aclasstodeine"/>
<?php
endforeach;

Resources