Error accessing my widgets.php in wordpress [duplicate] - php

I have been trying to show the posts of my website in a grid view in one of the category page. For doing so, i implemented the following code:
<div class="row-fluid">
<div class="span6 featured-block">
<?php while($block1Featured->have_posts()): $block1Featured->the_post(); ?>
<a class="featured_image" href="<?php the_permalink();?> "target="_blank">
<?php if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) {
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'large');
$image_url = $image_url[0];
if(!#getimagesize($image_url)){
$image_url = "/wp-content/uploads/2014/02/logo2.jpg";
}
$blogurl = get_bloginfo('url');
$image_url = str_replace($blogurl, '', $image_url);
?>
<img src="<?php echo bloginfo('template_url'); ?>/thumb.php?src=<?php echo ($image_url); ?>&w=390&h=216&zc=1&q=100" alt="<?php echo the_title(); ?>" />
<?php }
else { ?>
<img src="<?php echo bloginfo('template_url'); ?>/thumb.php?src=<?php echo "http://www.nepaljapan.com/wp-content/uploads/2014/02/logo2.jpg"; ?>&w=390&h=216&zc=1&q=100" alt="<?php echo the_title(); ?>" />
<?php }?>
</a>
<a href="<?php the_permalink();?>" target="_blank">
<h2><?php the_title(); ?></h2></a>
<p><?php echo excerpt(37); ?>
<?php endwhile; ?>
</div>
</div>
But i am facing the problem with this code.
This gives out the error as
Fatal error: Call to a member function have_posts() on a non-object in /home/npjp/public_html/wp-content/themes/nepaljapan/content-category.php on line 15
The line 15 on my code is
<?php while($block1Featured->have_posts()): $block1Featured->the_post(); ?>
What is the problem? Am i actually doing it correct??
Much confused.
Please help me.
Thank you in advance

Your custom query has some problems. You can use this guid to write custom queries in wordpress:
<?php
$block1Featured= new WP_Query( 'category_name=staff+news' );
if ( $block1Featured->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $block1Featured->have_posts() ) : $block1Featured->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Related

trying to insert php into img src attribute to display featured image on custom post archive page in Wordpress

The image is set to return a URL in the Custom Fields Plugin however it is just coming up with img src Unknown, everything else works on the page except this... code is as follows: Thanks in advance for any help!
<?php get_header(); ?>
<?php
$args = array(
'post_type' => 'artists',
);
$query = new WP_Query( $args );
?>
<section class="row artists">
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col-sm-12 col-md-6 col-lg-3">
<a href="<?php if( get_post_field('artist_website') ) { ?>
<?php echo $artist_website; ?>
<?php } else { the_permalink(); } ?>">
<img src="<?php get_post_field('artist_feature_image'); ?>" alt="<?php echo the_title() ; ?>">
<p><?php echo the_title() ;?></p>
</a>
</div>
<?php endwhile; endif; wp_reset_postdata(); ?>
</section>
<?php get_footer(); ?>
The problem is that get_post_field('artist_feature_image') only return the image.
You need to display it with "echo"
<img src="<?php echo get_post_field('artist_feature_image'); ?>" />
And don't do echo the_title() ;, because the_title() is already doing an echo on get_the_title();
function the_title() {
echo get_the_title();
}
So if you want to display it you just have to do: the_title();

Wordpress custom field - css in loop

I try to insert the value of a custom field in a background-image property ( therefore not in img src="..."').
In my category.php; I can display the custom field linked to each post; but when I put the variable in my style ( inline css ), wordpress always displays the same image.
The code :
<?php
// The Loop
while ( have_posts() ) : the_post(); ?>
<div class="interview">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php $photo_interview = get_field('photo_apercu', $post->ID); ?>
<?php echo $photo_interview; ?>
<?php the_title(); ?>
<style type="text/css">
.photo_interview {
background-image: url(<?php echo $photo_interview; ?>);
}
</style>
<div class="photo_interview"></div>
</a>
</div>
<?php endwhile;
else: ?>
<?php endif; ?>
Any idea ? My page here : http://www.overso.me/category/interview/
Your code should be this:
<?php
// The Loop
while ( have_posts() ) : the_post(); ?>
<div class="interview">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php $photo_interview = get_field('photo_apercu', $post->ID); ?>
<?php echo $photo_interview; ?>
<?php the_title(); ?>
<!-- You don't need style tags if you only want to set the background image -->
<div style="background-image: url(<?php echo $photo_interview; ?>)"></div>
</a>
</div>
<?php endwhile;
else: ?>
<?php endif; ?>
For what I can see you haven't set the global $post, so get_field do not know which is needed to display. in this case it will be better to use the_ID() to get the current post ID inside the while loop.
Cheers

How to show text if no posts from query?

I have a custom WordPress post type, and want to make so if there are no posts in the query to display, it shows a message of saying no posts were found, I've tried a few suggestions but nothing seems to be working so far.
Full Code of the relevant section:
<?php
$k=0;
$args = array(
'post_type'=>'portfolios',
'posts_per_page'=>5,
'order'=>'DESC'
);
$the_query = new WP_Query($args);
while($the_query->have_posts()):$the_query->the_post();
$k++;
if($k==1){
$color= 'gray';
}else if($k==2){
$color= 'blue';
}else if($k==3){
$color= 'green';
}else if($k==4){
$color= 'light-blue';
}else if($k==5){
$color= 'yellow';
}
?>
<div itemprop="exampleOfWork" class="col-xs-12 col-sm-4 portfolio-item">
<div class="portfolio-holder">
<?php
if(has_post_thumbnail()){
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'singleport',true);
?>
<figure><a itemprop="url" href="<?php the_permalink();?>"><img src="<?php echo $image_url[0];?>" alt="Portfolio image" /></a></figure>
<?php }else{ ?>
<figure><a itemprop="url" href="<?php the_permalink();?>"><img src="<?php echo esc_url( get_template_directory_uri() );?>/assets/images/noimage.jpg" alt="No image" /></a></figure>
<?php } ?>
<h6 itemprop="name"><?php the_title(); ?></h6>
<span itemprop="genre"><?php the_field('name');?></span>
<div class="transparentbg <?php echo $color; ?>">
<?php
$image = get_field('logo');
if( !empty($image) ): ?>
<a itemprop="url" href="<?php the_permalink();?>">
<img src="<?php echo $image['url']; ?>" alt="logo" />
</a>
<?php endif; ?>
<a href="<?php the_permalink();?>" class="info">
<img itemprop="image" src="<?php echo esc_url( get_template_directory_uri() );?>/assets/images/port-hover-bg.png" alt="Port hover bg" /><br />
<img itemprop="image" src="<?php echo esc_url( get_template_directory_uri() );?>/assets/images/search-icon.png" height="20" width="20" alt="Search icon">
</a>
</div>
</div>
</div>
<?php endwhile;wp_reset_query();?>
</div>
</div>
</section>
How can we make it so, if there's no posts, it echo's a message?
Add a check -
if (!empty($the_query->have_posts())) {
while(...) {...}
} else {
echo "No Posts!";
}
Issue
You want to use WP Query class for fetching a particular type of posts ( based on custom fields ), and if it returns no posts at all, it should display a message.
Solution
$the_query = new WP_Query( $args );
Where "$args" is your list of parameters for WP_Query constructor.
Now $the_query is an object of class type WP-Query. Hence it contains a function that will return whether there is any post matching your particular query. If there is at least one post, it will return True, else it will return False.
That function to check if there are any posts matching your queries is have_posts() .
Usage is ObjectName->have_posts();
in your specific case it is
$the_query->have_posts();
This will return true or false based on the query ( ie, arguments you supply using $arg variable ).
hence:
if($the_query->have_posts()) {
//There are posts matching the query
}
else
{
//There are no posts matching the query
}
You can use the below code ( You will have to modify this, but still a good start ).
<?php
$args = array(
'post_type'=>'portfolios',
'posts_per_page'=>5,
'order'=>'DESC'
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Reference : https://codex.wordpress.org/Class_Reference/WP_Query
You need to add if condition before while loop like that way:
<?php
$k=0;
$args = array(
'post_type'=>'portfolios',
'posts_per_page'=>5,
'order'=>'DESC'
);
$the_query = new WP_Query($args);
if( $the_query->have_posts() ) {
while($the_query->have_posts()):
// your code
<?php endwhile;
} else {
echo "No data found.";
}
wp_reset_query();
?>
<?php
$k=0;
$args = array(
'post_type'=>'portfolios',
'posts_per_page'=>5,
'order'=>'DESC'
);
$the_query = new WP_Query($args);
if($the_query->have_posts()):
while($the_query->have_posts()):$the_query->the_post();
$k++;
if($k==1){
$color= 'gray';
}else if($k==2){
$color= 'blue';
}else if($k==3){
$color= 'green';
}else if($k==4){
$color= 'light-blue';
}else if($k==5){
$color= 'yellow';
}
?>
<div itemprop="exampleOfWork" class="col-xs-12 col-sm-4 portfolio-item">
<div class="portfolio-holder">
<?php
if(has_post_thumbnail()){
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'singleport',true);
?>
<figure><a itemprop="url" href="<?php the_permalink();?>"><img src="<?php echo $image_url[0];?>" alt="Portfolio image" /></a></figure>
<?php }else{ ?>
<figure><a itemprop="url" href="<?php the_permalink();?>"><img src="<?php echo esc_url( get_template_directory_uri() );?>/assets/images/noimage.jpg" alt="No image" /></a></figure>
<?php } ?>
<h6 itemprop="name"><?php the_title(); ?></h6>
<span itemprop="genre"><?php the_field('name');?></span>
<div class="transparentbg <?php echo $color; ?>">
<?php
$image = get_field('logo');
if( !empty($image) ): ?>
<a itemprop="url" href="<?php the_permalink();?>">
<img src="<?php echo $image['url']; ?>" alt="logo" />
</a>
<?php endif; ?>
<a href="<?php the_permalink();?>" class="info">
<img itemprop="image" src="<?php echo esc_url( get_template_directory_uri() );?>/assets/images/port-hover-bg.png" alt="Port hover bg" /><br />
<img itemprop="image" src="<?php echo esc_url( get_template_directory_uri() );?>/assets/images/search-icon.png" height="20" width="20" alt="Search icon">
</a>
</div>
</div>
</div>
<?php
endwhile;
else:
echo "Your message goes here";
endif;
wp_reset_query();
?>
</div>
</div>
</section>
Adding if condition will do it.

Check for Wordpress Thumbnail and display dummy if not there

got another question to ask:
I would like to display post thumbnails with the post title underneath. I have managed to work that out through asking on here, however now I would like to add a function that checks for a thumbnail and if none is available displays a dummy image.
Here is my try, which does display the thumbnail (if there) but not the dummy (if no thumbnail is attached):
<div class="gallery_container_wrapper">
<?php query_posts( $args ); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="gallery_image_container">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<div class="gallery_image_thumb">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail'); }
else {
echo
'<img src="http://www.kunstamkasten.de/wp-content/uploads/2014/08/gallery_dummy.jpg" />'
; } ?>
</div>
<div class="gallery_title">
<h2>
<?php the_title(); ?>
</h2>
</div>
</a>
</div>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
What am I doing wrong here?
This code works for me. Yours is virtually identical.
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail(); ?>
<?php else : ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/sunlit_path_banner.jpg" alt="Sunlit Path" />
<?php endif; ?>
Note: I also cut and pasted your code and tested on my WP install, and it worked fine.
Or... try this alternative to the if condition:
<?php if ( get_the_post_thumbnail( get_the_ID() ) ) {
the_post_thumbnail('thumbnail'); }
else {
echo
'<img src="http://www.kunstamkasten.de/wp-content/uploads/2014/08/gallery_dummy.jpg" />'
; } ?>

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