Target first post in wordpress PHP - php

Can anybody help i need to add an image to the first post in this loop but can't figure it out, so i want to go through the loop and only have an image in the first post and just display the rest as normal. Below is the code, cheers
<div class="trinary-content content ten columns">
<?php
$catObj = get_category_by_slug('news-and-views');
$category_id = $catObj->term_id;
$args = array(
'sort_order' => 'DESC',
'sort_column' => 'post_date',
'hierarchical' => 0,
'exclude' => '',
'include' => '',
'meta_key' => '',
'meta_value' => '',
'authors' => '',
// 'parent' => $post->ID,
'category' => $category_id,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3
);
$pages = get_posts($args);
?>
<div class="widget widget-newsandviews">
<h2><?php echo $catObj->name; ?></h2>
<img class="news-image" src="/wp-content/themes/quentin/images/motorbike-accident/biker-full.png" />
<div class="items">
<?php $i = 0; ?>
<?php foreach($pages as $page): ?>
<div class="item <?php if($i)echo 'last'; ?>">
<a class="item-title" href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a>
<p class="summary"><?php echo $page->post_except; ?></p>
<a class="read-more" href="<?php echo get_page_link( $page->ID ); ?>">Find out more »</a>
<div class="clear"></div>
</div>
<?php $i++; ?>
<?php endforeach; ?>
</div>
</div>
</div>

Try an if statement, you can move that block to wherever you want in your code. Also you may want to use the_post_thumbnail() to retrieve the image from the post itself
<?php foreach($pages as $page): ?>
<?php if ($i==0){ ?>
<img class="news-image" src="/wp-content/themes/quentin/images/motorbike-accident/biker-full.png" />
<?php } ?>
<div class="item <?php if($i)echo 'last'; ?>">
<a class="item-title" href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a>
<p class="summary"><?php echo $page->post_except; ?></p>
<a class="read-more" href="<?php echo get_page_link( $page->ID ); ?>">Find out more »</a>
<div class="clear"></div>
</div>
<?php $i++; ?>
<?php endforeach; ?>

Related

Wordpress loop showing blank page

I'm using the code below to display an Owl Carousel of pages that have the same parent page as the current page, it works but the first item in the loop is blank.
Any Ideas? Thanks
<?php
// [service_page_carousel]
function service_page_carousel_func() {
global $post;
$direct_parent = $post->post_parent;
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $direct_parent,
'order' => 'ASC',
'orderby' => 'menu_order',
'post__not_in' => array( $post->ID ),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) : ?>
<div class="service-page-owl-carousel owl-carousel owl-theme owl-loaded owl-drag">
<?php while ( $query->have_posts() ) : $query->the_post();
// Featured image
$feat_image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), "full", true);
?>
<div class="item">
<div class="service">
<a href="<?php the_permalink(); ?>">
<div class="featured-img" style="background-image: url(<?php echo $feat_image[0]; ?>);"></div>
</a>
<a href="<?php the_permalink(); ?>">
<h3 class="brand-secondary"><?php the_title(); ?></h3>
</a>
<div class="text"><?php the_excerpt(); ?></div>
<p class="learn-more"><a class="btn primary" href="<?php the_permalink(); ?>">Learn More</a></p>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
<?php endif;
}
add_shortcode( 'service_page_carousel', 'service_page_carousel_func' );
i think you missed to add active class if only one page/post in owl carousel
<?php
// [service_page_carousel]
function service_page_carousel_func() {
global $post;
$direct_parent = $post->post_parent;
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $direct_parent,
'order' => 'ASC',
'orderby' => 'menu_order',
'post__not_in' => array( $post->ID ),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) : ?>
<div class="service-page-owl-carousel owl-carousel owl-theme owl-loaded owl-drag">
<?php
$index = 0;
while ( $query->have_posts() ) : $query->the_post();
// Featured image
$feat_image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), "full", true);
?>
<div class="item <?php echo ($index == 0) ? 'active' : ''; ?>">
<div class="service">
<a href="<?php the_permalink(); ?>">
<div class="featured-img" style="background-image: url(<?php echo $feat_image[0]; ?>);"></div>
</a>
<a href="<?php the_permalink(); ?>">
<h3 class="brand-secondary"><?php the_title(); ?></h3>
</a>
<div class="text"><?php the_excerpt(); ?></div>
<p class="learn-more"><a class="btn primary" href="<?php the_permalink(); ?>">Learn More</a></p>
</div>
</div>
<?php endwhile;
$index++;
wp_reset_query(); ?>
</div>
<?php endif;
}
add_shortcode( 'service_page_carousel', 'service_page_carousel_func' );

Wordpress featured images won't display

I am creating a widget in wordpress that pulls through child pages content (thumbnail/Featured image, title and excerpt) from the 'About Page' and displays them on the 'Homepage'. All content except the thumbnail/featured displays correctly. The thumbnail/featured image displays in grey, is is viable in other post and in the back end. when I inspect the code the url for the image does not seem to be pulling through.
Can anyone assist with this issue?
$pgs = array(
'parent' => '344',
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => 99,
'orderby' => 'menu_order'
);
$pages = get_pages( $pgs );
echo $args['before_widget'];
?>
<div class="text-center">
<h2 class="blessed-widget-title">title</h2>
<div class="text-center" id="services">
<?php foreach( $pages as $page ) { ?>
<div class="our-services-post-box col-md-4 col-sm-6 col-xs-6" id="child-<?php the_ID(); ?>">
<a href="<?php echo get_permalink($page->ID); ?>" rel="bookmark" title="<?php echo $page->post_title; ?>">
<div class="service-thumbnail background-image" style="background-image: url(<?php echo blessed_get_attachment($page->ID) ?>) ">
<img class="image-invisible" src=" <?php echo blessed_get_attachment($page->ID); ?> ">
</div>
<h2><?php echo $page->post_title; ?></h2>
</a>
<p><?php echo get_the_excerpt($page->ID) ?></p>
</div><!--services-->
<?php } ?>
</div>
</div>
<?php
echo $args['after_widget'];
I found 2 missed semicolons.
1st was here:
<div class="service-thumbnail background-image" style="background-image: url(<?php echo blessed_get_attachment($page->ID) ?>) ">
2nd was here:
<p><?php echo get_the_excerpt($page->ID) ?></p>
Maybe it caused the problem. Also Quotes are not necessary in an url, but I added it to see what will be the result.
Try to use the following one instead.
$pgs = array(
'parent' => '344',
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => 99,
'orderby' => 'menu_order'
);
$pages = get_pages( $pgs );
echo $args['before_widget'];
?>
<div class="text-center">
<h2 class="blessed-widget-title">title</h2>
<div class="text-center" id="services">
<?php foreach( $pages as $page ) { ?>
<div class="our-services-post-box col-md-4 col-sm-6 col-xs-6" id="child-<?php the_ID(); ?>">
<a href="<?php echo get_permalink($page->ID); ?>" rel="bookmark" title="<?php echo $page->post_title; ?>">
<div class="service-thumbnail background-image" style="background-image: url('<?php echo blessed_get_attachment($page->ID); ?>') ">
<img class="image-invisible" src=" <?php echo blessed_get_attachment($page->ID); ?> ">
</div>
<h2><?php echo $page->post_title; ?></h2>
</a>
<p><?php echo get_the_excerpt($page->ID); ?></p>
</div><!--services-->
<?php } ?>
</div>
</div>
<?php
echo $args['after_widget'];
Also try to review your CSS Classes related to Featured images.

PHP Parse error: syntax error, unexpected 'else' (T_ELSE) [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 3 years ago.
I'm trying to add pagination to my [shortcut]. This is my code:
<?php }
//LatestCar Function
function carforyou_LatestCar($atts){
ob_start();?>
<div class="row">
<?php
extract( shortcode_atts(array('show' =>''), $atts ));
extract( shortcode_atts(array('brand' =>''), $atts ));
$loop = new WP_Query( array(
'post_type' => 'auto',
'auto-brand' => $brand,
'posts_per_page'=>$show,
'offset' => 0
));// $count = $loop->found_posts; echo $count;
while ($loop->have_posts()) : $loop->the_post();
global $post; ?>
<div class="col-list-3">
<div class="featured-car-list">
<div class="featured-car-img">
<a alt="<?php echo get_the_title(); ?>" title="<?php echo get_the_title(); ?>" href="<?php the_permalink();?>">
<?php if(has_post_thumbnail()):
the_post_thumbnail('carforyou_small', array('class' => 'img-responsive'));
else:
echo "<div class='is-empty-img-box'></div>";
endif;
?>
</a>
<?php carforyou_AutoType(); ?>
<div class="compare_item">
<div class="checkbox">
<button id="compare_auto_btn" onclick="<?php echo esc_js('javascript:productCompare('.$post->ID.')'); ?>"><?php esc_html_e('Compare','carforyou'); ?></button>
</div>
</div>
</div>
<div class="featured-car-content">
<h6><a title="<?php echo get_the_title(); ?>" href="<?php the_permalink(); ?>"><?php $title = get_the_title(); echo mb_strimwidth($title, 0, 30, '...'); ?></a></h6>
<div class="price_info">
<?php if(!empty($post->DREAM_auto_price)): ?>
<p class="featured-price"><?php carforyou_curcy_prefix(); ?><?php echo number_format_i18n(esc_html($post->DREAM_auto_price)); ?></p>
<?php endif; ?>
<div class="car-location">
<?php $term_list = wp_get_post_terms($post->ID, 'auto-location', array("fields" => "all"));
foreach($term_list as $term_single)
$location = $term_single->name;
?>
<?php if(!empty($location)): ?>
<span><i class="fa fa-map-marker" aria-hidden="true"></i> <?php echo esc_html($location); ?> </span>
<?php endif; ?>
</div>
</div>
<ul>
<?php carforyou_featuredList(); ?>
</ul>
</div>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
<?php }
Above code using [latestcars show="10" brand="ford"] displays 10 cars made by ford. I want to add pagination, so I have added folllwing modification:
<?php }
//LatestCar Function
function carforyou_LatestCar($atts){
ob_start();?>
<div class="row">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
extract( shortcode_atts(array('show' =>''), $atts ));
extract( shortcode_atts(array('brand' =>''), $atts ));
$loop = new WP_Query( array(
'post_type' => 'auto',
'auto-brand' => $brand,
'paged' =>$paged,
'posts_per_page'=>$show,
'offset' => 0
));
// $count = $loop->found_posts; echo $count;
while ($loop->have_posts()) : $loop->the_post();
global $paged, $post;
?>
<div class="col-list-3">
<div class="featured-car-list">
<div class="featured-car-img">
<a alt="<?php echo get_the_title(); ?>" title="<?php echo get_the_title(); ?>" href="<?php the_permalink();?>">
<?php if(has_post_thumbnail()):
the_post_thumbnail('carforyou_small', array('class' => 'img-responsive'));
else:
echo "<div class='is-empty-img-box'></div>";
endif;
?>
</a>
<?php carforyou_AutoType(); ?>
<div class="compare_item">
<div class="checkbox">
<button id="compare_auto_btn" onclick="<?php echo esc_js('javascript:productCompare('.$post->ID.')'); ?>"><?php esc_html_e('Compare','carforyou'); ?></button>
</div>
</div>
</div>
<div class="featured-car-content">
<h6><a title="<?php echo get_the_title(); ?>" href="<?php the_permalink(); ?>"><?php $title = get_the_title(); echo mb_strimwidth($title, 0, 30, '...'); ?></a></h6>
<div class="price_info">
<?php if(!empty($post->DREAM_auto_price)): ?>
<p class="featured-price"><?php carforyou_curcy_prefix(); ?><?php echo number_format_i18n(esc_html($post->DREAM_auto_price)); ?></p>
<?php endif; ?>
<div class="car-location">
<?php $term_list = wp_get_post_terms($post->ID, 'auto-location', array("fields" => "all"));
foreach($term_list as $term_single)
$location = $term_single->name;
?>
<?php if(!empty($location)): ?>
<span><i class="fa fa-map-marker" aria-hidden="true"></i> <?php echo esc_html($location); ?> </span>
<?php endif; ?>
</div>
</div>
<ul>
<?php carforyou_featuredList(); ?>
</ul>
</div>
</div>
</div>
<?php endwhile;
carforyou_pagination();
else:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
extract( shortcode_atts(array('show' =>''), $atts ));
extract( shortcode_atts(array('brand' =>''), $atts ));
$loop = new WP_Query( array(
'post_type' => 'auto',
'auto-brand' => $brand,
'paged' =>$paged,
'posts_per_page'=>$show,
'offset' => 0
));
while ($loop->have_posts()) : $loop->the_post();
?>
<div class="col-list-3">
<div class="featured-car-list">
<div class="featured-car-img">
<a alt="<?php echo get_the_title(); ?>" title="<?php echo get_the_title(); ?>" href="<?php the_permalink();?>">
<?php if(has_post_thumbnail()):
the_post_thumbnail('carforyou_small', array('class' => 'img-responsive'));
else:
echo "<div class='is-empty-img-box'></div>";
endif;
?>
</a>
<?php carforyou_AutoType(); ?>
<div class="compare_item">
<div class="checkbox">
<button id="compare_auto_btn" onclick="<?php echo esc_js('javascript:productCompare('.$post->ID.')'); ?>"><?php esc_html_e('Compare','carforyou'); ?></button>
</div>
</div>
</div>
<div class="featured-car-content">
<h6><a title="<?php echo get_the_title(); ?>" href="<?php the_permalink(); ?>"><?php $title = get_the_title(); echo mb_strimwidth($title, 0, 30, '...'); ?></a></h6>
<div class="price_info">
<?php if(!empty($post->DREAM_auto_price)): ?>
<p class="featured-price"><?php carforyou_curcy_prefix(); ?><?php echo number_format_i18n(esc_html($post->DREAM_auto_price)); ?></p>
<?php endif; ?>
<div class="car-location">
<?php $term_list = wp_get_post_terms($post->ID, 'auto-location', array("fields" => "all"));
foreach($term_list as $term_single)
$location = $term_single->name;
?>
<?php if(!empty($location)): ?>
<span><i class="fa fa-map-marker" aria-hidden="true"></i> <?php echo esc_html($location); ?> </span>
<?php endif; ?>
</div>
</div>
<ul>
<?php carforyou_featuredList(); ?>
</ul>
</div>
</div>
</div>
<?php endwhile;
carforyou_pagination();
endif;
}
?>
Above solution returns following error code which I can't trace:
PHP Parse error: syntax error, unexpected
'else' (T_ELSE) in
/var/www/clients/clientxxx/webxxx/web/wp-content/themes/xxxxxx/functions/basic-functions.php
on line 439
Line 439 is "else:"
<?php endwhile;
carforyou_pagination();
else:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
(...)
Any advice? Thanks
Strip out the HTML tags and it becomes pretty clear that your if...else control structure is not correct. The else in question has no corresponding if structure before it.
while ($loop->have_posts()) : $loop->the_post();
global $paged, $post;?>
<?php if(has_post_thumbnail()): //OPEN IF ONE
the_post_thumbnail('carforyou_small', array('class' => 'img-responsive'));
else:
echo "<div class='is-empty-img-box'></div>";
endif; //CLOSE IF ONE
?>
<?php carforyou_AutoType(); ?>
<?php if(!empty($post->DREAM_auto_price)): ?> //OPEN IF TWO
<?php endif; ?> //CLOSE IF TWO
<?php $term_list = wp_get_post_terms($post->ID, 'auto-location', array("fields" => "all"));
foreach($term_list as $term_single)
$location = $term_single->name;
?>
<?php if(!empty($location)): ?> //OPEN IF THREE
<?php endif; ?> //CLOSE IF THREE
<?php carforyou_featuredList(); ?>
<?php endwhile;
carforyou_pagination();
else: //SO WHERE IS IF FOUR???
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
extract( shortcode_atts(array('show' =>''), $atts ));
...

Post page pagination not working - wordpress WP-PageNavi

I am having trouble with wordpress post page with pagination. I have installed a pagination plugin (WP-PageNavi), and I have being able to print all my posts so far, but pagination just shows one page (the present one). How can I make pagination work properly?
<?php
// args query
$args = array(
'post_type' => 'post',
'order' => 'DESC'
);
// custom query
$recent_posts = new WP_Query($args);
// check that we have results
if($recent_posts->have_posts()) : ?>
<ul class="article_list">
<?php
// start loop
while ($recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<li class="regular">
<a href="<?php echo get_permalink(); ?>">
<div class="text">
<p class="category"><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></p>
<h3 class="article_title"><?php echo mb_strimwidth(get_the_title(), 0, 80, '...'); ?></h3>
<p class="date"><?php echo get_the_date( 'Y-m-d' ); ?></p>
</div>
<div class="mask">
<img src="<?php the_post_thumbnail_url();?>" alt="" class="art_img">
</div>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif;
// reset query
wp_reset_postdata();
?>
<?php include($path.'libs/pagination.php'); ?>
Here is my pagination.php file:
<div class="pagination">
<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
</div>
I guess you have put wp_pagenavi(); function to libs/pagination.php.
So, replace it with
wp_pagenavi(array( 'query' => $recent_posts ));
So, final code should look like,
<?php
// args query
$args = array(
'post_type' => 'post',
'order' => 'DESC'
);
// custom query
$recent_posts = new WP_Query($args);
// check that we have results
if($recent_posts->have_posts()) : ?>
<ul class="article_list">
<?php
// start loop
while ($recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<li class="regular">
<a href="<?php echo get_permalink(); ?>">
<div class="text">
<p class="category"><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></p>
<h3 class="article_title"><?php echo mb_strimwidth(get_the_title(), 0, 80, '...'); ?></h3>
<p class="date"><?php echo get_the_date( 'Y-m-d' ); ?></p>
</div>
<div class="mask">
<img src="<?php the_post_thumbnail_url();?>" alt="" class="art_img">
</div>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif;
echo '<div class="pagination">';
if(function_exists('wp_pagenavi')) { wp_pagenavi(array( 'query' => $recent_posts )); }
echo '</div>';
wp_reset_postdata();
?>
I found the problem in my code:
I was not adding anything for the pagination in my query, so it was returning all my posts.
So I added a pagination logic to my custom query and it did work.
<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$custom_args = array(
'post_type' => 'post',
'order' => 'DESC',
'posts_per_page' => 5,
'paged' => $paged
);
// custom query
$recent_posts = new WP_Query($custom_args);
// check that we have results
if($recent_posts->have_posts()) : ?>
<ul class="article_list">
<?php
// start loop
while ($recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<li class="regular">
<a href="<?php echo get_permalink(); ?>">
<div class="text">
<p class="category"><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></p>
<h3 class="article_title"><?php echo mb_strimwidth(get_the_title(), 0, 80, '...'); ?></h3>
<p class="date"><?php echo get_the_date( 'Y-m-d' ); ?></p>
</div>
<div class="mask">
<img src="<?php the_post_thumbnail_url();?>" alt="" class="art_img">
</div>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif;
echo '<div class="pagination">';
if(function_exists('wp_pagenavi')) { wp_pagenavi(array( 'query' => $recent_posts )); }
echo '</div>';
wp_reset_postdata();
?>

Wordpress show featured image sorted by subtitle

I try to sort this array by the subtitle. I want to show only featured images, where the subtitle is "anwesend". I tried different things, but nothing is working. Could you please support me in this task?
here is the code of this array.
<?php
$args = array(
'parent' => $post->ID,
'post_type' => 'page',
'post_status' => 'publish',
);
$pages = get_pages($args); ?>
<div style="max-width:1240px;">
<?php foreach( $pages as $page ) { ?>
<div class="box">
<a href="<?php echo get_permalink($page->ID); ?>" rel="bookmark" title="<?php echo $page->post_title; ?>">
<span class="tagesplan_title"><?php echo $page->post_title; ?></span>
<center><span class="thumbnail_box"><?php echo get_the_post_thumbnail($page->ID, 'small-thumb'); ?></span></center>
<span class="status2"><?php echo get_the_subtitle($page); ?></span>
<span class="desc"><?php echo get_post_meta($page->ID, 'desc', true); ?></span>
</a>
</div>
<?php } ?>

Categories