wordpress image isn't displaying where it supposed to - php

I'm trying to create a small wp plugin for my blog, but I've got the following problem.
The post image, isn't displaying in the right spot.
This is the proper HTML
<li>
<div class="projects">
<ul class="projects sticker">
<li><h2><?php the_title(); ?></h2></li>
<li><p>details</p></li>
</ul>
<img src="" />
</div>
</li>
This is how it's displaying now
<li>
<div class="projects">
<ul class="projects sticker">
<li><h2><?php the_title(); ?></h2></li>
<li><p>details</p></li>
</ul>
</div>
</li>
<img src="" />
Basically i have to put the img tag inside the list and div
Here is my code so far
$args = array( 'numberposts' => '3','category' => $cat_id );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li>'
. '<div class="projects">'
. '<ul class="projects sticker">'
. '<li>'
. '<h2>'
. '<a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >'
. $recent["post_title"]
. '</a>'
. '</h2>'
. '</li>'
. '<li><p>details</p></li>'
. '</ul>'
. '<img src="'.the_post_thumbnail('thumbnail').'" />'
. '</div>'
. '</a>';

use this code, you have used extra <li></li>
$args = array( 'numberposts' => '3','category' => $cat_id );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<a href="' . get_permalink($recent["ID"]) .
'" title="Look '.esc_attr($recent["post_title"]).'" >'
.'<div class="projects">' .'<ul class="projects sticker">'
.'<li>' .'<h2>' . $recent["post_title"] .'</h2>' .'</li>'
.'<li><p>details</p></li></ul>'
.'<img src="'.the_post_thumbnail('thumbnail').'" />'
.'</div>' .'</a>';
}

You have an extra closing <li> at the end and the placement of the closing tag of the first <li> is improperly nested and <a href> opening & closing tag is misplaced as well. Also you could have solved this problem easier—possibly by yourself—if you format the code so humans it can more easily be read. Piling on a stack of instructions on one line like that will only cause confusion:
$args = array( 'numberposts' => '3','category' => $cat_id );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li>'
. '<div class="projects">'
. '<ul class="projects sticker">'
. '<li>'
. '<h2>'
. '<a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >'
. $recent["post_title"]
. '</a>'
. '</h2>'
. '</li>'
. '<li><p>details</p></li>'
. '</ul>'
. '<img src="'.the_post_thumbnail('thumbnail').'" />'
. '</div>'
. '</a>'
;

Related

PHP make the whole container as link (not only the button)

im a total php illiterate so i wanted to make a change in the php file of my wordpress website. i have some cards which represent my services but the clickable link is only in the "read more..." phrase but i want to make the whole card be as a link.
below is the php code i think is associated with that:
<?php elseif( $style == 'style_5' ) : ?>
<h3><?php echo esc_html( $title ); ?></h3>
<?php else : ?>
<h4 class="no_stripe"><?php echo esc_html( $title ); ?></h4>
<?php endif; ?>
</div>
<?php } ?>
<?php echo wpb_js_remove_wpautop( $content, true ); ?>
<?php
if ( $link['url'] ) {
if ( ! $link['title'] ) {
$link['title'] = esc_html__( 'Read More', 'consulting' );
}
if ( ! $link['target'] ) {
$link['target'] = '_self';
}
if( $icon ){
$link['title'] = '<span>' . esc_html( $link['title'] ) . '</span>' . '<i class=" ' . esc_attr( $icon ) . ' stm_icon"></i>';
}
echo ' <a class="read_more" target="' . esc_attr( $link['target'] ) . '" href="' . esc_url( $link['url'] ) . '">' . $link['title'] . '</a>';
}
?>
<?php if( $style == 'style_3' ): ?>
</div>
<?php endif; ?>
<?php endif; ?>
In the html code, try to put the whole card inside of an 'a tag', this should make the whole card a link, and in the 'a tag' put your desired link in the href="". This should work!
Well, this is the link (from your code):
echo ' <a class="read_more" target="' . esc_attr( $link['target'] ) . '" href="' . esc_url( $link['url'] ) . '">' . $link['title'] . '</a>';
So, I would simply try to wrap that around the container of your "card" by splitting it up into...
echo ' <a class="read_more" target="' . esc_attr( $link['target'] ) . '" href="' . esc_url( $link['url'] ) . '">' . $link['title'];
...and:
echo '</a>';
The code of the container has to go between those two parts. Most likely that would be directly under the first line of the code you posted for the first part until the original position of the closing a tag. And since there is (non-php) HTML code in that section, that first line should be wrapped by php tags, like:
<?php echo ' <a target="' . esc_attr( $link['target'] ) . '" href="' . esc_url( $link['url'] ) . '">' . $link['title']; ?>
Probably the "read more" class whould be avoided in the link now.
So the complete code would be
<?php elseif( $style == 'style_5' ) : ?>
<?php echo ' <a target="' . esc_attr( $link['target'] ) . '" href="' . esc_url( $link['url'] ) . '">' . $link['title']'; ?>
<h3><?php echo esc_html( $title ); ?></h3>
<?php else : ?>
<h4 class="no_stripe"><?php echo esc_html( $title ); ?></h4>
<?php endif; ?>
</div>
<?php } ?>
<?php echo wpb_js_remove_wpautop( $content, true ); ?>
<?php
if ( $link['url'] ) {
if ( ! $link['title'] ) {
$link['title'] = esc_html__( 'Read More', 'consulting' );
}
if ( ! $link['target'] ) {
$link['target'] = '_self';
}
if( $icon ){
$link['title'] = '<span>' . esc_html( $link['title'] ) . '</span>' . '<i class=" ' . esc_attr( $icon ) . ' stm_icon"></i>';
}
echo '</a>'; } ?>
<?php if( $style == 'style_3' ): ?>
</div>
<?php endif; ?>
<?php endif; ?>
All this is based on the code you provided, there might be other stuff to consider, but you can try this.

Strange misbehaviour with chaining PHP Wordpress Post Loop

I am trying to simply output all posts but everything is working fine but I want to add the classes in the tag but the classes gets written in plain text not into the tag.
my code:
<?php
if (have_posts()) :
// Start the Loop.
while (have_posts()) : the_post();
echo '<article id="post-' . get_the_ID() . '" ' . post_class() . '>';
twentyfourteen_post_thumbnail();
the_title('<h1 class="entry-title">', '</h1>');
echo '<div class="entry-summary">';
the_excerpt();
echo '</div>';
echo '</article>';
endwhile;
endif;
?>
I think it has something to do with the chaining but I tried everything... :(
Replace
echo '<article id="post-' . get_the_ID() . '" ' . post_class() . '>';
with
echo '<article id="post-' . get_the_ID() . '" ';
post_class();
echo '>';

Get Wordpress excerpt in list of posts

I'm building a custom WordPress widget/plugin to return the title and excerpt of a post within a permalink to the article. I am almost there but am struggling with getting the excerpt to show. Here is my current code:
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
$categories = get_the_category($recent["ID"]);
$excerpt = apply_filters('get_the_excerpt', $recent->post_excerpt);
echo '<a class="m-item diet-and-nutrition" href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" > <div class="pix"></div><div class="eyebrow"> <b>' . $categories[0]->name . '</b> / '. $recent["post_date"].'</div> <figure>' . get_the_post_thumbnail($recent["ID"], 'full') . '<figcaption class="center"> <span> <h4>'. $recent["post_title"].'</h4> </span> <span> <p>'. $excerpt .'</p> </span> </figcaption> </figure> </a>';
}
?>
It's an array so:
$recent->post_excerpt
should be:
$recent['post_excerpt'];

How to show first post content and links for older post only in WordPress

I am trying to create news paper web site and need help to show the content of recent post and title of old posts only... here is my code
<?php
$args = array(
'orderby' => 'id',
'hide_empty' => 1,
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) { ?>
<div class="newsdiv">
<?php
echo '<center><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></center> <br/> ';
$post_args = array(
'numberposts' => 3,
'category' => $category->term_id
);
$posts = get_posts($post_args);
foreach($posts as $post) {?>
<?php echo"*";the_title(); ?>
<div class="entry">
<?php the_content(); ?>
</div>
<?php
}
echo '<dd class="view-all"> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>Read more </a></dd>';
echo '</dl>';
?>
</div>
<?php
}
?>
you can try the codex previous post
<?php previous_post_link( $format, $link, $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ); ?>
you can use it in simpler and/or complex way something like this.
<?php previous_post_link(); ?>
for more info you can go through with codex http://codex.wordpress.org/Function_Reference/previous_post_link

Display Author Name (outside of loop)

I have an area of my index.php file that I'm customizing in a theme whereby I would like to display the most recent post and some additional meta. I was able to pull the featured image, post title (and link), & the date; but not the author. I've tried various examples from the WP boards with no luck. The latest attempt below:
<?php
$args = array( 'numberposts' => '1');
$recent_posts = wp_get_recent_posts( $args );
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
//$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
foreach( $recent_posts as $recent ){
$post_author = get_user_by( 'id', $recent->post_author );
echo '<div class="full-width" id="featured-post" style="background-image: url('. $feat_image .')">';
echo '<div class="row featured-post-meta"><div class="small-8 columns">';
echo '<h2><a href="' . get_permalink($recent["ID"]) . '" title="Read: '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a></h2>';
echo '<p>'. $post_author->display_name .' | '. get_the_time('F jS, Y') .'</p>';
echo '<a class="read-post" href="'. get_permalink($recent["ID"]) .'">Read the post</a>';
echo '</div></div></div>';
}
?>
Inside your foreach loop:
$post_author = get_user_by( 'id', $recent['post_author'] );
This gets you the user object which looks like what you were trying to do with $curauth.
You could then output $post_author->display_name in place of $curauth.
echo '<p>'. $post_author->display_name .' ...

Categories