i have the title of my last 4 posts but i need the image post too. I'm terrible programmer `
include('../blog/wp-load.php'); // Blog path
// Get the last 4 posts
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 4,
'category' => 0,
'orderby' => 'post_date',
'post_type' => 'post',
'post_status' => 'publish'
));
// Display them as list
echo '<ul>';
foreach($recent_posts as $post) {
echo '<li>', $post['post_title'], '</li>';
}
echo '</ul>'`
i'm trying to do something like this:
I think you're looking for get_the_post_thumbnail Here's the Codex.
For example:
get_the_post_thumbnail( $postID,'medium', array( 'class' => 'aligncenter' ));
You might try something like this for your final product (untested):
// Display them as list
$output = '<ul>';
foreach($recent_posts as $post) {
$link = get_permalink($post['ID']);
$image = get_the_post_thumbnail( $postID,'medium', array( 'class' => 'aligncenter' ));
$title = $post['post_title'];
$output .= '<li>
<a href="'.$link.'">'.
$image
.'<h2>'.$title.'</h2>
</a>
</li>';
}
$output .= '</ul>';
echo $output;
It's must be work!
<?php
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 4,
'category' => 0,
'orderby' => 'post_date',
'post_type' => 'post',
'post_status' => 'publish',
));
foreach($recent_posts as $single_post){
$get_post_images = get_attached_media( 'image', $single_post['ID'] );
$get_post_images = array_shift( $get_post_images );
$first_image_url = $get_post_images->guid;
?>
<?php echo $single_post['post_title'] ?><br />
<img src="<?php echo $first_image_url; ?>" /><br />
<?php
}
?>
If you have't the post thumbnail, but have images inside post, then you need this solution which use inside the loop:
$get_post_images = get_attached_media( 'image', $post->ID );
$get_post_images = array_shift( $get_post_images );
//Get url for image
$first_image_url = $get_post_images->guid;
// Show the image
echo '<img src="'. $first_image_url .'" />';
I got it!
<ul>
<?php
include('esenergy/wp-load.php'); // Blog path
function recentPosts() {
$rPosts = new WP_Query();
$rPosts->query('showposts=3');
while ($rPosts->have_posts()) : $rPosts->the_post(); ?>
<li>
<?php the_post_thumbnail('recent-thumbnails'); ?>
<h2><?php the_title();?></h2>
</li>
<?php endwhile;
wp_reset_query();
}
?>
</ul>
<?php echo recentPosts(); ?>
Related
I'm running the following code in my WordPress site and while it works, it breaks my comments. I suspect I'm not resetting the query properly.
<?php
// name: posts you may also like
// get the current post's "post id."
$postId = get_the_ID();
$query = new WP_Query([
'post_type' => 'post',
'posts_per_page' => 5,
'post__not_in' => array($postId),
'orderby' => 'rand',
]);
$html = '<div style="font-weight:bold;">OTHER INTERESTING POSTS:</div><ul>';
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$title = get_the_title();
$permalink = get_the_permalink();
$html .= <<<EOD
<li>$title</li>
EOD;
}
}
$html .= '</ul>';
echo($html);
?>
Try the below code.
<?php
// name: posts you may also like
// get the current post's "post id."
$postId = get_the_ID();
$query = new WP_Query( [
'post_type' => 'post',
'posts_per_page' => 5,
'post__not_in' => array($postId),
'orderby' => 'rand',
] );
?>
<div style="font-weight:bold;">OTHER INTERESTING POSTS:</div>
<ul>
<?php if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post();
$title = get_the_title();
$permalink = get_the_permalink(); ?>
<li><?php echo $title; ?></li>
<?php } wp_reset_postdata(); } ?>
</ul>
</div>
<?php
I have four loops. In the first loop I want to display all current articles from all categories (breaking news). In the 2nd loop only articles with the category 4,5,6,7 should be displayed. But if in the 1st loop an article from these categories were published, this newest article should not appear in the 2nd loop. So far I have only read about the general exclusion of articles but nothing about conditions.
You must use category__not_in:
$include_cats = array( 4, 5, 6, 7 );
$exclude_cats = array( 3 ); // 'breaking news' TERM ID
$posts_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 4 ,
'ignore_sticky_posts' => true,
'category__in' => $include_cats,
'category__not_in' => $exclude_cats,
'orderby' => 'date',
);
$posts = new WP_Query( $posts_args );
if ( $posts->have_posts() ) {
while ( $posts->have_posts() ) {
$posts->the_post();
?>
<a href="<?php echo get_the_permalink( $posts->post->ID ); ?>" title="<?php echo get_the_title( $posts->post->ID ); ?>">
<?php
if ( has_post_thumbnail( $posts->post->ID ) ) {
echo get_the_post_thumbnail( $posts->post->ID, $img, array( 'class' => 'post-thumb' ) ); // YOUR IMAGE SIZE
} else {
echo '<img class="' . $img . '" src="placeholder.png" width="150" height="150" alt="' . get_the_title( $posts->post->ID ) . '">'; //ADD PLACEHOLDER IMAGE HERE
}
?>
<h3 class="post-title"><?php echo get_the_title( $posts->post->ID ); ?></h3>
<span class="author"><?php echo get_the_author( $posts->post->ID ); ?></span>
</a>
<?php
}
}
wp_reset_postdata();
I face a problem with displaying products on a specific portfolio post. I use product shortcodes in order to display specific products, but they do not appear.
I use wordpress 5.2.5, avada theme 5.9.1 and woocommerce 3.9.2.
Anyone got an idea what is going on?
If not, can anyone tell me an alternative way to display them?
use shortcode [get_specific_product_shortcode id='here enter product id']
function get_specific_product_shortcode_function($atts){
extract( shortcode_atts(
array(
'id' => '',
), $atts )
);
ob_start();
$args = array(
'posts_per_page' => 1,
'post__in' =>array($id),
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'product',
'post_status' => 'publish',
);
$query = new WP_Query( $args );
$count = $query->found_posts;
if($query->have_posts()){
while( $query->have_posts() ) { $query->the_post();
$post_thumbnail_id = get_post_thumbnail_id();
$product = wc_get_product(get_the_ID());
if (!empty($post_thumbnail_id)) {
$image_size = 'medium'; // (thumbnail, medium, large, full or custom size)
$img_url_arr = wp_get_attachment_image_src( $post_thumbnail_id, $image_size );
$img_url = $img_url_arr[0];
}else{
//placeholder image
$img_url = home_url().'/wp-content/uploads/woocommerce-placeholder.png';
}
?>
<div>
<a href="<?php echo get_permalink();?>">
<img src="<?php echo $img_url?>" style="width: 150px;">
</a>
<div>Price: <?php echo $product->get_price_html(); ?></div>
<div><?php echo get_the_title();?></div>
</div>
<?php
}
wp_reset_postdata();
}else{
echo 'no post'; echo "<br>";
}
return ob_get_clean();
}
add_shortcode('get_specific_product_shortcode', 'get_specific_product_shortcode_function');
I want show featured image in my slider. so what i've done in my function.php is like below:
<?php
function revconcept_get_images($post_id) {
global $post;
$thumbnail_ID = get_post_thumbnail_id();
$images = get_children( array('post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
if ($images) :
foreach ($images as $attachment_id => $image) :
$img_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); //alt
if ($img_alt == '') : $img_alt = $image->post_title; endif;
$big_array = image_downsize( $image->ID, 'large' );
$img_url = $big_array[0];
echo '<li>';
echo '<img src="';
echo $img_url;
echo '" alt="';
echo $img_alt;
echo '" />';
echo '</li><!--end slide-->';
endforeach; endif; }
?>
<div class="flexslider"> <!-- function called in index.php -->
<ul class="slides">
<?php revconcept_get_images("$post->ID"); ?>
</ul>
</div><!--end flexslider-->
Now i have two problem. no.1 My featured image is not showing in the slider. It's showing a broken link. but it can echo the post_id. no.2 I want show only 3/4 images from the latest post of cat=5. can anyone help me to do this ? note that i am using flexslider for my slider.
Ok, this is what I've tested, you may want to refine the query a bit, but for me it worked. In functions.php put
function revconcept_get_images($post_id) {
$images = get_posts( array(
'post_type' => 'attachment',
'hide_empty' => true,
'order' => 'ASC',
'orderby' => 'menu_order ID'
));
foreach ($images as $image) {
$image_url = $image->guid;
$img_alt = get_post_meta($image->ID, '_wp_attachment_image_alt', true); //alt
echo '<li><img src="'.$image_url.'" alt="'.$img_alt.'" /></li>';
}
}
And in index.php put (where you want)
<div class="flexslider"> <!-- function called in index.php -->
<ul class="slides">
<?php revconcept_get_images($post->ID); ?>
</ul>
</div><!--end flexslider-->
If you want to show all featured images from posts you have just put 'posts_per_page' => -1, in the get_posts() array.
I've created a shortcode that displays my Staff post. Each post is floating left and there should be four post on each row. Everything is working fine but I need to add a clear div after every four post since the height may vary depending on the content in each post. How do I add the clear div after every four post in my shortcode? My code is below:
/*staff category start*/
add_shortcode( 'staff_category', 'staff_category' );
function staff_category( $atts ) {
ob_start();
// define attributes and their defaults
extract( shortcode_atts( array (
'type' => 'staff',
'order' => 'ASC',
'orderby' => 'menu_order',
'posts' => -1,
'staff_category' => '',
'category' => '',
), $atts ) );
// define query parameters based on attributes
$options = array(
'post_type' => $type,
'order' => $order,
'orderby' => $orderby,
'posts_per_page' => $posts,
'stafftype' => $staff_category,
'category_name' => $category,
);
$query = new WP_Query( $options );
// run the loop based on the query
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<article class="fourth-col-center staff-profile">
<?php
if(has_post_thumbnail()) { ?>
<div class="staff-img">
<?php the_post_thumbnail( 'staff-thumb' ); ?>
</div>
<?php } else { ?>
<div class="staff-img">
<?php echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/nophoto.jpg" />'; ?>
</div>
<?php } ?>
<h3><?php the_title(); ?></h3>
<div class="loop-post-meta">
<ul>
<li>
<?php
global $post;
$stafftitle_value = get_post_meta($post->ID, "wpstaff_textfield", true);
if($stafftitle_value){ ?>
<?php echo $stafftitle_value ?>
<?php } ?>
</li>
</ul>
</div>
</article>
<?php endwhile;
wp_reset_postdata(); ?>
<div class="cleared"></div>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
/*staff category end*/
You could use CSS, something like this should work:
article.staff-profile:nth-child(4n+5) {
clear: both;
}