Trying to include a specific page on the frontpage of a WordPress Theme.
The overal HTML structure should be similar to this:
<div>
<h1>Om</h1>
<p>Content comes here</p>
</div>
<div style="background-image: url(...)">
</div>
At the moment it echos the content of the page successfully.
Here is how that part works with PHP:
<div class="omtekst col-md-6 half-content-wrapper">
<h1 class="semibold">Om</h1>
<?php
$my_id = 7;
$post_id_7 = get_post($my_id);
$content = $post_id_7->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>
</div>
As mentioned earlier I want to include the thumbnail below this. This is where I am having problems.
However, I have started on the PHP to show how I am thinking. The thumbnail should be $image or similar.
<div class="ombilde col-md-6" style="background-image: url('<?php echo $image; ?>')">
Do you have a working solution for this?
Edit
My custom thumb which is not used with this image:
add_action( 'after_setup_theme', 'mytheme_custom_thumbnail_size' );
function mytheme_custom_thumbnail_size(){
add_image_size( 'frontpage_thumb', 500 ); // Unlimited height
}
Enabled post thumbnails:
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
add_theme_support( 'nav-menus' );
add_theme_support( 'widgets' );
}
to get the url of image you can use
$image = wp_get_attachment_image_src( get_post_thumbnail_id([ID_HERE]), [IMAGE SIZE HERE] );
then you can use
<div class="ombilde col-md-6" style="background-image: url('<?php echo $image[0]; ?>')">
to get image size string look at your theme for "add_image_size" function
and as pgk said post-thumbnails have to be enabled inside functions but i guess you already did this
edit:
[ID_HERE] replace with post id
[IMAGE SIZE HERE] you can test this with "full"
Use wp_get_attachment_url($id) for retrieving the URL of the desired post.
In your example:
$my_id = 7;
$post_id_7 = get_post($my_id);
$my_thumb = wp_get_attachment_url($my_id);
<div style="background-image: url('<?php echo $my_thumb; ?>')" class="omtekst col-md-6 half-content-wrapper">
<h1 class="semibold">Om</h1>
</div>
If you want to get post thumbnail, you can make this like that:
if ( has_post_thumbnail( $post_id_7->ID ) ) {
$image = get_the_post_thumbnail( $post_id_7->ID, $size, $args );
}
This returns image tag. You can find more info here Codex - get_the_post_thumbnail
Your theme must have ‘post-thumbnail’ support enabled.
This is the easiast way to attach image to your post.
And if you are in theLoop, you can use the_post_thumbnail() function, it directly prints thumbnail image:
<?php the_post_thumbnail( $size, $attr ); ?>
Codex - the_post_thumbnail()
If you want to print image as background, you must pit 'large' as $size.
Related
My theme generate my img portfolio with this code :
<img width="222" height="440" src="#" class="attachment-portfolio" alt="#">
I found this code :
<div class="carousel-inner">
<?php foreach($attachments as $attachment) {
$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
$image_big = wp_get_attachment_image( $attachment->ID, 'portfolio' ); ?>
<div class="item">
<?php echo $image_big ?>
</div>
<?php } ?>
I'm not good in PHP but i understand that the image size is generate from "$image_big" ?
I found also this in my function.php
add_image_size('big',9999,400);
I try to change the width and height in the add_image_size but nothing happend on the website ! It's still width="222" height="440"
I would like the image to be 100% or around 800px width (if it's impossible to get % unit). What to do ?
On my static front page I have created, I query the Wordpress Loop to only show the two most recent blog posts. For some reason, it started displaying two instances of the attached image for each post. Here is what it looks like:
I have no idea why it is outputting two of the attached images for each.
Here is the code for this specific part of front-page.php:
<div class="row">
<?php $latest = new WP_Query('showposts=2'); ?>
<?php while( $latest->have_posts() ) : $latest->the_post(); ?>
<a href='<?php echo get_permalink(); ?>'>
<div class="col-md-6 blog-wrap">
<?php $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' ); ?>
<div class="home-featured-img" style="background: url(<?php echo $src[0];?>);"></div>
<div class="excerpt-home">
<?php get_template_part( 'content', get_post_format() ); ?>
</div>
</div></a>
<?php endwhile; ?>
</div>
I have a feeling that the problem occurs when I call:
If you need any other code snippets from my custom theme, please let me know.
I only want for the post to display the image from this (which is the top picture):
<?php $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' ); ?>
<div class="home-featured-img" style="background: url(<?php echo $src[0];?>);"></div>
Any and all suggestions are greatly appreciated.
you can remove any code like that
<?php
// Post thumbnail.
twentyfifteen_post_thumbnail();
?>
Explanation...
that file display the second image - the title - the meta
you need to edit it to remove the code that display the image :) thats it.
i am calling in a function to show the post featured image and post it a background image. The dimensions are set within the div yet my client updates originals that are huge and wondering if theres a way to set the size of the image to scale them down:
<?php
$catquery = new WP_Query( 'cat=23&posts_per_page=1' );
while($catquery->have_posts()) : $catquery->the_post();
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'featured' );?>
<div onclick="window.location='<?php the_permalink(); ?>'" title="<?php the_title(); ?>">
<div class="front-page-featured-post" style="background: url(<?php echo $src[0]; ?> ); background-size: cover; background-repeat:no-repeat;">
in my functions:
add_theme_support( 'post-thumbnails' );
add_image_size('featured', 370, 240, true);
Have you tried this?
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 370, 340 );
You need to regenerate the images after you implement the new thumbnail size, just use this plugin: https://wordpress.org/plugins/regenerate-thumbnails/
to regenerate all the images and it will create new images with dimensions 370x240
Can someone help me change this theme to pull the images in the sidebar from the custom field "sidebar" of each post, instead of the featured image, like it is now. The frame and link can stay the same, I just need the image to come from the custom field instead of the featured image.
<div id="content" class="city_page">
<div id="inner_sidebar">
<?php
$mypages = get_pages( array( 'child_of' => 9, 'sort_column' => 'post_title', 'sort_order' => 'asc' ) );
foreach( $mypages as $page ) {
$content = $page->post_content;
if ( ! $content ) // Check for empty page
continue;
$content = apply_filters( 'the_content', $content );
?>
<div class="item city">
<?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>
<img class="frame" src="<?php bloginfo('url'); ?>/images/city_image_frame_thumb.png" alt="" />
<a class="title" href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a>
</div>
<?php
}
?>
</div>
As long as your are placing an image URL in the "sidebar" custom field you can replace the entire img src with the following.
<?php echo get_post_meta($post->ID, 'sidebar', true) ?>
You can learn more about the get_post_meta for thumbnail urls here: http://codex.wordpress.org/Function_Reference/get_post_meta#Retrieve_a_Custom_Field_Thumbnail_Url
Hope that helps!
Personally, I am a huge fan of the Advanced Custom Fields plugin. It is one of the first plugins I install in a new WordPress installation. It is really flexible and has a simple API for getting and setting custom fields.
Check it out!
I'm building my first WordPress Theme and I'm stuck on something.
I have a function in my functions.php called get_first_photo() which grabs the first image uploaded on each post and puts it on the blog archive page. It's working fine, but it loads the full-sized image and resizes it using CSS. I would rather it load the image at it's thumbnail size set in the WP control panel so I don't have the image-size overhead. Any way to accomplish this?
Here's the code from functions.php:
function get_first_photo($id) {
global $wpdb;
return $wpdb->get_var("SELECT guid FROM wp_posts WHERE post_parent = $id AND post_mime_type = 'image/jpeg' ORDER BY id DESC LIMIT 1");
}
And here's the blog template:
<?php
get_header(); ?>
<div id="content" class="blog">
<div id="body">
<h3 class="title" id="blog">The Ned Leary Blog</h3>
<?php if (have_posts()) :
query_posts("category_name=blog");
while (have_posts()) :
the_post();
$first_photo = get_first_photo(get_the_ID());
?>
<div class="snippet">
<?php if (!empty($first_photo)) : ?>
<img src="<?php echo $first_photo; ?>" alt="Thumbnail" />
<?php else : ?>
<img src="<?php bloginfo('url'); ?>/images/blog/gravatarBig.jpg" alt="Ned Leary Gravatar" />
<?php endif; ?>
<div class="details">
<h2><?php the_title(); ?></h2>
<h5><?php the_time('D, M j, Y') ?> by <?php the_author('') ?> | <?php comments_popup_link('0 Comments', '1 Comment', '% Comments'); ?></h5>
<?php the_excerpt(); ?>
<h4>Read more…</h4>
</div>
</div><!--end snippet-->
<?php endwhile; endif;// end of the loop. ?>
</div><!--end body-->
<?php get_sidebar(); ?>
</div><!--end content-->
<?php get_footer(); ?>
All you need is to grab the post ID for that first image you want and then run it through "get_the_post_thumbnail()".
$first_photo = post id of the first image;
echo get_the_post_thumbnail( $first_photo );
You can also pull your own custom thumbnail sizes if you like as well. As long as you are using wordpress 2.9+.
Simply add this to functions.php
add_theme_support( 'post-thumbnails' ); //enable thumbs
add_image_size( 'mycustom-preset', width you want, height you want); //custom size
Then run the same function but call your new preset...
$first_photo = post id of the first image;
echo get_the_post_thumbnail( $first_photo, 'mycustom-preset' );
http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
http://codex.wordpress.org/Function_Reference/add_image_size
Hope that helps. It doesn't seem like your having trouble obtaining the post id for the first photo so I didn't really cover how to obtain it.
You can use GD or some other image library to programmatically manipulate an image. However, it would be best to implement an operation to create a thumbnail file when the image is uploaded. You can have it dynamically generate a thumbnail every time the page is loaded, but such an operation can be relatively computationally expensive and put unneeded load on the ssystem.
Thanks to John Ford for pointing me in the right direction, I was able to figure this out.
The query in my function changed slightly, grabbing the post id instead of guid.
function get_first_photo($id) {
global $wpdb;
return $wpdb->get_var("SELECT id FROM aire_posts WHERE post_parent = $id AND post_mime_type = 'image/jpeg' ORDER BY id DESC LIMIT 1");
}
And then I had to add another line to my theme file:
$first_photo = get_first_photo(get_the_ID());
$thumb = wp_get_attachment_image_src($first_photo);
Lastly, I updated my image src:
<img src="<?php echo $thumb[0]; ?>" alt="Thumbnail" />
you could use the get_the_post_thumbnail function or use a php thumbnail generator like timbthumb
<?php $images = get_children( array( 'post_parent' => $page->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
if ( $images ) {
$total_images = count( $images );
$image = array_shift( $images );
$thumbnail = wp_get_attachment_image_src($image->ID, array(225,125) );
$thumbnail = $thumbnail[0]; }
?>
<img class="size-thumbnail alignleft" src="<?php echo $thumbnail; ?>" alt="<?php echo $page->post_title ?>">