How to add Wordpress image thumbnail above the post title - php

Trying to edit the post page inside our theme and wanted to add the thumbnail image above the post title but it keeps placing the thumbnail at the end of the post title. Any help is appreciated.
if (have_posts()) :
echo "<h1>" . __('Valhalla Integration Blog','avia_framework') . "</h1>";
echo "<ul>";
while (have_posts()) : the_post();
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
echo "<li><h4><a href='".get_permalink()."' rel='bookmark' title='". __('Permanent Link:','avia_framework')." ".the_title_attribute('echo=0')."'>".get_the_title()."</a></h4></li>";
endwhile;
echo "</ul>";
else:
echo "<h3>" . __('No Blog Posts found','avia_framework') . "</h3>";
endif;

You're echoing an <img> tag into a <ul> which is invalid HTML. I modified it to echo the image inside the <li> which also contains your title.
if (have_posts()) :
echo "<h1>" . __('Valhalla Integration Blog','avia_framework') . "</h1>";
echo "<ul>";
while (have_posts()) : the_post();
echo "<li>";
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
echo "<h4><a href='".get_permalink()."' rel='bookmark' title='". __('Permanent Link:','avia_framework')." ".the_title_attribute('echo=0')."'>".get_the_title()."</a></h4></li>";
endwhile;
echo "</ul>";
else:
echo "<h3>" . __('No Blog Posts found','avia_framework') . "</h3>";
endif;

Related

adding pagination to a foreach loop in wordpress

Am looking for a way to implement pagination on my categories
i tried to use paginated_links(); functions but it works only for posts, and not the actual categories. I have looked around and it seems this is a little trickier than I was expecting...
$cats = get_categories(
array('parent' => $cat->cat_ID)
);
// loop through the categries
foreach ($cats as $cat) {
echo '<div class="card">';
// setup the cateogory ID
$cat_id= $cat->term_id;
echo '<div class="card-body">';
// make images for the category
echo '<div class="card-title">' . $cat->name . ' <i class="fas fa-arrow-right"></i></div>';
echo '</div>';
// create a custom wordpress query
query_posts("cat=$cat_id&posts_per_page=3");
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php echo '<div class="blog-list-points">' ?>
<?php // create our link now that the post is setup ?>
<a href="<?php the_title();?>">
<?php the_excerpt(); ?>
<hr>
</a>
<?php echo '</div>'?>
<?php endwhile; endif; echo ' </div> '// done our wordpress loop. Will start again for each category ?>
<?php }
// done the foreach statement ?>

WordPress Adding tags to loop content

I am wrapping my head on wordpress loop at the moment, im trying to give tags to the respective content, so an H tag to the title, an p tag to the excerpt and so on...
The code i got so far is
<div id="<?php echo $page_id; ?>" class="container"><!-- begin container -->
<div id="postovi" style="display:none;">
<?php $custom_loop = new
WP_Query('showposts=5&category_name=Zanimljivosti&orderby=rand');
if ( $custom_loop->have_posts() ) : echo '<ul>'; while ( $custom_loop->have_posts() ) : $custom_loop->the_post(); echo '<li>' . get_the_title() . get_the_post_thumbnail($loop->post->ID, 'shop_catalog') . get_the_excerpt();'</li>'; endwhile; wp_reset_query(); echo '</ul>';endif;?> </div>
any suggestions apreciated :)
So your solution is :
<?php
$custom_loop = new WP_Query('showposts=5&category_name=Zanimljivosti&orderby=rand');
if ( $custom_loop->have_posts() ) :
echo '<ul>';
while ( $custom_loop->have_posts() ) :
$custom_loop->the_post(); echo '<li><h2><a href="' . get_permalink() . '">' . get_the_title();
echo '</h2>' . get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
echo'<p>' . get_the_excerpt();'</p></a></li>';
endwhile; wp_reset_query();
echo '</ul>';endif;
?>

PHP if statement isn't working?

I'm working on a Wordpress index page that only shows posts if they have a featured image. This is my content.php code:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="col-sm-4">
<?php
if (has_post_thumbnail()) {
echo '<div class="small-index-thumbnail clear">';
echo '<a href="' . get_permalink() . '" title="' . get_the_title() . '" rel="bookmark">';
echo the_post_thumbnail('index-thumb');
echo '</a>';
echo '</div>';
}
?>
</div>
</article><!-- #post-## -->
I'm just wondering if this is enough code for someone to tell me why it's still showing the posts, despite the posts not having a featured image. Thank you!
Actually, it looks right but there is a note about this on Codex, which is something like this:
// Must be inside a loop.
if ( has_post_thumbnail() ) {
// ...
}
Note The above code apparently fails in some instances and the below code is "recommended"
if ( '' != get_the_post_thumbnail() ) {
// some code
} else {
// some code
}
Check get_the_post_thumbnail if needed.

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 '>';

Display custom post type posts and meta data within the_content filter

I am using this in my mu-plugins.php file//
function new_default_content($content) {
global $post;
if ($post->post_type == 'textures') {
$content .='<li>
<figure>
<?php the_post_thumbnail('thummy'); ?>
<figcaption>
<h3><?php the_title(); ?></h3>
<span>Cool stuff brah.</span>
<?php
if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >View Full Image</a>'; }?>
</figcaption>
</figure>
</li>';
}
return $content;
}
add_filter('the_content', 'new_default_content');
and in my page template I used <?php the_content(); ?> to display everything.
UPDATE//
Full Page Template code
<div id="container" class="clearfix">
<div id="left-content">
<?php get_sidebar('two');?>
</div>
<div id="right-content">
<h1><?php wp_title(''); ?></h1>
<ul class="grid cs-style-3">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
</ul>
</div><!--right-content-->
</div><!--container-->
<?php get_footer(); ?>
But I am recieving this error//
Parse error: syntax error, unexpected T_STRING in /home/xxx/public_html/domain.com/testing/wp-content/mu-plugins/must-use.php on line 15
I am trying this method of displaying content because I want to use the WP-Members plugin, and I realized that the plugin will only work for content within the_content().
So my question is how can I fix the code I posted above to display the title, thumbnail, links etc the correct way?
function new_default_content($content) {
global $post;
if ($post->post_type == 'textures') {
$content .='<li>';
$content .='<figure>';
$content .= the_post_thumbnail('thummy');
$content .= '<figcaption>';
$content .= '<h3>';
$content .= the_title();
$content .= '</h3>';
$content .= '<span>Cool stuff brah.</span>';
if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >View Full Image</a>';
}
$content .= '</figcaption>';
$content .= '</figure>';
$content .= '</li>';
}
return $content;
}
add_filter('the_content', 'new_default_content');
Just try this code. If you still get same error we'll check.
FOUND A SOLUTION
I remembered that you can add content inside of shortcodes.
So I created my own shortcode [textures_content]
Then used the code pasted below to display the content within <?php the_content(); ?> function//
add_shortcode( 'textures_content', 'textures_shortcode' );
function textures_shortcode( $atts ) {
ob_start();
$query = new WP_Query( array(
'post_type' => 'textures',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'date',
) );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<li>
<figure>
<?php the_post_thumbnail('thummy'); ?>
<figcaption>
<h3><?php the_title(); ?></h3>
<?php if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >View Full Image</a>'; }?>
</figcaption>
</figure>
</li>
<?php endwhile;
wp_reset_postdata(); ?>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
Now the Members only plugin works as expected and the blocked content is displayed once logged in :)

Categories