WP-Foundation (by 320press) Orbit Slider with background-image - php

The thing I want to do here, is adding the post-thumbnails of the blogposts as background-image into the built in Orbit slider within 320press WP-Foundation Wordpress-theme.
First thing said, is that I don't know how to code PHP. I just wanted to do this little tweak to make the design look nicer.
My code:
<?php
global $post;
$tmp_post = $post;
$args = array( 'numberposts' => 5 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
$post_thumbnail_id = get_post_thumbnail_id();
$featured_src = wp_get_attachment_image_src( $post_thumbnail_id, 'wpf-home-featured');
?>
<div style="background-color: #F2F2F2; background-size:cover; background-image: url(<?php echo wp_get_attachment_image_src($post_thumbnail_id); ?>);">
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
<p>Read More ยป</p>
</div>
<?php endforeach; ?>
That's the whole code, now onto the part I have problems with:
`<div style="background-color: #F2F2F2; background-size:cover; background-image: url(<?php echo wp_get_attachment_image_src($post_thumbnail_id); ?>);">`
As you can see, I try to get the image source and make it the background-image.
I have no clue how to solve this problem, so, in advance, thanks for your help!

The documentation for wp_get_attachment_image_src reveals the problem. This function returns an array, containing the url, width, and height of the image. Also, the second argument it accepts is the output size of the image. Since you are using it with background-size:cover, I presume you want the image to be full resolution.
In order to output the proper background-url instead array(), change your echo in the background div to $featured_src[0]:
<div style="background-color: #F2F2F2; background-size:cover; background-image: url(<?php echo $featured_src[0]; ?>);">
To make the background-image full resolution, change the second argument on the $featured_src line to 'full':
$featured_src = wp_get_attachment_image_src( $post_thumbnail_id, 'full');

Try changing it to
background-image: url('<?php echo $featured_src; ?>')

Related

Is my Logic Wrong? Trying to get Wordpress recent post in a category to display to page

The code that I'm working on resides in a Twenty Seventeen's child theme, on content-front-page.php. I'm trying to get the most recent posts's featured image in each category (I have three categories) to display in a certain way. Shown below:
Originally, in each colored block. I had this in a php block: <?php
$recentport = new WP_Query(array('category_name' => 'ports-and-terminals', 'numberposts'=> 1, 'posts_per_page'=> 1));
while($recentport->have_posts()): $recentport->the_post();
$urlp = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' );
?>
<div style="height: 250px; position: relative">
<div style="height:inherit; background: url('<?php echo $urlp;?>'); background-size:cover; "> <!--i-->
<div id="img-overlay" style="height: inherit; background: linear-gradient(#0000ff, #000066); opacity: .7;">
</div>
<span class="feat-title-link">
<a href="<?php the_permalink(); ?>">
<?php the_title_limit(75, '...'); ?>
</a>
</span>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
The featured images populated with the above code. If I add a new post in wordpress and set it to a category with the code above, the new post appears underneath or above the set category, like:
That is not what I want. I want to keep the layout in the 1st picture. So...I changed the code in one color block to this to test: <?php
$args = array('category_name' => 'ebusiness', 'numberposts'=>'1');
$recentcust = wp_get_recent_posts($args);
foreach( $recentcust as $post ){
$linkid = get_permalink($post["ID"]);
$thumb_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' );
$thumb_url = $thumb_url[0];
echo '' .$post["post_title"].'';
echo (!empty($thumb_url)) ? $thumb_url : 'No thumb!';
}
wp_reset_query();
//$recentebiz = new WP_Query(array('category_name' => 'ebusiness', 'post_per_page'=>1));
//while($recentebiz->have_posts()): $recentebiz->the_post();
//$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' );
?>
<div id="recentebiz-tile" style="background: url('<?php echo $thumb_url;?>');">
</div>
The above code does not populate the featured image of the recent post in each category. Hence, my problem. Here's my logic:
$args = array();: $args variable holds the parameters for the next line.
$recentcust = wp_get_recent_posts($args): $recentcust variable holds the results of *wp_get_recent_posts* query with my set of parameters.
foreach( $recentcust as $post ){} loops through the results and separates them into $post.
Inside the foreach(){} loop: $linkid = get_permalink($post["ID"]): $linkid is the link with the $post's ID.
Inside the foreach(){} loop: $thumb_url = wp_get_attachment_image_src(
get_post_thumbnail_id($post->ID), 'thumbnail' ): gets the featured image that is associated with the $post's ID?
Inside the foreach(){} loop: $thumb_url = $thumb_url[0]: gets the 1st picture (with the array position at 0)?
Outside of the foreach(){} loop: echo $thumb_url[0] to display featured image.
But it's not displaying it. I want the featured image to display in each code block. I know I'm going about this the hard way, but I want to know how the code and Wordpress works. Am I missing something? Thanks in advance.
Resources I've used are to come up with this code:
Wordpress.org Codex: wp_get_recent_post
Wordpress.org Codex: wp_get_attachment_image_src
StackOverflow Question + Answers
And some others...
I've figured it out...somehow. For future reference, I got the code snippet from here. Then changed it to this:
<?php
$args = array('category_name' => 'ebusiness', 'numberposts'=>'1');
$posts = get_posts( $args );
foreach($posts as $post) {
$thumbnail_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array('220','220'), true );
$thumbnail_url = $thumbnail_url[0];
echo ( !empty($thumbnail_url) ) ? $thumbnail_url : 'No thumb!';
}
wp_reset_query();
?>
Then I used another foreach loop to bring back the permalink and the post title with the same args parameters in a different query.
I just wanted get help in understanding the functions involved bringing back pictures. I know there's a better way of doing this. Just need help figuring that out now.
In this code of yours
$recentport = new WP_Query(array('category_name' => 'ports-and-terminals', 'numberposts'=> 1, 'posts_per_page'=> 1));
while($recentport->have_posts()): $recentport->the_post();
$urlp = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' );
?>
<div style="height: 250px; position: relative">
<div style="height:inherit; background: url('<?php echo $urlp;?>'); background-size:cover; "> <!--i-->
<div id="img-overlay" style="height: inherit; background: linear-gradient(#0000ff, #000066); opacity: .7;">
</div>
<span class="feat-title-link">
<a href="<?php the_permalink(); ?>">
<?php the_title_limit(75, '...'); ?>
</a>
</span>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
As what can I see, you are getting ports-and-terminals latest post then display it within HTML, and the while loop continues doing so.
What I think you should do is closing While loop after getting the recent post immediately.
You can do it all at once like so...
<?php
$args = array('category_name' => 'ebusiness', 'numberposts'=>'1');
$posts = get_posts( $args );
foreach($posts as $post) {
$thumbnail_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array('220','220'), true );
?>
<div style="height: 250px; position: relative">
<div style="height:inherit; background: url('<?php echo $thumbnail_url[0];?>'); background-size:cover; "> <!--i-->
<div id="img-overlay" style="height: inherit; background: linear-gradient(#0000ff, #000066); opacity: .7;">
</div>
<span class="feat-title-link">
<a href="<?php get_permalink($post->ID); ?>">
<?php echo wp_trim_words( $post->post_title, 5, '...' );?>
</a>
</span>
</div>
</div>
<?php
}
?>

the_post_thumbnail_url() causes fatal error with thumbnail support active

I'm working on a child theme from twentythirteen.
Twentythirteen has support fro thumbnails:
add_theme_support( 'post-thumbnails' );
But when I use:
the_post_thumbnail_url()
I get a fatal error.
All the google answers say that add_theme_support( 'post-thumbnails' ) has to be in the parent theme functions.php, well, in this case it is there but I'm getting the fatal error anyway.
I have even duplicated the support sentence in the child functions.php (just in case) but still in trouble with this.
The code:
query_posts('category_name=curso&showposts=3');
?>
<?php if (have_posts()) : ?>
<h2>Cursos</h2>
<?php while ( have_posts() ) : the_post(); ?>
<div class = "ficha curso">
<?php
if ( has_post_thumbnail() && ! post_password_required() ) : ?>
<div class="ficha-thumbnail" style = "background: url('<?php the_post_thumbnail_url('large'); ?>') no-repeat; background-size: 300px auto"></div>
<?php endif; ?>
<h3 class="ficha-title"><?php the_title(); ?></h3>
<div class="ficha-resumen">
<?php the_excerpt(); ?>
</div><!-- .entry-content -->
</div>
<?php endwhile; endif;
Try this Logic if it does anything for you:
<?php
if ( has_post_thumbnail() && ! post_password_required() ) :
$imgURL = wp_get_attachment_url( get_post_thumbnail_id(get_the_ID()) );
?>
<div class="ficha-thumbnail" style = "background: url('<?php echo $imgURL; ?>') no-repeat; background-size: 300px auto"></div>
<?php endif; ?>
Hope this does the trick for you... ;-)
With the excellent help of Poiz I have finally found a solution. The one suggested by him works, but I needed to get the large thumbnail rather than the attachment.
This is doing the trick for me:
<?
if ( has_post_thumbnail() && ! post_password_required() ) :
$imgURL = the_post_thumbnail(get_the_ID(), 'large');
preg_match('/src="([^"]+)/i',$imgURL, $src);
?>
<div class="ficha-thumbnail" style = "background: url('<?php echo $src; ?>') no-repeat; background-size: 300px auto">
I will mark Poiz answer as the correct one as he deserves the reputation.
Have you tried removing single quotation marks around php tags in your markup? It seems like these are the source of a parsing problem.
background: url(<?php the_post_thumbnail_url('large'); ?>)
You might like this simple function to do the job.
function getImage($id, $size){
if(has_post_thumbnail()){
return wp_get_attachment_image_src(get_post_thumbnail_id($id), $size)[0];
}
return false;
}
It checks if there is a post thumbnail, if there is it looks for the post thumbnail ID, and then using the $size and wp_get_attachement_image_src's first part of the answer (the src) the response.
I was able to retrieve the post featured image url with the following:
wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' );

Wordpress featured image as background with default image

I have some code that uses a posts featured image as the background of a DIV...
<?php if (has_post_thumbnail( $post->ID ) ):
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
endif; ?>
<div class="news-image" style="background-image: url(<?php echo $image[0]; ?>); background-repeat: no-repeat; background-color: #000; background-position: center center; background-size: cover;">
Pretty simple, but I need to set a default image on this background for any posts that do NOT have a featured image set.
Is that possible by modifying my above code?
For example...
If post has featured image - show it.
If post does not have featured image - show default.jpg.
Sure you can do that, I did some rought snippet, hope you can get the info from my comments :)
<?php
// first you have to define and save your default image somewhere,, save it in options table for ex.. like this:
add_option( 'my_default_pic', 'http://www.website.com/image/jpg', '', 'yes' );
// then on page or template use like this
if (has_post_thumbnail( $post->ID ) ){
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' )[0];
} else {
$image = get_option( 'my_default_pic' );
}
?>
<div class="news-image" style="#000 no-repeat center center background: url(<?php echo $image; ?>);">
body = lorem ipsum
</div>
that's it :)
hth,
edit: inserted example image path

Recent posts as thumbnails - want to show title below image

I have found this code for showing thumbnails as recent posts in a widget. It appears in a grid format which I like very much. I would like to add the title of the post below each thumbnail images. I can manage to get the title show by using the_title(); but then it does not stay as a grid but turns into a list. I would appreciate any help. Thanks
The css used is:
.attachment-thumbnail {
height:150px;
width:150px;
padding:5px;
background:#fff;
margin:5px 5px 0 0;
}
Code:
<?php
$my_query = new WP_Query('showposts=12&amp;amp;amp;orderby=rand');
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
?>
<?php
$attachments = get_posts( array(
'post_type' => 'attachment',
'number_posts' => 1,
'post_status' => null,
'post_parent' => $my_query->post->ID,
) );
if ($attachments) {
?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php
$thumbnail_id = $attachments[0]->ID;
echo wp_get_attachment_image( $thumbnail_id );
}
endwhile;
}
wp_reset_query();
?>
It'd be helpful to know the CSS attributes that are being assigned to the title text when it's introduced to the page you're creating as that's likely what's turning your grid into a list. I would think the best way forward here is to wrap the thumbnail/title pairs in a div and to make those divs have the attribute "display: inline" so that the divs appear side by side rather than on top of each other.

How to add a thumbnail in a post in wordpress?

I want to add thumbnails in the post in front page. But don't know how to do it. My theme do not support custom_field. one of the category's code is I pasted here.......
<div class="featured">
<h2>HEADLINES</h2>
<!--This is where the thumbnails are found for the homepage bottom section - note the custom field name for this image is "thumbnail". Recommended image size is 70x70, as the stylesheet is written for this size.-->
<?php $recent = new WP_Query("cat=3&showposts=3");
while($recent->have_posts()) : $recent->the_post();?>
<?php the_post_thumbnail(); ?>
<?php if( get_post_meta($post->ID, "thumbnail", true) ): ?>
<imgstyle="float:left;margin:0px 10px 0px 0px;" src="<?php echo get_post_meta($post->ID, "thumbnail", true); ?>" alt="alt text" />
<?php else: ?>
<imgstyle="float:left;margin:0px 10px 0px 0px;" src="<?php bloginfo('template_url'); ?>/images/thumbnail.jp" alt="Default thumbnail" />
<?php endif; ?>
<b><a href="<?php the_permalink() ?>" ><?php the_title(); ?></a></b>
<?php the_content_limit(80, ""); ?>
<div style="border-bottom:1px dotted #AFAFAF; margin-bottom:10px; padding:0px 0px 10px 0px; clear:both;"></div>
<?php endwhile; ?>
I rename the extension jpg to jp coz stackoverflow donot allow me to add image. I also changed
Prior to WordPress 2.9, thumbnails had to be custom fields. Since then, native support has been added.
Read about it on the Codex: http://codex.wordpress.org/Post_Thumbnails
In a nutshell, add this to your theme's functions.php file:
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
}
Then, add this to your theme where you want the thumbnail to appear:
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
You can try this one.
http://wordpress.org/extend/plugins/wp-post-thumbnail
I didn't test it though. Thanks

Categories