wordpress recent posts show thumbnail - wordpress

How can I show either a small thumbnail of a image or featured image, from each post In my recent posts custom widget
Here's my code:
<?php
include($_SERVER['DOCUMENT_ROOT'] . $root . 'blog/wp-load.php');
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 3
));
?>
<h3 class="divider">Recent Blog Posts</h3>
<ul class="listing">
<?php foreach($recent_posts as $post) { ?>
<li><a href="<?php echo get_permalink($post['ID']) ?>">
<div><?php echo $post['post_title'] ?></div></a></li>
<?php } ?>
</ul>

You can use get_the_post_thumbnail
Usage:
<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>
Integrated with your code:
<?php foreach($recent_posts as $post) : ?>
<li>
<a href="<?php echo get_permalink($post['ID']) ?>">
<?php echo get_the_post_thumbnail($post['ID'], 'thumbnail'); ?>
<div><?php echo $post['post_title'] ?></div>
</a>
</li>
<?php endforeach; ?>
Note: To enable Post Thumbnails, the current theme must include add_theme_support( 'post-thumbnails' ); in its functions.php file. See also Post Thumbnails.
Source(s)
Function Reference/get the post thumbnail
See also
Post Thumbnails
Function Reference/add theme support

In your current installed theme, if you are making any changes to the PHP files, it will get replaced if there are any updates done to the theme. To avoid, this, create a child theme and make updates to those php files.
Thr below blog also explains in details on how to add Thumbnail to Recent post widget.
Blog on how to Add Thumbnail to Recent posts
Hope this helps.

Related

Custom fields in WP and get_pages function

I have in my template-parts file where I created this code to list pages under page with ID=347 (I just wanted to create list of products using template file on one, single page). Code looks like this:
<?php $pages = get_pages(array('child_of' => 347, 'sort_column' => 'post_date', 'sort_order' => 'desc')); ?>
<?php foreach ($pages as $page): ?>
<div class="fw-col-xs-12 fw-col-sm-4 product-box">
<?php echo get_the_post_thumbnail($page->ID, 'large');?>
<div class="fw-heading fw-heading-h3 naglowek-maly"><h3><?php echo $page->post_title; ?></h3></div>
<a class="fw-btn fw-btn-1 button1" href="<?php echo get_permalink($page->ID); ?>" title="<?php echo esc_attr($page->post_title);?>"><span>check more</span></a>
</div><!--product box-->
<?php endforeach; ?>
And now I want to add extra field there using Advanced Custom Fields plugin. I need to display below the small info (it will be product dimensions). So I created custom field and I am able to show field only if I add an ID of this page for example:
<?php the_field('product_dimensions', 200); ?>
How to make it dynamic? I was trying to use simply:
<?php the_field('product_dimensions'); ?>
But nothing shows then. Do I have to add there some extra code to read the ID for each page?
just add $post->ID
the_field('product_dimensions', $post->ID);

Switch Wordpress database outside Wordpress

I have two Wordpress blogs on the same domain/server - one regular posts the other videos - plus other non-Wordpress pages which have a box with the latest blog posts using this code:
<ul>
<?php
require($_SERVER['DOCUMENT_ROOT'] . '/blog/wp-load.php');
$args = array(
'cat' => 5, // Only source posts from a specific category
'posts_per_page' => 1 // Specify how many posts you'd like to display
);
$latest_posts = new WP_Query( $args );
if ( $latest_posts->have_posts() ) {
while ( $latest_posts->have_posts() ) {
$latest_posts->the_post(); ?>
<li>
<span class="post_title"><?php the_title(); ?></span>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php if ( has_post_thumbnail() ) { ?>
<span class="post_thumbnail"><?php the_post_thumbnail(); ?></span>
<?php } ?>
</a>
<span class="post_excerpt"><?php the_excerpt(); ?></span>
</li>
<? }
}
wp_reset_postdata();
?>
</ul>
All works as I want it. However we now want a second box under the first on the same pages with posts from the other blog (video). As wp-load.php has already been included for the first blog box it gets posts from the wrong blog.
How can I swap the database connection to the settings for the other (video) blog?
I have searched for solutions and found
$wpdb = new wpdb( $dbuser, $dbpassword, $dbname, $dbhost );
I am lost as to how to use this and make it work.
I have considered merging them in to one blog but that seems a bit excessive for what would seem a simple problem so would rather not do that.
Thanks

wordpress the_content not showing video

I am developing a separate website and for showing the blogs i am using the worpress.I have used following code to display blogs.It shows the textual contents properly but for video it just shows the player bar and not click able.
I have also checked themes index.php there is no the_excerpt.
When i check preview of the post using wordpress admin it shows the video properly.
can anyone help me resolve this?
here is my code..
<?php
global $more;
$posts = get_posts('category=3&numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post) : setup_postdata( $post ); ?>
<?php $more = 1; ?>
<?php the_date(); echo "<br />"; ?>
<span style="color:#1C1644;font-size:1.3em !important;font-weight: bold;">
<?php the_title(); ?>
</span>
<div id="lol"><?php the_content(); ?>
</div>
<hr>
<?php
endforeach;
?>
Please try this
<?php
global $more;
$posts = get_posts('category=3&numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post) :
setup_postdata( $post ); ?>
<?php $more = 1; ?>
<?php the_date(); echo "<br />"; ?>
<span style="color:#1C1644;font-size:1.3em !important;font-weight: bold;">
<?php echo the_title(); ?>
</span>
<div id="lol">
<?php echo the_content(); ?>
</div>
<hr>
<?php
endforeach;
?>
All you need to do to embed something into a post or page is to post the URL to it into your content area. Make sure that the URL is on its own line and not hyper-linked (clickable when viewing the post).
For example:
http://www.youtube.com/watch?v=dQw4w9WgXcQ
WordPress will automatically turn that into a YouTube embed when the post is viewed.
You can also optionally wrap the URL in the [embed] shortcode. It will accomplish the same effect, but does not require the URL to be on its own line.
It also allows you to set a maximum (but not fixed) width and height, like so:
[embed width="123" height="456"]http://www.youtube.com/watch?v=dQw4w9WgXcQ[/embed]
If WordPress fails to embed your URL you will get a hyper-link to the URL.
Use wp custom fields. Add video_embed custom field your post and add code.
<?php echo get_post_meta($post->ID, 'video_embed', true); ?>
Edit:
if(get_post_meta($post->ID, 'video_embed', true)){
echo get_post_meta($post->ID, 'video_embed', true);
}
else
{
the_content();
}

Displaying post titles from specific category in WordPress

I would like to display a few post titles from a specific category on homepage. First one have to be with a small thumbnail and excerpt and rest of them just title. Below this section I would like to have a link clicking on which will show all the posts under this category.
like this http://i.stack.imgur.com/N5jUA.jpg
As arslaan ejaz said, you can use wp_query. But I think that answer is not enough for your question. You want to show first post with thumbnail and others with titles only right?. It can be done by php count. Here is what I am using on my site. check below code, it will show first post with thumbnail, title and excerpt, other three posts with title only from category ID 1.
<div class="main-div">
<div class="thumbnaildiv">
<?php $count=1; $query = new WP_Query('showposts=4&cat=1&offset=0'); if ($query->have_posts()) : ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
<?php if($count==1){ ?>
<h2>
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?></a>
</h2>
<div class="thumb">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('thumbnail'); ?>
</a>
<p><?php the_excerpt(); ?> ...</p>
</div>
</div><!--div with thumbnail, title, and excerpt end-->
<div style="clear:both"></div>
<div class="without-thumb">
<ul>
<?php } if($count>1){ ?>
<li>
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?></a>
</li>
<?php } if($count==4){ ?>
</ul>
<?php } ?>
<?php $count++; endwhile; else: endif; wp_reset_postdata(); ?>
</div><!--div without thumbnail end-->
</div><!--main div end-->
The div's I have used is for information purpose only. You can change and style it as desired.
Use WP-Query:
<?php
$args = array('cat'=>1);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>' . get_the_title() . '</li>';
echo '<li>' . the_permalink() . '</li>';
echo '<li>' . the_excerpt() . '</li>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
It will list all posts of category 1, with title, permalink, and excerpt
More info: wp_query
I can recommend you to add 'Elementor' plugin. With this plugin, you can add 'read more' and split your text. If you add 'read more' in the beginning of your text then there will be shown only title and under the title a 'read more' link.
Here is the Screenshot

showing the ten post thumbnail

I want to show latest ten post of my security category with date and post title and the post thumbnail.
To show the pic I have faced with problem. According to this article
http://www.wpbeginner.com/beginners-guide/how-to-add-featured-image-or-post-thumbnails-in-wordpress/
when we want to showing the thumbnail first of all we should have to copy the following code to the function.php file
add_theme_support( 'post-thumbnails' );
and using the
<?php the_post_thumbnail(); ?>
To show pic of the post, I use this code into the loop but it doesn’t work. my code is here:
<?php query_posts('securitysoft=CATEGORYNAME&showposts=10');
while ( have_posts() ) : the_post(); ?>
<br/>
<?php
php the_post_thumbnail();
?>
<br/>
<?php the_time(__('j/F/ Y','kubrick')) ?>
<br/>
<?php the_title();?>
<?php endwhile; ?>
The main problem is that the picture does not show. To prove this I am taking post Id of the post that has thumbnail(292) like this :
<?php if ( has_post_thumbnail(292))
{
echo "<script type='text/javascript'>alert('yes')</script>";
has_post_thumbnail(292);
}
else
{
echo "<script type='text/javascript'>alert('no')</script>";
has_post_thumbnail(292);
}
?>
note:292 is the post id
The result of above code is no.
I have replaced php the_post_thumbnail(); with each one of following code but does not work:
get_the_post_thumbnail($post->ID);
echo get_the_post_thumbnail($post->ID);
get_the_post_thumbnail($post_id, 'thumbnail');
echo get_the_post_thumbnail($post_id, 'thumbnail');
<?php echo get_the_post_thumbnail($post_id, 'thumbnail', array('class' => 'alignleft')); ?>
And this is my function.php :
<?php
add_theme_support( 'post-thumbnails' );
if ( function_exists('register_sidebar') )
register_sidebar();
?>
do you set the feature image!! remember if you don't set the feature image. the image won't display. because this command that you have used only take the thumbnail. to set the feature image go to the feature image panel and click on the Set featured image link.

Resources