How to display thumbnail if post have one other wise don't - php

I would like to display the most recent post in a a block, and if the post is assigned with a thumbnail I would like it to be shown on the left to the post content.
This is my code that I have been trying
<?php query_posts('posts_per_page=1') ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="jumbotron">
<?php if ( has_post_thumbnail(); )?>
<div class="col-md-3">
<?php the_post_thumbnail(); ?>
</div>
<div class="col-md-9">
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(); ?></p>
<p class="text-muted">Posted on <?php the_time('jS F, Y'); ?></p>
</div>
}
<?php else;?>
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(); ?></p>
<p class="text-muted">Posted on <?php the_time('jS F, Y'); ?></p>
</div>
<?php endwhile; wp_reset_query(); ?>
Hope someone could help me! Thanks in advance!

This may be useful for you try something like :
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
if (has_post_thumbnail()) {
$image_url = wp_get_attachment_image_src(get_post_thumbnail_id(),'large', true);
echo '<a href="' . $image_url[0] .'" title="' . the_title_attribute('echo=0') . '">';
the_post_thumbnail();
echo '</a>';
} else {
echo 'test';
}
?>

Related

Custom Image Gallery is not fetching the images on WordPress

I am using an Image Gallery in Custom Post Type on my WordPress Website. The Plugin I am using for that is Advanced Custom Fields. There is some error, due to which image gallery is not getting displayed on frontend.
Please check the page.
http://divinepower.co.in/cof/projects/residential/surrey-hills-vic/
Though the same code is working fine on another custom post type.
http://divinepower.co.in/cof/brands/gloster/asta/
The code I am using is
<?php get_header(); ?>
<div id="content">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
// Show the Gallery
if( have_rows('gallery_slideshow') ):
echo '<div class="projects-gallery-wrap">';
echo '<div class="projects-gallery">';
// loop through the rows of data
while ( have_rows('gallery_slideshow') ) : the_row();
$imgObj = get_sub_field('image', $post->ID);
echo '<img class="item" src="'.$imgObj[sizes]['projects-gallery'].'" alt="'.$imgObj[alt].'"/>';
endwhile;
echo '</div>';
echo '</div>';
endif;
?>
<div id="inner-content" class="wrap cf">
<main id="main" class="main-content" role="main" itemscope itemprop="mainContentOfPage" itemtype="http://schema.org/Blog">
<div class="gallery-controls">
<button class="prev"><i class="fa fa-chevron-left"></i></button>
<button class="next"><i class="fa fa-chevron-right"></i></button>
</div>
<article id="post-<?php the_ID(); ?>" <?php post_class('cf'); ?> role="article" itemscope itemtype="http://schema.org/BlogPosting">
<?php
$currentBrandName = cosh_get_brand_name();
$currentBrandSlug = cosh_get_brand_slug();
?>
<div class="content">
<header class="article-header">
<div class="title-wrap">
<h1 class="entry-title single-title" itemprop="headline"><?php the_title(); ?></h1>
<?php
// Brand Image
print apply_filters( 'taxonomy-images-list-the-terms', '', array(
'before' => '<ul class="projects-logos">',
'after' => '</ul>',
'image_size' => 'full',
'taxonomy' => 'brand'
) );
?>
</div>
<?php
// Social share
$currentURL = get_permalink( $post->ID );
?>
<div class="social-share">
<i class="fa fa-facebook"></i>
<i class="fa fa-pinterest-p"></i>
</div>
</header>
<section class="entry-content cf" itemprop="articleBody">
<div class="content-wrap">
<?php the_content(); ?>
<?php
// projects Link
$projectsURL = site_url()."/brand/".$currentBrandSlug;
if($projects){ ?>
View all <?php echo $currentBrandName; ?> projectss ›
<?php }; ?>
</div>
<?php
// Additional Info
$materials = get_field('materials', $post->ID);
$projectsSpecs = get_field('projects_specs', $post->ID);
if($materials || $projectsSpecs){ ?>
<div class="additional-info">
<?php
// Materials
if($materials){ ?>
<h4>Materials <i class="fa fa-pencil"></i></h4>
<p><?php the_field('materials'); ?></p>
<?php };
// projects Specs
if($projectsSpecs){ ?>
Download Specs <i class="fa fa-download"></i>
<?php }; ?>
</div>
<?php } ?>
</section>
<?php/*
// Show the Gallery
if( have_rows('gallery_slideshow') ):
echo '<div class="projects-gallery-wrap-mobile">';
echo '<div class="projects-gallery-mobile">';
// loop through the rows of data
while ( have_rows('gallery_slideshow') ) : the_row();
$imgObj = get_sub_field('image', $post->ID);
echo '<img class="item" src="'.$imgObj[sizes]['projects-gallery'].'" alt="'.$imgObj[alt].'"/>';
endwhile;
echo '</div>';
echo '</div>';
endif;
*/ ?>
</div>
</article>
<?php get_template_part('parts/enquiry_form'); ?>
</main>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php get_footer(); ?>
Please help me to find the error. Thanks

Editing Wordpress code to bring post type

Is there a way to edit the following so that rather than bringing in the most recent posts, it bring in posts from a specific category?
if( !function_exists('zolo_recent_posts') ) { function zolo_recent_posts($atts, $content = null) { ob_start(); extract(shortcode_atts(array( "num" => '4', 'columnsizepost' => '', 'data_animation'=>'fadeInDown', 'data_delay'=>'0' ), $atts)); global $post;
query_posts( 'post_type=post&posts_per_page='.$num.'&paged='. #$paged ); ?>
<div class="zolo-recent-post <?php echo $columnsizepost;?>">
<?php $i=1 ; while (have_posts()) : the_post(); if (has_post_thumbnail( $post->ID ) ): ?>
<?php //$image=w p_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'recent_posts_thumb' ); ?>
<?php endif; if( $i % 4==0 ) $class='last' ; else $class='' ; ?>
<div class="blog-box <?php echo $class;?> animated hiding" data-animation="<?php echo $data_animation;?>" data-delay="<?php echo $data_delay; ?>">
<div class="blog-img"><a href="<?php the_permalink(); ?>">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('recent_posts_thumb');
}
else {
echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/thumb-img.jpg" />';
}
?>
</a>
</div>
<div class="blog-text-area">
<h3><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a></h3>
<div class="alltag-row"> <span class="date"><i class="fa fa-calendar"></i>
<?php the_time('F jS, Y') ?>
</span> <span class="add-comment"><a href="<?php the_permalink(); ?>#hello">
<?php comments_number( '0 <i class="fa fa-comment-o"></i>', '1 <i class="fa fa-comment"></i>', '% <i class="fa fa-comments"></i>' ); ?>
</a></span>
</div>
<?php the_excerpt() ;?>
</div>
</div>
</span>
<?php $i++; endwhile; ?>
</div>
<?php wp_reset_query(); $demolp_output=o b_get_clean(); return $demolp_output; } add_shortcode( "zolo_recent_post", "zolo_recent_posts"); }
<?php query_posts('category_name=CATEGORYNAME&showposts=5');
while (have_posts()) : the_post();
// do whatever you want
?>
<b><?php the_title(); ?>
<?php
endwhile;
?>
Using this way you can fetch posts of a specific category in WordPress.

Pull all posts with certain tag WordPress

I have this code:
<div class="col-md-4 col-sm-4 col-xs-12 mob">
<?php
$args = array('tag_slug__and' => array('testtag'));
$loop = new WP_Query( $args );
while ($loop->have_posts() ) : $loop->the_post();
?>
<a style="color:#333; text-decoration:none;" href="<?php echo get_permalink(); ?>">
<?php
if(has_post_thumbnail()) {
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'full' );
echo '<img src="' . $image_src[0] . '" width="100%" />';
}
?>
<h4><?php the_title(); ?></h4>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</a>
Which gets a post which has the tag 'testtag'.
Instead of copying this code, and using 'testtag1', 'testtag2'
How can I just grab ALL the posts which have the tag 'testtag' and just keep adding them into 4 columns?
Any links/help
Nevermind, simple fix!!
<div class="row" style="margin-top:20px;">
<?php
$args = array('tag_slug__and' => array('testtag'));
$loop = new WP_Query( $args );
while ($loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-4 col-sm-4 col-xs-12 mob">
<a style="color:#333; text-decoration:none;" href="<?php echo get_permalink(); ?>">
<?php
if(has_post_thumbnail()) {
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'full' );
echo '<img src="' . $image_src[0] . '" width="100%" />';
}
?>
<h4><?php the_title(); ?></h4>
<?php $trimexcerpt = get_the_excerpt();
$shortexcerpt = wp_trim_words( $trimexcerpt, $num_words = 10, $more = '… <br/> Read More ...' );
echo '<a style="color:#333; text-decoration:none;" href="' . get_permalink() . '"><p>' . $shortexcerpt . '</p></a>';
?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</a>
</div>

Wordpress posts_nav_link issue

I have a blog.php with the following code:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</h1>
<section>
<h3><?php the_category(); ?></h3>
<h5><?php the_date(); ?></h5>
<h5><?php posts_nav_link(); ?></h5>
</section>
<p class="articlemeta center"></p>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail();} ?>
<?php the_content(); ?>
<?php edit_post_link( 'bearbeiten' ); ?>
<?php posts_nav_link(); ?>
<?php endwhile; else: ?>
<h2>
Sorry noch keine Texte vorhanden
</h2>
<section class="entry-utility">
<?php $tags_list = get_the_tag_list( '', ', ' ); if ( $tags_list ):?>
<span class="tag-links">
<?php printf( __( '<span class="%1$s">Tagged</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list ); ?>
</span>
<span class="meta-sep">|</span>
<?php endif; ?>
</section>
<?php endif; ?>
My issue is now, that the dont compute a navigation. There is also no navigation when i paste the code at any other destination of the code. I hope anyone could help me to fix my issue.
Many thanks in advance.
I noticed you call <?php posts_nav_link(); ?> twice. I am not sure if this is an error because I don't have much to work with here.
I would take a look at the Wordpress Codex for guidance. An example they provide that might help is:
<div style="text-align:center;">
<?php posts_nav_link( ' · ', 'previous page', 'next page' ); ?>
</div>

Limiting feed to only certain categories

I'm using the following code to pull articles up for display on my homepage, i'd like to limit it to only two or three categories - can anyone point me in the right direction?
<?php
$i = 1;
$my_categories = get_option('of_news_page');
$wp_query = new WP_Query("cat=' . $my_categories . '&posts_per_page=14");
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<?php $image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'large');
$image_url = $image_url[0];?>
<?php if($i==1) { ?>
<div class="featured_single">
<div class="featured_single_image">
<?php if($image_url) { ?><a class="image_article" href="<?php the_permalink(); ?>"><img src="<?php echo bloginfo('template_directory'); ?>/js/timthumb.php?src=<?php echo $image_url; ?>&h=170&w=255&zc=1" alt="" /></a><?php } ?>
<div class="clear"></div>
<span>Posted in : <?php the_category(', '); ?></span>
<span><?php comments_popup_link('No comments yet', '1 Comment »', '% Comments »'); ?></span>
</div>
<div class="featured_single_text">
<span><?php the_time('M j, Y') ?></span>
<h3><?php the_title(); ?></h3>
<?php wpe_excerpt('wpe_featured_single'); ?>
<p>Read More »</p>
</div>
</div>
<div class="clear"></div>
<div id="featured-posts-news">
<?php } elseif($i>1 && $i<6) { ?>
<div class="featured-post-news-container clearfix">
<?php if($image_url) { ?><img src="<?php echo bloginfo('template_directory'); ?>/js/timthumb.php?src=<?php echo $image_url; ?>&h=120&w=209&zc=1" alt="" /><?php } ?>
<h2><?php the_title(); ?></h2>
<span><?php the_time('M j, Y') ?></span>
<?php wpe_excerpt('lotf_news_page'); ?><span class="news-morelink">[ Read More → ]</span>
</div>
<?php } ?><?php $i++; ?><?php endwhile; $i=0; ?>
</div>
You can use the query_posts() function for ease, here is an example of how to pull posts from category_ids 1,3 and 5, in ascending order and 5 posts per page:
query_posts('cat=1,3,5&order=ASC&posts_per_page=5');
You can find out more here: http://codex.wordpress.org/Function_Reference/query_posts
Note, the WP_Query object you are using works in much the same way as query posts, in terms of arguments that is.

Categories