How to display certain categories based on id - php

I have code like this:
<div class="featured-title"><h3>Paket Lainnya</h3></div>
<div class="boxer2">
<?php $this_post = $post;
$category = get_the_category();
$category = $category[0];
$category = $category->cat_ID;
$posts = get_posts('numberposts=7&offset=0&orderby=rand&order=DESC&category='.$category);
$count = 0;
foreach ($posts as $post) {
if ($post->ID == $this_post->ID || $count == 6) {
unset($posts[$count]);
} else {
$count++;
}
}
?>
<?php if ( $posts ) : ?>
<?php foreach ( $posts as $post ) : ?>
<div class="col-md-4"><?php get_template_part('thumb'); ?></div>
<?php endforeach ?>
<?php endif ?>
<?php wp_reset_query(); ?>
</div>
What do I expect?
I want to change the code to display the results of a certain category by entering the category id.
I have set up the category id that is 9.
Full code:
<?php get_header(); ?>
<?php
if ((is_home())&& ($paged < 1)) {
get_template_part('home-featured');
}
?>
<div class="wisata-konten">
<div class="container">
<div class="row">
<div class="col-md-12">
<?php
$query = new WP_Query(array('cat' => 9));
if ($query->have_posts()) {
while ($query->have_posts()) {
<div class="featured-title"><h3>Paket Lainnya</h3></div>
<div class="boxer2">
<?php
$this_post = $post;
$category = get_the_category(); $category = $category[0]; $category = $category->cat_ID;
$posts = get_posts('numberposts=7&offset=0&orderby=rand&order=DESC&category='.$category);
$count = 0;
foreach ($posts as $post) {
if ($post->ID == $this_post->ID || $count == 6) {
unset($posts[$count]);
} else {
$count++;
}
}
?>
<?php if ( $posts ) : ?>
<?php foreach ( $posts as $post ) : ?>
<div class="col-md-4"><?php get_template_part('thumb'); ?></div>
<?php endforeach ?>
<?php endif ?>
<?php wp_reset_query(); ?>
</div>
</div>
</div>
</div>
}
}
?>
<div class="wisata-testimoni">
<div class="container">
<div class="row">
<div class="col-md-12">
<div id="testimoni" class="carousel slide">
<div class="carousel-inner">
<?php fastestwp_comments(); ?>
</div>
</div>
<div class="tombol"><a class="medium beli blue pull-right" href="<?php echo home_url(); ?>/testimoni">Lihat Semua Testimoni <span class="glyphicon glyphicon-thumbs-up"></span></a></div>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>

Go through the below code, this may help you
<?php
$query = new WP_Query( array( 'cat' => 9 ) );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
//Your code here
}
}
?>

I have cleaned Your code check it
<?php get_header(); ?>
<?php if((is_home())&& ($paged < 1)) { ?>
<?php get_template_part( 'home-featured' ); ?>
<?php } ?>
<div class="wisata-konten">
<div class="container">
<div class="row">
<div class="col-md-12">
<?php
$query = new WP_Query( array( 'cat' => 9 ) );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) { ?>
<div class="featured-title"><h3>Paket Lainnya</h3></div>
<div class="boxer2">
<?php $this_post = $post;
$category = get_the_category(); $category = $category[0]; $category = $category->cat_ID;
$posts = get_posts('numberposts=7&offset=0&orderby=rand&order=DESC&category='.$category);
$count = 0;
foreach ( $posts as $post ) {
if ( $post->ID == $this_post->ID || $count == 6) {
unset($posts[$count]);
}else{
$count ++;
}
}
?>
<?php if ( $posts ) : ?>
<?php foreach ( $posts as $post ) : ?>
<div class="col-md-4"><?php get_template_part( 'thumb' ); ?></div>
<?php endforeach; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
</div>
</div>
</div>
<?php }
}
?>
<div class="wisata-testimoni">
<div class="container">
<div class="row">
<div class="col-md-12">
<div id="testimoni" class="carousel slide">
<div class="carousel-inner">
<?php fastestwp_comments(); ?>
</div>
</div>
<div class="tombol"><a class= "medium beli blue pull-right" href="<?php echo home_url() ; ?>/testimoni" >Lihat Semua Testimoni <span class="glyphicon glyphicon-thumbs-up"></span></a></div>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>

Related

WP_Query and variables in php (WordPress)

I have a set_gallery function and I pass the set_args array to her. The set_args array always changes. I'm looking for a solution for deleting posts posted and so that the loop outputs posts with new parameters. How can this be realized? Thanks...
function set_gallery($set_args = '') { ?>
<?php if ($set_args == '') {
$args = array (
'post_type' => 'gallerys',
'posts_per_page' => '-1'
);
} else {
$args = $set_args;
} ?>
<?php $gallerys_posts = new WP_Query($args);
wp_reset_query();
if( $gallerys_posts->have_posts() ) :
while ( $gallerys_posts->have_posts() ) :
$gallerys_posts->the_post(); ?>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12 shuffle-item filtered">
<div class="portfolio-item">
<a href="<?php echo get_permalink(get_the_id());?>">
<?php if ( get_the_post_thumbnail(get_the_id()) ) { ?>
<?php echo get_the_post_thumbnail( get_the_id(), array(620, 423 )); ?>
<?php } ?>
<div class="portfolio-overlay">
<div class="caption">
<?php the_title(); ?>
<span>
<?php echo get_the_content(); ?>
</span>
</div>
</div>
</a>
</div>
</div>
<?php endwhile; endif; wp_reset_query();?>
<?php }

wordpress loop show pending posts even though $count_posts->publish;

I have an Easy Digital Downloads archive page the shows published downloads, but also pending downloads although its not suppose to.
for some reason, the wp_count_posts() doesn't affect on the loop, and I can see also pending posts in the archive page. I cant figure out why.
<?php
$count_posts = wp_count_posts('download');
$published_posts = $count_posts->publish;
$key = 0;
?>
<?php if (have_posts()) : ?>
<div class="all_product">
<div class="row">
<?php while (have_posts()) : the_post();
if ($key < 12) {
?>
<div class="col-lg-3 col-md-4 col-sm-12 single-download-list">
<?php get_template_part('content-grid', 'download'); ?>
</div>
<?php
}
$key++;
endwhile; ?>
</div>
</div>
<?php else : ?>
<?php
$args= array(
'post_type' => 'download',
'post_status' => 'publish'
)
$posts = get_posts($args);
$key = 0;
?>
<?php if ($posts) : ?>
<div class="all_product">
<div class="row">
<?php foreach($posts as $post) setup_postdata($post );
if ($key < 12) {
?>
<div class="col-lg-3 col-md-4 col-sm-12 single-download-list">
<?php get_template_part('content-grid', 'download'); ?>
</div>
<?php
}
$key++;
endforeach;
endif;
?>
</div>
</div>
<?php
endif;
wp_reset_postdata();?>
?>
<?php else : ?>

add badge to thumbnail image

What I want to do on the front page where I have recent posts, is to add a small upper corner badge or ribbon that says "NEW" to the thumbnails of posts that were published on today's date. Below is my index.php which controls the front page layout:
<?php
/**
* The main template file.
*
* Used to display the homepage when home.php doesn't exist.
*/
?>
<?php
// Get options
$mts_options = get_option(MTS_THEME_NAME);
// Featured Section 1 settings ( slider is here) --------------------------------
$slider_enabled = ( $mts_options['mts_featured_slider'] === '1' ) ? true : false;
if ( empty($mts_options['mts_featured_slider_cat']) || !is_array($mts_options['mts_featured_slider_cat']) ) {
$mts_options['mts_featured_slider_cat'] = array('0');
}
$slider_cat = implode(",", $mts_options['mts_featured_slider_cat']);
// Featured Section settings --------------------------------------------------
$f2_title = $mts_options['mts_featured_section_2_title'];
if ( empty($mts_options['mts_featured_section_2_cat']) || !is_array($mts_options['mts_featured_section_2_cat']) ) {
$mts_options['mts_featured_section_2_cat'] = array('0');
}
$f2_cat= implode(",", $mts_options['mts_featured_section_2_cat']);
// Featured Section 3 settings --------------------------------------------------
get_header(); ?>
<div id="page">
<div class="article">
<?php if ( $slider_enabled && is_home() && !is_paged() ) { ?>
<section id="featured-section-1" class="featured-section featured-section-1-1 clearfix">
<div class="slider-container">
<div class="primary-slider-container clearfix loading">
<div id="slider" class="primary-slider">
<?php
$slider_full = 'best-featuredfull';
if ( empty( $mts_options['mts_custom_slider'] ) ) {
$my_query = new WP_Query('cat='.$slider_cat.'&posts_per_page='.$mts_options['mts_featured_slider_num']);
while ( $my_query->have_posts() ) : $my_query->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail($slider_full,array('title' => '')); ?>
<div class="slider-caption">
<?php if(isset($mts_options['mts_home_headline_meta_info']['enabled']) && $mts_options['mts_home_headline_meta_info']['enabled']){ ?>
<div class="sliderdate">
<?php if(isset($mts_options['mts_home_headline_meta_info']['enabled']['date']) == '1') { ?>
<span class="thetime updated"><?php the_time( get_option( 'date_format' ) ); ?></span>
<?php } ?>
</div>
<?php } ?>
<h2 class="slide-title"><?php the_title(); ?></h2>
</div>
<div class="post-day"><?php if (function_exists('wp_review_show_total')) wp_review_show_total(true, 'review-total-only'); ?></div>
</a>
</div>
<?php endwhile; wp_reset_postdata();
} else { ?>
<?php foreach( $mts_options['mts_custom_slider'] as $slide ) : ?>
<div>
<a href="<?php echo esc_url( $slide['mts_custom_slider_link'] ); ?>">
<?php echo wp_get_attachment_image( $slide['mts_custom_slider_image'], $slider_full, false, array('title' => '') ); ?>
<div class="slider-caption">
<div class="sliderdate"><?php echo esc_html( $slide['mts_custom_slider_title'] ); ?></div>
<h2 class="slide-title"><?php echo esc_html( $slide['mts_custom_slider_text'] ); ?></h2>
</div>
</a>
</div>
<?php endforeach; ?>
<?php } ?>
</div>
</div>
</div><!-- slider-container -->
</section>
<?php } ?>
<?php if( $mts_options['mts_featured_section_2'] === '1' && is_home() && !is_paged() ) {// featured section 2 ?>
<section id="featured-section-3" class="featured-section clearfix">
<?php if (!empty($f2_title)) { ?><h4 class="featured-section-title"><?php echo $f2_title;?></h4><?php } ?>
<?php
$f2_query = new WP_Query('cat='.$f2_cat.'&posts_per_page=4');
$f2_count = 1; if ($f2_query->have_posts()) : while ($f2_query->have_posts()) : $f2_query->the_post();
$f2_loop_params = best_mixed_layout_params( 1, 2, $f2_count );// see functions.php
$f2_clear_class = $f2_loop_params['clear_class'];
$f2_type_class = $f2_loop_params['box_class'];
$f2_loop_thumb = $f2_loop_params['thumb'];
$f2_show_excerpt = $f2_loop_params['show_excerpt'];
$f2_show_author = $f2_loop_params['show_author'];
$begin_f2_extra_wrappers = $f2_loop_params['open_wrappers'];
$end_f2_extra_wrappers = $f2_loop_params['close_wrappers'];
?>
<article class="post-box latestPost mixed <?php echo $f2_type_class;?> <?php echo $f2_clear_class;?>">
<?php echo $begin_f2_extra_wrappers; ?>
<div class="post-img">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="nofollow">
<?php the_post_thumbnail($f2_loop_thumb,array('title' => '')); ?>
</a>
</div>
<div class="post-data">
<div class="post-data-container">
<header>
<h2 class="title post-title"><?php the_title(); ?></h2>
<?php mts_the_postinfo(); ?>
</header>
<?php if ($f2_show_excerpt) : ?>
<div class="post-excerpt">
<?php echo mts_excerpt(40); ?>
</div>
<?php endif; ?>
</div>
</div>
<?php echo $end_f2_extra_wrappers; ?>
</article><!--.post-box-->
<?php $f2_count++; endwhile;
endif; wp_reset_postdata();
?>
</section><!--#featured-section-3-->
<?php } ?>
<div id="content_box">
<?php $latest_posts_used = false;
if ( !empty( $mts_options['mts_featured_categories'] ) ) {
foreach ( $mts_options['mts_featured_categories'] as $section ) {
$category_id = $section['mts_featured_category'];
$featured_category_layout = $section['mts_featured_category_layout'];
$posts_num = $section['mts_featured_category_postsnum'];
if ( $category_id === 'latest' && ! $latest_posts_used ) {
$latest_posts_used = true;
$fc_section_class = ( in_array( $featured_category_layout, array( 'vertical', 'mixed' ) ) ) ? '' : ' '.$featured_category_layout;
?>
<section id="latest-posts" class="clearfix<?php echo $fc_section_class ?><?php //echo $fc_section_no_gap ?>">
<h4 class="featured-section-title"><?php _e( "Latest", "best" ); ?></h4>
<?php $j = 1; if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php best_the_homepage_article( $featured_category_layout, $j, true );?>
<?php $j++; endwhile; endif; ?>
<!--Start Pagination-->
<?php if (isset($mts_options['mts_pagenavigation_type']) && $mts_options['mts_pagenavigation_type'] == '1' ) { ?>
<?php $additional_loop = 0; mts_pagination($additional_loop['max_num_pages']); ?>
<?php } else { ?>
<div class="pagination pagination-previous-next">
<ul>
<li class="nav-previous"><?php next_posts_link( '<i class="fa fa-chevron-left"></i> '. __( 'Previous', 'best' ) ); ?></li>
<li class="nav-next"><?php previous_posts_link( __( 'Next', 'best' ).' <i class="fa fa-chevron-right"></i>' ); ?></li>
</ul>
</div>
<?php } ?>
<!--End Pagination-->
</section><!--#latest-posts-->
<?php } elseif ( $category_id !== 'latest' && !is_paged() ) {
$fc_section_class = ( in_array( $featured_category_layout, array( 'vertical', 'mixed' ) ) ) ? '' : ' '.$featured_category_layout;
?>
<section class="featured-section clearfix<?php echo $fc_section_class ?>">
<h4 class="featured-section-title"><?php echo get_cat_name($category_id); ?></h4>
<?php $cat_query = new WP_Query('cat='.$category_id.'&posts_per_page='.$posts_num); ?>
<?php $j = 1; if ($cat_query->have_posts()) : while ($cat_query->have_posts()) : $cat_query->the_post(); ?>
<?php best_the_homepage_article( $featured_category_layout, $j );?>
<?php $j++; endwhile; endif; wp_reset_postdata();?>
</section>
<?php } ?>
<?php } ?>
<?php } ?>
</div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
What I have is this: to check the date and display the badge (not including css) but not sure where to input it exactly:
<?php
$days_old = (current_time(timestamp) - get_the_time('U') - (get_settings('gmt_offset') *3600 ) ) / (60*60*24);
if ($days_old <=1) {
echo <div class="post-badge">NEW</div>;
}
?>
I want to insert it into The Latest Post Section. Any help would be greatly appreciated:)

Wordpress Timeline Show post Monthly

I am fetching data through custom post type and category. I have added 12 category Jan to Dec and Jan has 1 posts Feb have 2 post
What I am struggling to do on January post the 2 circle is showing on the left I want only one January Circle rest of the January post under the circle.
Can you please how can i put check on category?
here is the site http://novartis.portlandvault.com/timeline/
Thanks
<div id="timeline">
<?php
//Define your custom post type name in the arguments
$args = array('post_type' => 'timeline', 'order' => 'asc' );
//Define the loop based on arguments
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
$thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'large') );
$category = get_the_terms($id, 'timeline_categories');
?>
<div class="timeline-item">
<div class="timeline-icon">
<div class="timeline-month">
<?php echo $category[0]->name;?>
</div>
</div>
<div class="timeline-content right">
<h2>
<?php the_title(); ?>
</h2>
<p>
<?php echo the_content(); ?>
</p>
<div class="timeline_img">
<img src="<?php echo $thumb; ?>" class="img-responsive">
</div>
</div>
</div>
<?php endwhile;?>
</div>
<!-- Timeline ends here -->
I am not sure using categories is the best way to do this. You are limiting yourself to one years worth of data. I would suggest actually using the post date to separate the posts, then your code could look something like this.
<div id="timeline">
<?php
//Define your custom post type name in the arguments
$args = array('post_type' => 'timeline', 'order' => 'asc' );
//Define the loop based on arguments
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
$thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'large') );
$category = get_the_terms($post->ID, 'timeline_categories');
?>
<section id="<?php echo $post->ID; ?>">
<?php
if( $loop->current_post === 0 ) {
$current_quarter = $category[0]->name; ?>
<div class="quarterlyheading">
<?php echo $current_quarter; ?>
<div class="quarterlinebreak"><hr></div>
</div>
<?php } else {
$post_quarter = $category[0]->name;
if($current_quarter != $post_quarter) { ?>
<div class="quarterlyheading">
<?php echo $post_quarter; ?>
<div class="quarterlinebreak"><hr></div>
</div>
<?php }
}
$current_quarter = $post_quarter;
?>
<div class="timeline-item">
<?php
if( $loop->current_post === 0 ) {
$current_month = get_the_time('M'); ?>
<div class="timeline-icon">
<div class="timeline-month">
<?php echo $current_month; ?>
</div>
</div>
<?php } else {
$post_month = get_the_time('M');
if($current_month != $post_month) { ?>
<div class="timeline-icon">
<div class="timeline-month">
<?php echo $post_month; ?>
</div>
</div>
<?php }
}
$current_month = $post_month;
?>
<div class="timeline-content right">
<h2>
<?php the_title(); ?>
</h2>
<p>
<?php echo the_content(); ?>
</p>
<div class="timeline_img">
<img src="<?php echo $thumb; ?>" class="img-responsive">
</div>
</div>
</div>
</section>
<?php endwhile;?>
</div>

Wordpress limit posts per page with pagination

I want to create blog page with recent posts and pagination. Code below shows recent posts but pagination doesn't want to work.
<?php get_header(); ?>
<div class="container clearfix">
<div id="content" class="clearfix">
<?php
$postslist = get_posts('numberposts=-1&posts_per_page=5&order=DESC&orderby=date');
foreach ($postslist as $post) :
setup_postdata($post);
?>
<div class="entry">
<div class="recent-post-thumbnail">
<?php echo the_post_thumbnail($recent->ID, 'thumbnail'); ?>
</div>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
<h4>More ></h4>
</div>
<?php endforeach; ?>
</div> <!-- end content -->
<div class="kreska-pion"></div>
<div class="sidebar">
<?php get_sidebar(); ?>
</div>
</div>
<?php get_footer(); ?>
numberposts=6 replace this with post_per_page and let me know if It worked .
<?php
$postlist = get_posts( 'numberposts=-1&posts_per_page=5' );
$posts = array();
foreach ( $postlist as $post ) {
$posts[] += $post->ID;
}
$current = array_search( get_the_ID(), $posts );
$prevID = $posts[$current-1];
$nextID = $posts[$current+1];
?>
<?php
foreach ( $posts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
<div class="navigation">
<?php if ( !empty( $prevID ) ): ?>
<div class="alignleft">
<a href="<?php echo get_permalink( $prevID ); ?>"
title="<?php echo get_the_title( $prevID ); ?>">Previous</a>
</div>
<?php endif;
if ( !empty( $nextID ) ): ?>
<div class="alignright">
<a href="<?php echo get_permalink( $nextID ); ?>"
title="<?php echo get_the_title( $nextID ); ?>">Next</a>
</div>
<?php endif; ?>
</div><!-- .navigation -->
I tried
<?php get_header(); ?>
<div class="container clearfix">
<div id="content" class="clearfix">
<?php
$args = array( 'post_per_page' = -1 );
$query= new WP_Query( $args );
var_dump( $query );
// The 2nd Loop
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>' . get_the_title( $query->post->ID ) . '</li>';
}
// Restore original Post Data
wp_reset_postdata();
?>
<div class="vertical"></div>
<div class="sidebar">
<?php get_sidebar(); ?>
</div>
</div>
<?php wp_pagenavi(); ?>
<?php get_footer(); ?>
But it shows a blank page.
EDIT:
Here's what I did:
<?php get_header(); ?>
<div class="container clearfix">
<div id="content" class="clearfix">
<?php
$args = array( 'post_per_page' = -1 );
$query= new WP_Query( $args );
var_dump( $query );
wp_reset_postdata();
?>
<div class="vertical"></div>
<div class="sidebar">
<?php get_sidebar(); ?></div>
</div>
<?php wp_pagenavi(); ?>
<?php get_footer(); ?>
And it shows a blank page.

Categories