Get featured image of a page (no post) in Wordpress - php

I need to show the featured images of all the pages, not the posts. I have this code:
<?php
if ((is_singular() || is_home()) && current_theme_supports('post-thumbnails')) : echo get_the_post_thumbnail( '12', 'full' ); ?>
<img src="<?php header_image(); ?>" class="header-img" alt="" />
<?php endif;?>
But this only shows one featured image.
Thank you so much!

You can simply use WP_Query to get that,
$loop = new WP_Query( array( 'post_type' => 'page', 'meta_key' => '_thumbnail_id' ) );
Or if you want to do by your way you need to fetch all pages first & than loop over it to get thier feature image,
$args = array(
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages($args);
foreach($pages as $page) {
echo get_the_post_thumbnail( $page->ID, 'full' );
}

Related

How to put php created content into targeted specific div?

Noob here! I'd like to run a simple post-loop on a wordpress homepage. If I write the loop in my functions.php file and then load the homepage, the result of the query is shown at the top of the mainpage.
Now I would like to see the result of the loop in a specific div. The reason for that is, that I would like to use a pagebuilder for most of the sites content first and then see the loop.
TLDR; How can I make the content created by a php function be displayed/loaded into a specific div container? Is it even possible?
I've tried creating the div and putting the shortcode for my function inside of it but wordpress wouldn't even allow me to save the changes.
in functions.php:
function show(){
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'post_date',
'order' => 'desc',
'posts_per_page' => '30',
'post_status' => 'inherit'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$image = wp_get_attachment_image_src( get_the_ID() );
echo'<div><div class="numbertext">1</div>';
echo $images = wp_get_attachment_image( $query_images->posts->ID, 'full' );
echo'<div class="title">';
the_title();
echo'</div></div>';
endwhile;
};
add_shortcode( 'imgshow', 'show' );
Then in Pagebuilder:
<div id="imgcontainer">[imgshow]<div>
So for each img the query finds, I expect the loop to create some div boxes with the images and their titles in it.
You're on the right track, but a little off on your shortcode syntax. One thing to remember with shortcodes is you always have to return content, as opposed to echoing it. I haven't tested this, but this should work.
function show(){
ob_start(); // should usually start a shortcode with an output buffer
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'post_date',
'order' => 'desc',
'posts_per_page' => '30',
'post_status' => 'inherit'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$image = wp_get_attachment_image_src( get_the_ID() ); ?>
// You don't have to echo divs you can use html tags in PHP
<div class="numbertext">1</div>
<img src="<?php echo $image; ?>" />
<div class="title">
<?php the_title(); ?>
</div>
</div>
<?php
endwhile;
// put the content in a variable creating from the output buffer
$content = ob_get_clean();
// return the content
return $content;
};
add_shortcode( 'imgshow', 'show' );

how to loop in single attachment posts in custom post type in wordpress?

please help me with this annoying issue as i'm junior in wordpress , i have a different slider in my project and i made a custom post type to make attachment posts on in to make every single post as a slider .. the problem is how to loop inside CPT to retrieve every single post when i want is to display on front-end i tried alot and searched on net about it but the only code that i found and i used it made every single post to override the first post .. this is my code
<?php
$query = new WP_Query( array(
'post_type' => 'owlgalleryslider',
'posts_per_page'=> 3,
'fields' => 'ids'
));
$image_query = new WP_Query(array(
'post_type' => 'attachment',
'post_status' => 'puplish',
'post_mime_type' => 'image',
'posts_per_page' => 3,
'post_parent' => $query->id,
'order' => 'DESC'
));
?>
<?php if( have_posts( )) : ?>
<?php while ($image_query->have_posts()) : $image_query->the_post( ); ?>
<div class="item owl-slide"> <img src="<?php echo wp_get_attachment_url( get_the_ID() ); ?>" /> </div>
<?php endwhile; wp_reset_query( );?>
<? endif;?>
If you want to create a slider, set the featured image for every slider post and use that image in your WP_Query loop.
<?php
$query = new WP_Query( array(
'post_type' => 'owlgalleryslider',
'posts_per_page'=> 3,
));
?>
<?php if( have_posts( )) :
while ($query->have_posts()) : $query->the_post();
$image = wp_get_attachment_url( get_post_thumbnail_id() );
?>
<div class="item owl-slide"> <img src="<?php echo $image; ?>" />
</div>
<?php endwhile; wp_reset_query( );
endif;
?>

How can I get the image url in WordPress?

I'm using this code to get all images uploaded to a page and it's working fine but I'd like to use the image urls for a lightbox function and currently I'm getting an img object. Is there any way I can get the image url specifically? I'd like to think it's part of an array. Here's the code I'm using:
<?php
$images = get_children( array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC',
'numberposts' => 999 ) );
if ( $images ) {
//looping through the images
foreach ( $images as $attachment_id => $attachment ) {
?>
<li>
<a href="*the_img_url*" data-lightbox="lightbox-1" data-title="<?php echo $attachment->post_excerpt; ?>" ><?php echo wp_get_attachment_image( $attachment_id, 'full' ); ?>
</a>
</li>
<?php
}
}
?>
Thank you.
Have you tried
<?php echo wp_get_attachment_url( $attachment_id ); ?>
// $attachment_id is The ID of the desired attachment

Displaying posts thumbnails in Wordpress show only first thumbnail

I want to display on the sidebar the latest posts title and thumbnail.
So far I'm getting the posts title and only one thumbnail duplicated.
You can see the result here.(only the first/oldest post image displaying)
Here is my code:
$rps = wp_get_recent_posts($params);
foreach($rps as $rp) :
$args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachment = current(get_posts( $args ));
?>
<?php echo $rp['post_title'];?><?php echo wp_get_attachment_image( $attachment->ID, 'thumbnail' );?>
<?php endforeach; ?>
Thanks for any tips/assistance given.
Replace 'post_parent' => $post->ID with 'post_parent' => $rp['ID'] . That's it.
What you are doing is, you are passing current post's ID in $args for all posts.
run 2 queries
one outputs the first post. The second outputs everything else excluding the first
<?php $args = array( 'post_type' => 'attachment', 'posts_per_page' => 1, 'post_parent' => $post->ID);
$first = new WP_Query( $args );
while ( $first->have_posts() ) : $first->the_post(); ?>
<?php the_title();?><?php echo wp_get_attachment_image( $first->ID, 'thumbnail' );?>
<?php endwhile; wp_reset_postdata(); ?>
<?php $args2 = array( 'post_type' => 'attachment', 'posts_per_page' => 1, 'post_parent' => $post->ID, 'offset' => 1);
$rest = new WP_Query( $args2 );
while ( $rest->have_posts() ) : $rest->the_post(); ?>
<?php the_title();?><?php echo wp_get_attachment_image( $rest->ID, 'thumbnail' );?>
<?php endwhile; wp_reset_postdata(); ?>

Avoiding Duplicate item in loop

I wrote a query to show related posts in Wordpress,
A brief explanation:
First it checks what categories current post belongs to,
then puts "slug" name of that categories inside an array ("$cats")
Then using foreach, I query the posts with same taxonomies.
This code works great except for when current post belongs to more than one category and there is another post which belongs to these categories too, in this situation the other post gets displayed twice or even more depending on how many categories they share,
So the question is, How do I check if a post is repeating and how to prevent it?
Code:
<?php
$terms = get_the_terms( $currentid, 'taxonomy' );
$cats = array_map(function($a){ return $a->slug; }, $terms);
foreach ($cats as $cat) {
$args=array(
'taxonomy' => $cat ,
'post_type' => 'apps',
'post_status' => 'publish',
'posts_per_page' => 4,
);
global $wp_query;
$wp_query = new WP_Query($args);
while ($wp_query->have_posts()) : $wp_query->the_post();
if($currentid !== get_the_ID()){
?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php
if ( has_post_thumbnail() ){
$appfetured = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ));
$appfeturedimage = $appfetured[0];
}else{
$appfeturedimage = '../img/defaultappicon.png';
}
?>
<img class="appfeatured" src="<?php echo $appfeturedimage; ?>" alt="<?php the_title_attribute(); ?>" />
</a>
</li>
<?php
}
endwhile;
wp_reset_query(); // Restore global post data stomped by the_post().
}//end foreach
?>
If all you are trying to do is get all of the posts in all of the categories for the current post try this:
<?php
$args = array(
'cat' => wp_get_post_categories($currentid),
'post_type' => 'apps',
'post_status' => 'publish',
'posts_per_page' => 4
);
new WP_Query($args);
It's been a while since I've used Wordpress so I don't know if there's a difference between the categories and the taxonomies so you can try this:
<?php
terms = get_the_terms( $currentid, 'taxonomy' );
$cats = array_map(function($a){ return $a->slug; }, $terms);
$args = array(
'taxonomy' => $cats,
'post_type' => 'apps',
'post_status' => 'publish',
'posts_per_page' => 4,
);
global $wp_query;
$wp_query = new WP_Query($args);

Categories