Can't change default thumbnail size - php

I'm making gallery in wordpress and I want to change default thumbnail size but it doesn't work properly. When I set photo as post thumbnail natural size is 150x150 in wordpress media options I changed thumbnail size to 215 143.
in functions I have
add_theme_support( 'post-thumbnails' );
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 1240, 1240 );
}
My gallery query:
<ul id="stage">
<?php
// The Query
$the_query = new WP_Query( array( 'post_type' => 'flota', 'orderby' => 'title', 'order' => 'ASC' ) );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id ( $the_query->ID ), 'thumbnail' );
$full = wp_get_attachment_image_src( get_post_thumbnail_id ( $the_query->ID ), 'full' );
$cats = wp_get_object_terms( $post->ID, 'flota_category' );
$items = array();
foreach ( $cats as $cat ){
$slug = $cat->slug;
$items[] = $slug;
}
$counter = count($cats);
$i = 0;
?>
<li data-tags="<?php
foreach ( $items as $tag ){
if (++$i === $counter ){
$tags = $tag;
}
else{
$tags = $tag . ', ';
}
echo $tags;
}
?>"><img src="<?php echo $thumbnail[0] ?>" width="215" height="143" style="background:#ffffff;"></li>
<?php
endwhile;
?>
</ul>

In functions.php use:
add_theme_support( 'post-thumbnails' );
add_image_size( 'gallery-thumb', 215, 143 );
Your default posts per page will be 10 which is why you can only see 10 images. Override this by changing your query to:
$the_query = new WP_Query( array( 'post_type' => 'flota', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => -1, ) );
In your query replace $thumbnail and $full with:
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id(), 'gallery-thumb' );
$full = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
Then use a plugin like http://wordpress.org/plugins/regenerate-thumbnails/ to regenerate your thumbnails.
In a loop get_post_thumbnail_id doesn't require the ID to be set but if you do decide to set it elsewhere you were looking for $post->ID rather than $the_query->ID. Set $global $post; as well when using it.

Related

How to return the SRC attribute of my secondary image via php

I'm using the following code in wordpress functions.php file in order to retrieve my secondary image (the non-featured one).
function haruki($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) :
if ( $image->ID != $thumbnail_ID ) :
$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 $img_url;
endif; endforeach; endif; }
Then when I want to output the html for the image I do:
<?php haruki("$post->ID"); ?>
Now I would like to just return the src attribute, but I'm failing by doing this:
$myvariable = haruki( $post_id, false );
How am I supposed to fix this last one in order to make it work correctly?

Get wordpress attachments ID's separated by commas of current post

i have created custom post type named "Person" and i need to get all attachments ID's separated by commas of current post in loop.
Here i have a code that loops the "person" post type posts and code that shows every posts attachments ID's, but thing is that that those ID returns like this "2713271227112710" i need it to return seperated by commas like this "2713,2712,2711,2710"
Here is my code:
<?php
$args = array(
'post_type' => 'person',
'post_status' => 'publish',
'posts_per_page' => 24,
'order' => 'ASC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="col-md-4">
<div class="person">
<?php if ( $post->post_type == 'person' && $post->post_status == 'publish' ) {
$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
) ); ?>
<?php
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$thumbimg = wp_get_attachment_url( $attachment->ID, 'full', false );
$attachment_id = attachment_url_to_postid( $thumbimg );
echo $attachment_id; //here outputs the attachemnts ID's of current post
}
}
}
?>
<?php echo do_shortcode( '[vc_images_carousel images="'.$attachment_id.'" img_size="large" speed="3000" autoplay="yes" hide_pagination_control="yes" hide_prev_next_buttons="yes" wrap="yes"]' ); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
Use implode in combination with an array_map, to return an array of IDs, and then convert it to a string of IDs, separated by a comma.
So instead of this:
foreach ( $attachments as $attachment ) {
$thumbimg = wp_get_attachment_url( $attachment->ID, 'full', false );
$attachment_id = attachment_url_to_postid( $thumbimg );
echo $attachment_id; //here outputs the attachemnts ID's of current post
}
Try this:
$attachment_ids = implode(',', array_map(function($attachment){
return $attachment->ID;
}, $attachments));
echo $attachment_ids;

Finding attachment page URL of/from an image url

I'm using the below code to get all image URL's attached to a post.
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];
endforeach; endif; }
The output I get is something like this:
https://www.example.com/wp-content/uploads/2019/01/image.gif
What I need to find is the attachment page URL for this image which will be something like this https://www.example.com/post-name/image-22
I tried using wp_get_attachment_image, but the output wasn't what I needed.
any idea how can i do that?
You are looking for get_attachment_link to return a pretty link you need to make sure your permalink structure is set to pretty links.
https://codex.wordpress.org/Function_Reference/get_attachment_link
Example from page:
<?php
$attachment_id = 1; // ID of attachment
$attachment_page = get_attachment_link( $attachment_id );
?>
<?php echo get_the_title( $attachment_id ); ?>

Wordpress: Display image attachments from child pages of a specific page

I am trying to display all image attachments on the child pages of a specific parent page, i.e. all the pictures on Pages 10, 11 and 12.
Projects (Page ID: 5)
- Project 1 (Page ID: 10)
- Project 2 (Page ID: 11)
- Project 3 (Page ID: 12)
This is what I have so far, and it works to display all images on the site:
<?php
$args = array(
'post_parent' => 0,
'post_type' => 'attachment',
'numberposts' => -1
);
$images = get_children( $args );
if ( empty($images) ) {
// no attachments here
} else {
foreach ( $images as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id, 'full' );
}
}
?>
However, if I add the post parent ID (5), nothing comes up:
<?php
$args = array(
'post_parent' => 5,
'post_type' => 'attachment',
'numberposts' => -1
);
$images = get_children( $args );
if ( empty($images) ) {
// no attachments here
} else {
foreach ( $images as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id, 'full' );
}
}
?>
Any suggestions would be really helpful!
I think get_children() return objects from a single parent post or all (passing 0 as value).
You may try a nested foreach to get all post children first, then query attachment page by page. Here is an untested sample, but it gives you an idea:
<?php
$subpages_args = array(
'post_parent' => 5,
'post_type' => 'page',
'numberposts' => -1
);
$sub_pages = get_children( $subpages_args );
foreach( $sub_pages as $subpage_id => $sub_page) {
$args = array(
'post_parent' => subpage_id,
'post_type' => 'attachment',
'numberposts' => -1
);
$images = get_children( $args );
if ( empty($images) ) {
// no attachments here
} else {
foreach ( $images as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id, 'full' );
}
}
}
?>
Good luck :)
Here is an SQL Query based solution. Copy following function in your theme's functions.php
function get_children_page_attachments($parent_id) {
global $wpdb;
// just a precautionary typecast and check
$parent_id = intval( $parent_id );
if( empty($parent_id) ) return [];
$query = "SELECT ID FROM {$wpdb->posts} P WHERE post_type='attachment' AND P.post_parent IN (SELECT ID FROM {$wpdb->posts} WHERE post_parent={$parent_id} AND post_type='page' )";
return $wpdb->get_results($query, 'ARRAY_A');
}
Once you have above function in your functions.php, then you can use it like this:
// for example the parent page id is 16
$image_ids = get_children_page_attachments(16);
foreach($image_ids as $image_id) {
$image = wp_get_attachment_image($image_id['ID'], 'full');
}

Display randomly picked image from Wordpress's page?

I would like to display one image at a time on my page, that is picked randomly from the images loaded in this page (from wordpress admin).
I am working with my own theme and I have a "front-page.php" file where I have my function to load the content of my page.
I don't know how to add the random image function to this code.
my PHP code:
<?php get_header(); ?>
<div class="container">
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
$images = [];
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$image_url = ( !empty( wp_get_attachment_url( $attachment->ID ) ) ) ? wp_get_attachment_url( $attachment->ID ) : '';
$image_alt = ( !empty( get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true) ) ) ? get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true) : '';
$images[] = array(
'url' => $image_url,
'alt' => $image_alt
);
}
}
$image = $images[array_rand( $images, 1 )];
?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php
}
}
?>
</div>
Create an array of available images and then choose one at random.
$images = array(
'http://via.placeholder.com/350x150',
'http://via.placeholder.com/350x150',
'http://via.placeholder.com/350x150',
'http://via.placeholder.com/350x150',
);
$image_url = $images[array_rand( $images, 1 )];
echo $image_url;
If you want to include alt text, use a multidimensional array:
$images = array(
array( 'url' => 'http://via.placeholder.com/350x150', 'alt' => 'My image'),
array( 'url' => 'http://via.placeholder.com/350x150', 'alt' => 'My image'),
array( 'url' => 'http://via.placeholder.com/350x150', 'alt' => 'My image'),
array( 'url' => 'http://via.placeholder.com/350x150', 'alt' => 'My image'),
);
$image = $images[array_rand( $images, 1 )];
echo $image['url'];
echo $image['alt'];
From your response it sounds like you're trying to pull the images from the post content, if so, you'll need to loop through the post's attachments to create the images array. However, there are a number of potential pitfalls with this method. If I were you, I would create a gallery field using Advanced Custom Fields and use that as the source of the image group.
<?php
if ( have_posts() ) {
$images = [];
while ( have_posts() ) {
the_post();
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
// get url from Plugin: WP Gallery Custom Links
$image_href = ( !empty( get_post_meta( $attachment->ID, '_gallery_link_url', true) ) ) ? get_post_meta( $attachment->ID, '_gallery_link_url', true) : '';
// get attachment
$image_url = ( !empty( wp_get_attachment_url( $attachment->ID ) ) ) ? wp_get_attachment_url( $attachment->ID ) : '';
$image_alt = ( !empty( get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true) ) ) ? get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true) : '';
$images[] = array(
'url' => $image_url,
'alt' => $image_alt,
'href' => $image_href // url from WP Gallery Custom Links
);
}
}
}
$image = $images[array_rand( $images, 1 )];
?>
<a href="<?php echo $image['href']; ?>" title="<?php echo $image['alt']; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
</a>
<?php
}
?>

Categories