0
votes

I've tried to figure out how to display an image in different posts. When I create a post in WordPress it displays a featured image on each post. I have a custom DB with 1000 images and I want to display each image from my DB on each post. I would have 1000 posts, but each with its respective image from the DB, but I can't find where those php files are.

I've already done this in local host. I got a .php file which displays all these images, but I don't know if its just an issue of .php file or I have to code the functions in WordPress.

1
It sounds like you want to automate the process of setting 1000 featured images on posts? You could code something custom to do this but it is a non-trivial task. You would need to figure out which images go with which posts and associate their post ID with each image in your custom DB, and then write some code to loop through these values and add each image to the media library as well as insert/update each post's featured image in your Wordpress database ... You would have to research what these records look like in WP to dupe the format. Have you looked at the Quick Featured Images plugin? - Typel
Yea , i already know how to do it with that plugin , but i developed another platform , so i add elements and these elements go to the custom DB , andwith a php file i display all these elements , but i wanna add elements to my platform and at the same time it upgrades the posts to 1001 in wordpress , im trying to modify the posts but with code , no with plugins :D - Joshua
It's a little difficult to understand what you're talking about exactly without something to look at. Do you have any examples or code you can share? SO is more about solving specific coding issues rather than concepts. If I'm understanding correctly, it sounds like you will need to modify your custom platform to integrate with the wordpress side. If they are on the same server it's just a matter of having it save the image to multiple locations and updating two databases instead of one. If not on the same server, you will probably have to utilize Wordpress's REST api features. - Typel
what i want to do is , with code lets suppose i have a DB with 3 images , x,y,z , so i wanna make 3 posts but instead the posts display the featured image in the post , i want that the post 1 displays the image X , the post 2 displays the image Y , the post 3 displays the image Z , and these X,Y,Z, images are on my custom DB , of course these have a specific ID , but i don't find what should i modify from the wordpress files or what files :) - Joshua
External links are temporary, but this one seems especially relevant to what you're looking for: developer.wordpress.org/themes/functionality/… - you may be able to add some customization to your theme's functions.php file in WordPress using the 'post_thumbnail_html' hook that would just override the default functionality altogether. Then you could have it do whatever you want - such as looking up and pulling the image from somewhere other than the media library - Typel

1 Answers

0
votes

How about adding customization to your theme's functions.php file in WordPress that reroutes WordPress's default behavior for determining each featured image?

Note: Ideally these types of mods should be added to a child theme rather than the main theme so that updates to your theme do not overwrite your customization.

Here is an example of how this might work:

Added to /wp-content/themes/your-child-theme/functions.php

add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
function my_post_image_html($html, $post_id, $post_image_id) {
    // run a query to find the location of your preferred 
    // image in the alt location. $img is just an example.
    $img = "/other_images/otherThumbnail-".$post_id.".jpg";
    return '<img src="'.$img.'" alt="modified">';
}