Wordpress Custom Post Thumbnail Not Showing On Homepage - wordpress

This is NOT a question about the featured image meta field. I've tried to find an answer but every search I've tried shows people trying to add the 'featured image' meta to their custom post type. I have that enabled for my theme and my custom-post type. It seems to work just fine. I've set a 'featured image' and I see it when I edit the post. My theme is a custom child of twentyeleven.
On my homepage I'm displaying recent entries (both 'post' type and 'custom-post' type) with the title, an excerpt, and a thumbnail to the left. The thumbnail is displaying properly for all my 'post' types, but not for my 'custom-post' type. I'm not sure where I need to look or what I need to add to get custom-post to show the thumbnail.
Adding Code:
This is in the 'content.php' from my custom theme. It's mostly appropriated from twentyeleven but I think I've made some minor changes. As far as I can tell the 'if' isn't proving true for the custom post type.
$thumbnails = get_posts('numberposts=5');
foreach ($thumbnails as $thumbnail) {
if ( has_post_thumbnail($thumbnail->ID) && $thumbnail->ID == $id) {
echo 'ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">'; echo get_the_post_thumbnail( $id, 'thumbnail', array('class' => 'alignleft') );
echo '';
}
}
Update:
The problem looks like it's related to
$thumbnails = get_posts('numberposts=5');
It's only pulling from the 'post' type so it can't check against my 'custom-post' type. I get the correct thumbnail when I change the code as follows, but then none of the 'post' thumbnails work.
$args = array(
'numberposts' => 5,
'post_type' => 'pnw_picture-post');
$thumbnails = get_posts($args);
So I guess the solution is to pull both of those types in a single query.

Can you post the code that you are using that you think should be returning the post thumbnail?
You may just be looking for the_post_thumbnail from the WP Codex

I took a closer look at the twentyeleven content.php - It looks like whatever I put in my child content.php is completely different. I hadn't realized I had changed so much of the code.
This is the modified working code to pull in both post types:
$args = array(
'numberposts' => 5,
'post_type' => array ( 'post', 'custom-post'));
$thumbnails = get_posts($args);
foreach ($thumbnails as $thumbnail) {
if ( has_post_thumbnail($thumbnail->ID) && $thumbnail->ID == $id) {
echo 'ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">'; echo get_the_post_thumbnail( $id, 'thumbnail', array('class' => 'alignleft') );
echo '';
}
}

Related

How to create a filter to display custom post types

I have a custom post type 'projects' and have an overview page that displays these post types with featured image & post title. I have also created a custom taxonomy for that Post Type and assigned the posts to the categories from that taxonomy.
What I want to achieve now is that on the Overview Page where all the posts are listed, above them should be something like a filter bar with the custom taxonomy categories displayed.
My question now is: What WordPress functions do I need so that when someone clicks on one of the categories, only the posts assigned to that category will be displayed? I don't want the page to refresh or load another page. Here is an example of what I want to achieve: https://www.hauserlacour.de/en/work
Also, I am not a coder. I use Pinegrow to convert my static html sites to a wordpress theme. but in Pinegrow I have the option of a lot of WP function. That's why I just need to understand how the set up of something like above would work.
Many thanks in advance!
If you know a bit more about the the WP_Query you can use the tax_query as below:
$args = array(
'post_type' => 'project',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => array( 'project_cat' ), // <-- NO! Does not work.
'field' => 'slug',
'terms' => array( 'project_cat1', 'project_cat2')
)
)
);
$query = new WP_Query( $args );
Reference: https://developer.wordpress.org/reference/classes/wp_tax_query/
or you can simply list the taxonomy terms and simply redirect to taxonomy detail page where respective projects will be listed automatically.
$args = array( 'hide_empty=0' );
$terms = get_terms( 'my_term', $args );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
$count = count( $terms );
$i = 0;
$term_list = '<p class="my_term-archive">';
foreach ( $terms as $term ) {
$i++;
$term_list .= '' . $term->name . '';
if ( $count != $i ) {
$term_list .= ' ยท ';
}
else {
$term_list .= '</p>';
}
}
echo $term_list;
}
Reference: https://developer.wordpress.org/reference/functions/get_terms/

How to fetch all images from particular page id in wordpress

I have created a page called "Gallery" and created new gallery with 10 images. My page id is 129. I have 10 images in that page id(129).
Now my question is, i need a code to fetch those 10 images in wordpress. please anyone help me with a code. Thanks in advance.
Get all images from post.
function get_all_images() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
return $first_img;
}
Here you get frist image such like you get all other images
Use get_children
I used this code to extract all the images from a page gallery in the chosen order. you can include this code in the loop or use it stand alone. just choose the appropriate post_parent code (see bellow the code example).
This example show all images associated to the page id 129, have a look:
$images = get_children( array( 'post_parent' => 129, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
$images is now a object that contains all images (related to post id 1) and their information ordered like the gallery interface.
if ( $images ) {
//looping through the images
foreach ( $images as $attachment_id => $attachment ) {
?>
<?php /* Outputs the image like this: <img src="" alt="" title="" width="" height="" /> */ ?>
<?php echo wp_get_attachment_image( $attachment_id, 'full' ); ?>
This is the Caption:<br/>
<?php echo $attachment->post_excerpt; ?>
This is the Description:<br/>
<?php echo $attachment->post_content; ?>
<?php
}
}
Find the post id you wan to extract images from and insert it into this argument: 'post_parent' => 129
you can also use:
'post_parent' => $post->ID
If you want to use get_children in a loop, and get the post id from the returned post id.
If you want to exclude the image selected as a featured image i would have a if statement check if the image URL is equal to the featured image URL.

Display the content and title of a specific page on Homepage

I want the title and content of a specific page to show in my Homepage and footer area.
It will be something like this.
This is the Title of the page
This is the content of the page, etc....blahh blah..
readmore...
I know that there is no excerpt in the page post. But i want to limit the characters of the content that will show in the footer area.
Can you give me a code/link/tutorial that will show me how to do this?
Thanks,
First there is excerpt avaIlable in the page posts. Just set it in the Screen Options.
About the code try this one which I use on my projects.
It will show excerpt of those pages, which have custom field named showOnHomePage and the value of this field determines the order in the list.
$pages=get_posts( array( 'meta_key' => 'showOnHomePage', 'post_type' => 'page', 'orderby' => 'meta_value', 'order' => 'ASC'));
foreach( $pages as $page )
{
echo '<h2 class="entry-title"><a href="'.get_permalink( $page->ID ).'" >'.get_the_title( $page->ID ).'</a></h2>';
echo $page->post_excerpt;
echo '<a class="read-more" href="'. get_permalink( $page->ID ) . '">Read More</a>';
}

Show A Custom Field from Similar Category (Getting A Custom Field from Similar Posts)

I have a single.php where I display a Product. I have Category and Sub Categories for this product and I want to display a the bottom a custom field image from similar categories.
For Example I'm in Red Bag 1, I want to put in the bottom a thumbnail of similar pages like Red Bag 2 and Red Bag 3 which are in the same category "Red Bag"
I actually have a code already but I don't know how to target the custom field created by Advance Custom Field plugin: the_field("product_image")
here's my code:
<?php
global $post;
$cat_ID=array();
$categories = get_the_category(); //get all categories for this post
foreach($categories as $category) {
array_push($cat_ID,$category->cat_ID);
}
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'numberposts' => 8,
'post__not_in' => array($post->ID),
'category__in' => $cat_ID
); // post__not_in will exclude the post we are displaying
$cat_posts = get_posts($args);
$out='';
foreach($cat_posts as $cat_post) {
$out .= '<li>';
$out .= ''.wptexturize($cat_post->post_title).'</li>';
}
$out = '<ul class="cat_post">' . $out . '</ul>';
echo $out;
?>
where .wptexturize($cat_post->post_title). suppose to be an image instead of the title. The image source should be taken from the custom field the_field("product_image")
If you would use post_thumbnail_html it would be a lot simpeler. Instead you use a a function which is not default to wordpress: the_field.
I don't know which plugin does this.
Probably that function (the_field) would work within a normal loop.
To do that you should change get_posts($args) to WP_Query($args) and replace the foreach($cat_posts.. with a loop.
I advise to use the default wordpress thumbnail/featured image with post_thumbnail_html

How do i show wordpress attachments from current post?

So with my blog i have a photo attachment page but it only shows to photo's at a time, and those two photo's are used as the navigation and i hate that.
I want the attachment page to show all the photo's that goes along with the rest of that set.
Here is the current code
<div id="nav-images" class="navigation clearfix">
<div class="nav-next"><?php next_image_link() ?></div>
<div class="nav-previous"><?php previous_image_link() ?></div>
How do i change that to show all the post attachments?
To clarify, this doesn't work anymore - at least with version 3.5.2. I used this instead;
$attachments = get_children(
array(
'post_type' => 'attachment',
'post_parent' => get_the_ID()
)
);
foreach ($attachments as $attachment) {
// ...
}
Only resurrecting an old thread because this one ranks quite highly for this search term.
When you're on a page or post, you can get all of its attachments with the following:
global $post; // refers to the post or parent being displayed
$attachements = query_posts(
array(
'post_type' => 'attachment', // only get "attachment" type posts
'post_parent' => $post->ID, // only get attachments for current post/page
'posts_per_page' => -1 // get all attachments
)
);
foreach($attachements as $attachment){
// Do something exceedingly fancy
}
Since you're currently on an attachment page, you can get all the other attachments using the $post->post_parent value:
global $post; // refers to the attachement object
$attachements = query_posts(
array (
'post_type' => 'attachment', // only get "attachment" type posts
'post_parent' => $post->post_parent, // attachments on the same page or post
'posts_per_page' => -1 // get all attachments
)
);
To then display the attachment images, you can use the wp_get_attachment_image_src function. The attachment's ID will be available in each iteration of your foreach loop as $attachement->ID (if you use the same naming convention as my first example).
Since WordPress 3.6.0 you can also use get_attached_media.
$media = get_attached_media( 'image', $post->ID );
if(! empty($media)){
foreach($media as $media_id => $media_file){
$thumbnail = wp_get_attachment_image_src ( $media_id, 'thumbnail' );
$full = wp_get_attachment_url( $media_id );
echo '<img src="'.$thumbnail[0].'" alt="'.$media_file->post_title.'" />';
}
}

Resources