I am having users of a Wordpress blog upload a single image to different post's galleries with the name "banner". Basically, every post will have an image named "banner" uploaded to its gallery and this image needs to be displayed on the post's page outside of the article content. So, how can I get the URL to display an image in a post's single.php template?
Can I iterate through the images of a given post's gallery and find the one with the correct title somehow?
I've searched through the Wordpress codex docs and haven't found anything on a way to do this, just information on displaying galleries of photos. Note that I'm already using Wordpress's post thumbnail feature for something else.
Thanks!
I think I finally figured it out. The part I couldn't figure out was using the vaguely named get_children() function.
<?php
have_posts(); //must be in the loop
the_post(); //set the ID
$images =& get_children(array('post_mime_type' => 'image', 'post_parent' => get_the_ID()));
rewind_posts(); //rewind for the actual loop
$image_url = null;
foreach($images as $image) {
if($image->post_title == 'banner') {
$image_url = $image->guid;
break;
}
}
?>
Related
I'm using RainLab.Blog plugin for my blog. Also I'm trying to create a subscription script for my site and successfully managed to get both title and slug parameters from the database,use them whenever I want to send latest posts via mail. Everything works fine, but I also want to send featured image, which can be added to the post, while it's being created :
Is there a way to include it in the following script, considering that I've already extracted both title and slug parameters from rainlab_blog_posts table. And obviously there are no links to featured images in this table :
foreach($blogPosts as $posts)
{
$text .= ''.$posts->title.'<br>';
}
You should check Post Model to find relation between posts and images
if method name is featured_images so you can get images with $posts->featured_images
But I guess featured_images is not one to one relation and each post could have more than one image
so you should decide which images you want to get to display
in your sample code:
foreach($blogPosts as $posts)
{
$text .= ''.$posts->title.'<br>';
$image .= $posts->featured_images[0]->path;
}
featured_images[0] get first image in featured_images and also path is the column name that store path of featured image to add this path in img src
also
$posts->featured_images->first()->path is another way to get an image of post
On my Wordpress blog page (home.php) template, I'm trying to display the featured image set for that page at the top, then the content archive grid underneath. However, instead of displaying the featured image I set for the page, the first blog post's featured image is displayed instead. Additionally (related), if I remove the first blog post's featured image, it uses another post image on the content archives page. I'm guessing the two problems are related, but I'm not sure. Any help would be appreciated.
Here's the code I put in my functions.php
//Add featured images
add_action( 'genesis_before_content_sidebar_wrap', 'ws_post_image', 8 );
function ws_post_image() {
global $post;
wp_reset_postdata();
echo the_post_thumbnail($post->id); /*you can use medium, large or a custom size */
}
WordPress ignores anything you set on the "Page for Posts". Instead of treating it like a page in the general sense, it treats it more like an archive of posts and the "page" is merely a URL. That said, you can access the ID of the page and then use that to get the title, featured image, or other custom fields you assign to that page:
add_action( 'genesis_before_content_sidebar_wrap', 'ws_post_image', 8 );
function ws_post_image() {
echo get_the_post_thumbnail( get_option( 'page_for_posts' ), 'large' ); // Or use whatever size you need
}
This says, "Get me the featured image on the page I set as the Blog page in Settings->Reading". The ID of that page is stored in your options table with the name page_for_posts.
I've created a custom posts called Projects and created a Projects template inside of archive-projects.php. I've also created a single-projects.php to display individual project. However, this became a problem when I had to use an ACF on the Projects template. I want to display a featured project on the Project page and also list out few of the posts I've created for projects on the same page. Below is my code to get featured project image.
<?php echo get_field('featured_project'); ?>
<?php
$featured_project = get_field('featured_project');
if ($featured_project):
$project = $featured_project;
setup_postdata( $project );
if ( has_post_thumbnail($featured_project->ID) ) { // check if the post has a Post Thumbnail assigned to it.
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($featured_project->ID), 'large' );
$featured_image = $featured_image[0];
}
?>
I've read few articles and similar questions on Stackoverflow but haven't found an answer yet. Has anyone been successful figuring this out?
You can try to use my plugin ACF CPT Options Page
That creates options page for each custom post type, then you can add Relationship Field to get posts easily
I have a Wordpress site which uses a feature image on the posts which link to a page with the post's content (images, content, etc)
I separate out the images from the content with
<?php
preg_match_all('/(<img [^>]*>)/', get_the_content(), $images);
for( $i=0; isset($images[1]) && $i < count($images[1]); $i++ ) {
echo $images[1][$i];
}
?>
I want to target the first image of each individual post (it is not the featured image) I want to put
<h1><?php the_title(); ?></h1>
overtop the first image, to make it act as a header for the post, then I can have the other images and text underneath. not sure how to accomplish this
EDIT - clarification
My index.php shows the featured image of each post in a 'thumbnail gallery' style. each picture links to content-single.php where the content and images are displayed for that post (the featured image is NOT on this page). content-single.php is where i pull out the text and the images
I want to be able to put a title overlapping the first image of the post (NOT the featured image on index.php) because I want the first image of the content to act as a header for each single post page
Try This.
preg_match_all('/<img[^>]+>/i',$get_the_content(), $result);
echo "<pre>";
print_r($result);
For Frist Image Try this $result[0][0].
It Get All Images.
Try This.
$content = apply_filters ("the_content", $post->post_content);
preg_match_all('/<img[^>]+>/i',$content, $result);
echo "<pre>";
print_r($result);
i hope this is useful for you.!
I'm trying to show a featured image in Wordpress, I've uploaded the image (put it as the smallest image possible) but when I view the blog list view (category.php) I just get the blog post header, and no image.
Anybody know how to show the image? Checked the source, and doesn't show an image at all.
Hope someone can help, would be much appreciated.
In addition to setting the featured image in WP admin, you need rendering code somewhere in your template files. Something like this:
if ( has_post_thumbnail( get_the_ID() ) ) {
echo get_the_post_thumbnail( get_the_ID() );
}
I use this code in my header.php theme file, but you may prefer it within page.php or elsewhere (category.php seems appropriate in your case).
Check the documentation for help changing the dimensions, adding class attributes, etc. to the <img/> tag generated.
http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail