Featured post loop displaying wrong post excerpt - php

Working on a WordPress theme issue, I have an archive page that has a featured post at the top that displays a featured image, post date, and excerpt along with other posts below it.
Running into an issue where the featured post has the correct title, correct image, but incorrect excerpt. It pulls in a different post's text instead.
Any clue as to what is incorrect with the code below?
<?php
$args = array(
'numberposts' => '1' ,
'post_type' => 'post',
'meta_key' => 'post_featured',
'meta_compare' => '=',
'meta_value' => 1
);
$recent_posts = wp_get_recent_posts( $args );
$fID = 0;
foreach( $recent_posts as $recent ) : ?>
<?php $fID = $recent["ID"]; ?>
<div class="blog-listing featured_post">
<a class="blog-image-lg" href="<?php echo get_permalink($recent["ID"]); ?>" style="background-image: url('<?php echo get_the_post_thumbnail_url( $recent["ID"], 'full' ); ?>');">
<span>FEATURED POST</span>
<?php echo get_the_post_thumbnail( $recent["ID"], 'post-thumbnails-big' ); ?>
</a>
<div class="blog-info pull-left">
<h3 class="blog-title"><?php echo $recent["post_title"]; ?></h3>
<span class="blog-date"><?php echo strtoupper(get_the_date('F j, Y')); ?></span>
<p><? echo the_excerpt(); ?></p>
<p class="readmore-wrapper"><a class="readmore" href="<?php echo get_permalink($recent["ID"]); ?>">READ MORE ยป</a></p>
</div>
</div>

Change <? echo the_excerpt(); ?> to <?php echo get_the_excerpt($recent["ID"]); ?>.
the_excerpt() does not work in your case as you are not inside a WP Loop, just a regular for loop.

Related

Attempting something new in WP. Issue resetting WP loop

Im working in WP and basically what I've got set up is some logic to create a separate carousel for each category of my blog.
For each carousel im displaying the top viewed posts for each category. Each Item in the carousel has a post thumbnail, author name and author avatar.
My problem(if you scroll down to the bottom of the home screen pst"meet the community") the posts are showing the right post names and thumbnails but something funky is going on with the author names and avatars. The first post in the shelf looks correct but after that each post is showing the wrong author.I really dont know how to handle this. Ive been mashing they keys for a week already. Any thoughts?
<!-- A shelf for each category by most posts and posts by highest view count-->
<?php
$cat_args = array(
'number' => 5,
'orderby' => 'count',
'post_type' => 'post',
'order' => 'DESC',
'child_of' => 0
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
echo '<div class="shelf_holder">';
echo '<div class="shelf_title"> <h1> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" )) . '" ' . '> View All</h5></div>';
echo '<div class="shelf_prev"><div class="backwards_icon"></div></div>';
echo '<div class="shelf_next"><div class="forward_icon"></div></div>';
echo '<div class="display-posts-listing grid">';
$post_args = array(
'numberposts' => 8,
'category' => $category->term_id,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<div class="listing-item">
<?php
$thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'medium');
list($url, $width, $height, $is_intermediate) = $thumbnail;
?>
<div class="media-wrapper">
<?php $post_views = get_post_meta( get_the_ID(), 'post_views_count', true );?>
<div class="counter"><div class="views_icon"></div><?php echo $post_views; ?></div>
<div class="save_card"> <?php the_favorites_button($post_id);?></div>
<div class="media-gradient" style="height:196px;"></div>
<div class="media" style="height:196px;width:<?php echo $width; ?>px;background-image:url(<?php echo $url; ?>);background-size:cover;overflow: hidden;background-position: center;width: 100%;left:0px;"></div>
</div>
<div class="tiny_head"><?php echo get_avatar( get_the_author_meta( 'ID' ), 32 );?> </div>
<div class="card_content">
<h3><?php the_title(); ?></h3>
<div class="entry-meta">
<?php
global $post;
$author_id=$post->display_name;
?>
<?php
the_author_meta( 'display_name', $author_id );
?>
<?php wp_reset_query(); ?>
</div>
</div>
</div> <!-- listing item -->
<?php
}
echo '</div class="display-posts-listing grid">';
echo '</div class="shelf_holder">';
} ?>
Replace your foreach with this:
foreach($posts as $post) : ?>
<div class="listing-item">
<?php
$thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'medium');
list($url, $width, $height, $is_intermediate) = $thumbnail;
$author_id=$post->post_author;
?>
<div class="media-wrapper">
<?php $post_views = get_post_meta( $post->ID, 'post_views_count', true );?>
<div class="counter"><div class="views_icon"></div><?= $post_views; ?></div>
<div class="save_card"> <?php the_favorites_button($post_id);?></div>
<div class="media-gradient" style="height:196px;"></div>
<div class="media" style="height:196px;width:<?= $width; ?>px;background-image:url(<?= $url; ?>);background-size:cover;overflow: hidden;background-position: center;width: 100%;left:0px;"></div>
</div>
<div class="tiny_head"><?= get_avatar( get_the_author_meta( 'ID' ), $author_id );?> </div>
<div class="card_content">
<h3><?= get_the_title($post); ?></h3>
<div class="entry-meta">
<?= get_the_author_meta( 'display_name', $author_id ); ?>
</div>
</div>
</div> <!-- listing item -->
<?php
endforeach;
You had some errors in the way you were using get_the_author_meta. It needs the author ID and you tried getting it using display_name. The one you used for the avatar is hardcoded. (That's only going to return the meta that belongs to ID 32).
There are a lot of inconsistencies in your code. Try to understand what is happening. Look up the functions you're using and what they do exactly. The PHP documentation and WordPress documentation both are excellent resources.

I have problem when showing post in loop using WP_Query in Wordpress

i have a problem when trying to show a current 4 post using WP_Query.
But, the older post (The very first post) also shown in the first loop.
This is my code:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => '3'
);
$query = new WP_Query( $args ); //show 4 post
if ( $query->have_posts() ){
/* Start the Loop */
while ( $query->have_posts() ) {
$query->the_post();?>
<div class="col-md-3 d-flex align-items-stretch">
<div class="card ">
<?php if (has_post_thumbnail()) {
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
?>
<img class="miniimg" src="<?php echo $featured_img_url; ?>" width="auto" height="200px">
<?php } ?>
<div class="card-body">
<h4><a href="<?php the_permalink(); ?>">
<?php the_title();
$post_date = get_the_date( 'j F Y' );
?>
</a></h4>
<p class="card-text"><?php the_excerpt();?></p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
<button type="button" class="btn btn-sm btn-primary">Read More</button>
</div>
<small class="text-muted"><?= $post_date; ?></small>
</div>
</div>
</div>
</div>
<?php
$counter++; }
}
?>
This is the result =>
How to fix this problem? is there any problem with my code?
Thankyou.
If specifying 'posts_per_page' => 3 gives back 4 posts, it is almost 100% sure that the first post is a sticky post. Use the option ignore_sticky_posts to ignore them.
$args = array(
'post_type' => 'post',
'posts_per_page' => 4,
'ignore_sticky_posts' => 1,
);
...
A Sticky Post is the post will be placed at the top of the front page of posts. This feature is only available for the built-in post type post and not for custom post types.
Source # https://developer.wordpress.org/themes/functionality/sticky-posts/
I'm pretty sure you applied is sticky to your post from 2012.
To verify you can just add the following to your loop inside the while statement:
<?php //...
while( have_posts() ): the_post();
if( is_sticky() ):
echo 1;
else: echo 0;
endif;
endwhile;
//... ?>
Or go to you post in the admin console, and verify that you didn't check the "is sticky" checkbox on the right side in the publish panel.
<?php
$args = array( 'post_type' => 'movies');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?><div class="movie-content" >
<?php
echo '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( $post->post_title ) . '">' ?>
<h2><?php echo the_title(); ?></h2>
<div><?php the_post_thumbnail('thumbnail'); ?></div>
<div class="excerpt"><?php the_excerpt(); ?></div>
<?php
the_content();
echo '<div class="entry-content">';
echo '</div>';
echo "</div></a>";
endwhile;
?>

Php with WordPress how to call up permalinks to two separate posts

i'm building a recent posts function into a wordpress site, i've got it to call up two different featured images but they are both linking to the same post, can anyone see where i am going wrong?
<?php
$args = array(
'posts_per_page' => 2,
'order_by' => 'date',
'order' => 'desc'
);
$post = get_posts( $args );
if($post) {
$post_id = $post[0]->ID;
if(has_post_thumbnail($post_id)){
?>
<div class="grid_24">
<div class="grid_12">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php
echo get_the_post_thumbnail($page->ID, 'medium');
?>
</a>
</div>
<div class="grid_12">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php
echo get_the_post_thumbnail( $post_id,'medium');
?>
</a>
</div>
</div>
<?php
}
}
?>
you can use echo get_the_permalink($post->ID) to get the uri for the posts
So it looks like in your case you'd need
echo get_the_permalink($post[0]->ID);
and
echo get_the_permalink($post[1]->ID);
in the href
However you're probably better off creating a foreach loop to go through the posts from the get_posts function
https://developer.wordpress.org/reference/functions/get_the_permalink/
https://developer.wordpress.org/reference/functions/get_posts/
Okay, first of all, you are not looping the query you have made ( e.g $posts = get_posts( $args ); ) you are just displaying the 1st post's thumbnail and the thumbnail of the current page.
You need to loop the post like this :
<?php
$args = array(
'posts_per_page' => 2,
'order_by' => 'date',
'order' => 'desc'
);
$posts = get_posts( $args );
?>
<?php if ( !empty( $posts ) ) :?>
<div class="grid_24">
<?php foreach ( $posts as $post ) : ?>\
<?php if( has_post_thumbnail( $post->ID ) ) ?>
<div class="grid_12">
<a href="<?php echo esc_url( get_permalink( $post->ID ) ) ?>">
<?php echo get_the_post_thumbnail( $post->ID, 'size_here'); ?>
</a>
</div>
<?php endif; ?>
<?php endforeach?>
</div>
<?php endif;

How to make php post loop with category icons

So basically, I'm working on a custom wordpress theme. What i'm trying to do is to set an icon for each category. If the loop starts and the post has a category, It'll show up with the icon that it has assigned. Right now it shows the correct icons, but the title and exerpt of the post keeps changing to the name of the page. Here is an example I have three posts math, english and history all of them have the correct icon, but display the name blog post page instead of math, english, or history.
<?php /* Template Name: News Blog Page */ get_header(); ?>
<div id="blog-post-wrapper" class="section_wrapper">
<div class="column three-fourth">
<?php $currentPage = get_query_var('paged');
$args = array(
'post_type' => 'post',
'order' => 'DESC',
'posts_per_page' => 9,
'paged' => $currentPage
);
$the_query = new WP_Query($args);
if($the_query -> have_posts()):
while ($the_query -> have_posts()): $the_query -> the_post();
get_template_part('postloopcontent', get_post_format());
endwhile;
echo "<div class='pagination'>";
echo paginate_links(array(
'total' => $the_query -> max_num_pages
));
echo "</div>";
endif;
?>
</div>
<div class="column one-fourth">
<?php get_sidebar(); ?>
</div>
</div>
<?php get_footer(); ?>
the top one is my basic layout and it grabs my loop. the bottom one is my loop
<?php
// Standard Post Format
?>
<?php $bgImage = get_the_post_thumbnail_url(); ?>
<div class="column one-third" style="background-image:url(<?php echo $bgImage; ?>);">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="nws-img">
<?php
// Find the first category the post is in.
$categories = get_the_category();
$category = $categories[ 0 ]->term_id;
$imgargs = array(
'cat' => $category,
'post_status' => 'inherit',
'post_type' => 'attachment',
'posts_per_page' => '1'
);
$imgquery = new WP_Query( $imgargs );
if ( $imgquery->have_posts() ) {
while ( $imgquery->have_posts() ) { $imgquery->the_post(); ?>
<div class="category-featured-image">
<?php echo wp_get_attachment_image( $post->ID, 'thumbnail' ); ?>
</div>
<?php
}
}
// Reset postdata to restore ordinal query.
wp_reset_postdata();
?>
</a>
<div id="content-box">
<h1> <a href="<?php the_permalink(); ?>" > <?php the_title(); ?> </a> </h1>
<?php the_excerpt(); ?>
</div>
</div>
In your loop file, you're resting post data i.e. wp_reset_postdata(); outside the $imgquery loop/condition. If you could wrap the postdata rest function inside the condition, I think that should work.
You code must look like this
<?php
// Standard Post Format
?>
<?php $bgImage = get_the_post_thumbnail_url(); ?>
<div class="column one-third" style="background-image:url(<?php echo $bgImage; ?>);">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="nws-img">
<?php
// Find the first category the post is in.
$categories = get_the_category();
$category = $categories[ 0 ]->term_id;
$imgargs = array(
'cat' => $category,
'post_status' => 'inherit',
'post_type' => 'attachment',
'posts_per_page' => '1'
);
$imgquery = new WP_Query( $imgargs );
if ( $imgquery->have_posts() ) {
while ( $imgquery->have_posts() ) { $imgquery->the_post(); ?>
<div class="category-featured-image">
<?php echo wp_get_attachment_image( $post->ID, 'thumbnail' ); ?>
</div>
<?php
}
// Reset postdata to restore ordinal query.
wp_reset_postdata();
}
?>
</a>
<div id="content-box">
<h1> <a href="<?php the_permalink(); ?>" > <?php the_title(); ?> </a> </h1>
<?php the_excerpt(); ?>
</div>
</div>

Getting multiple image of same Title

Taxonomy image
page image
Here, I have categorized my page with different page. In that, I categorized with taxonomy and slug. All the title have different taxonomy as shown in the image. But here I am getting multiple image because it have multiple taxonomy.
So, I just want 8 images as the Business theme are as shown in images and have to display as per the taxonomy.
As you can see the taxonomy count that is 4,4 and 5, So I am getting record like that, but I want to get record as per Business theme, as they are eight but as per taxonomy.
Here what I have tried:
<div class="portfolio-items">
<?php
$terms = get_terms('portfolio_cat');
foreach($terms as $row)
{
$args = array('post_type' => 'portfolio','tax_query' => array(
array(
'taxonomy' => 'portfolio_cat',
'field' => 'slug',
'terms' => array( $row->slug ),
)
));
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
$i=1;
?>
<div class="portfolio-item <?php echo $row->slug; ?> col-xs-12 col-sm-4 col-md-3">
<div class="recent-work-wrap">
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<?php $large_url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'large' ); ?>
<img class="img-responsive" src="<?php echo $url; ?>" alt="">
<div class="overlay">
<div class="recent-work-inner">
<h3><?php the_title(); ?></h3>
<?php $content = get_the_content(); ?>
<p><?php echo $content; ?></p>
<a class="preview" href="<?php echo $large_url; ?>" rel="prettyPhoto"><i class="fa fa-eye"></i> View</a>
</div>
</div>
</div>
</div><!--/.portfolio-item-->
<?php
// End of the loop.
$i++;
endwhile;
endif;
WP_Reset_Query();
}
?>
</div>

Categories