I'm having a hard time implementing this into my theme. Right now the theme only displays the recent posts. I know how to display the recent post or pages however I don't know how to display both of them.
Here is the current loop code:
$nr_posts = ($sd_data['home_sidebar_hide'] == 1) ? $sd_data['home_news_posts_disabled'] : $sd_data['home_news_posts'] ;
$i = 0;
$args = array('post_type' => 'post',
'posts_per_page' => $nr_posts,
'order' => 'DESC',
'orderby' => 'date',
'post_status' => 'publish'
);
query_posts( $args );
if( have_posts() ) : while ( have_posts() ) : the_post(); $i++;
$margin_nr = ($sd_data['home_sidebar_hide'] == 1) ? 5 : 4;
if( $i == 1) {
$class = 'span3 alpha';
} else if( $i == $margin_nr) {
$i = 0;
$i++;
$class = 'span3 alpha';
} else $class = 'span3';
?>
<div class="<?php echo $class; ?>">
<div class="news-item">
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) : ?>
<?php the_post_thumbnail('recent-blog'); ?>
<?php endif; ?>
<h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_title(); ?>
</a></h3>
<p><?php echo substr(get_the_excerpt(), 0, 50); ?>...</p>
<div class="news-meta clearfix"> <span class="news-date">
<?php the_time(get_option('date_format')); ?>
</span> <span class="news-comments">
<?php comments_popup_link( '0', '1', '%', 'comments-link', 'c'); ?>
</span> <span class="news-rating"><?php echo sd_post_like_link(get_the_ID()); ?></span> </div>
</div>
</div>
<?php endwhile; wp_reset_query(); endif; ?>
CHANGE
$args = array('post_type' => 'post',
'posts_per_page' => $nr_posts,
'order' => 'DESC',
'orderby' => 'date',
'post_status' => 'publish'
);
To
$args = array('post_type' => array('post','page'),
'posts_per_page' => $nr_posts,
'order' => 'DESC',
'orderby' => 'date',
'post_status' => 'publish'
);
Refer here for more WP QUERY
Related
I'm using the code (loop) below for the blog post grid displaying. I want the following structure for my tag.php as well (the post by the tag instead of all blog posts). But I don't know how to do it. Could you, please, check it?
I can't remember how I solved the following problem in the past but I'm at a 100% sure that I found the solution previously.
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'taxonomy_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<div class="item col-sm-4">
<div class="well">
<h2><?php the_title(); ?></h2>
<span class="date"><?php echo get_the_date("j.n.Y"); ?></span>
<a href="<?php the_permalink(); ?>"><?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?></a>
<?php the_excerpt(); ?>
<div class="readmore-wrapper">
<a class="readmore" href="<?php the_permalink(); ?>">Suite</a>
</div>
</div></div>
<?php endwhile; ?>
</div>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="next-posts-link">
<?php echo get_previous_posts_link( '< Page précédente' ); ?>
</div>
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Page suivante >', $custom_query->max_num_pages ); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Please check WP_Query with Tag Parameter
https://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters
example:
$custom_query_args = array(
'taxonomy_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date', // modified | title | name | ID | rand
'tag' => 'tag1,tag2'
);
How to show count number in popular post widget?
<div class="terpopuler__num">1</div>
see full code:
<div id="terpopuler" class="terpopuler__row">
<ul class="terpopuler__wrap">
<?php $popular_days_ago = '$popular_days days ago'; $recent = new WP_Query(array( 'posts_per_page' => $number, 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_key' => 'post_views_count', 'date_query' => array( array( 'after' => $popular_days_ago )) )); while($recent->have_posts()) : $recent->the_post(); ?>
<li class="terpopuler__item">
<a href="<?php the_permalink(); ?>" rel="bookmark">
<div class="terpopuler__num">1</div><!-- count number -->
<div class="terpopuler__title">
<?php the_title(); ?>
</div>
<?php $post_views = get_post_meta($post->ID, 'post_views_count', true); if ( $post_views >= 1) { ?>
<span class="terpopuler__info"><?php hwd_post_views(); ?> kali dibaca</span>
<?php } ?>
</a>
</li>
<?php endwhile; ?>
</ul>
</div><!--widget-terpopuler-->
and see my Screanshoot
Try this, I've added $i top of the while loop, then set $i as auto increment variable ($i++).
<div id="terpopuler" class="terpopuler__row">
<ul class="terpopuler__wrap">
<?php $popular_days_ago = '$popular_days days ago'; $recent = new WP_Query(array( 'posts_per_page' => $number, 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_key' => 'post_views_count', 'date_query' => array( array( 'after' => $popular_days_ago )) ));
$i = 1;
while($recent->have_posts()) : $recent->the_post(); ?>
<li class="terpopuler__item">
<a href="<?php the_permalink(); ?>" rel="bookmark">
<div class="terpopuler__num"><?php echo $i++; ?></div><!-- count number -->
<div class="terpopuler__title">
<?php the_title(); ?>
</div>
<?php $post_views = get_post_meta($post->ID, 'post_views_count', true); if ( $post_views >= 1) { ?>
<span class="terpopuler__info"><?php hwd_post_views(); ?> kali dibaca</span>
<?php } ?>
</a>
</li>
<?php endwhile; ?>
</ul>
</div><!--widget-terpopuler-->
<div id="terpopuler" class="terpopuler__row">
<ul class="terpopuler__wrap">
<?php $i = 0; $popular_days_ago = '$popular_days days ago'; $recent = new WP_Query(array( 'posts_per_page' => $number, 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_key' => 'post_views_count', 'date_query' => array( array( 'after' => $popular_days_ago )) )); while($recent->have_posts()) : $recent->the_post(); ?>
<li class="terpopuler__item">
<a href="<?php the_permalink(); ?>" rel="bookmark">
<div class="terpopuler__num"><?php echo ++$i; ?></div><!-- count number -->
<div class="terpopuler__title">
<?php the_title(); ?>
</div>
<?php $post_views = get_post_meta($post->ID, 'post_views_count', true); if ( $post_views >= 1) { ?>
<span class="terpopuler__info"><?php hwd_post_views(); ?> kali dibaca</span>
<?php } ?>
</a>
</li>
<?php endwhile; ?>
</ul>
</div><!--widget-terpopuler-->
On my WordPress website, I created a page in which I display 9 posts of the category "interviews". Then there is a button ("Charger plus d'interviews") to click if you want to see more posts of the category.
There is a problem in the loop, because when I click on it, it displays posts from all the other categories.
What shall I do to display only the "interviews" ?
<section class="blocs" id="home_article_derniers">
<div class="iso-container">
<?php
$post_home_query = new WP_Query(array(
'post_type' => 'post',
'category_name' => 'interview',
'posts_per_page' => 9,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
'paged' => 2
));
if($post_home_query->have_posts()){
while ( $post_home_query->have_posts() ) {
$post_home_query->the_post();
get_template_part('loop-post', 'actus');
}
wp_reset_postdata();
} ?>
</div>
</section>
<div class="separator">
<a class="btn btn-default more-article" data-paged="1" data-post-not-in="<?php echo implode(',', $array_post_not_in) ?>">Charger plus d'interviews »</a>
?>
</div>
This is the code of the loop :
<?php $color = wami_get_first_category_color(get_the_ID()); ?>
<article data-id="<?php the_ID(); ?>" class="post post-actu iso-item bloc-infinity <?php echo (isset($phone) ? $phone : '' ) ?> ">
<a href="<?php the_permalink(); ?>">
<div class="post-header">
<?php
if(has_post_thumbnail()):
the_post_thumbnail('actu-hp-small');
endif;
?>
<div class="overlay">
<div class="img"></div>
<div class="background" style="background-color:<?php echo $color; ?>">
</div>
</div>
<div class="post-header-title">
<?php wami_the_first_category(get_the_ID(), false, $color, true); ?>
</div>
</div>
<div class="post-body">
<?php
the_title( '<h2>', '</h2>' );
/*the_excerpt();*/
?>
</div>
</a>
</article>
Here's the function
function wami_load_more_posts(){
if ( isset($_REQUEST) ) {
$paged = $_REQUEST['paged'];
$post_not_in = explode(',', $_REQUEST['post_not_in']);
$args = array(
'post_type' => 'post',
'posts_per_page' => 9,
'orderby' => 'date',
'order' => 'DESC',
'post__not_in' => $post_not_in,
'post_status' => 'publish',
'paged' => $paged
);
if(isset($_REQUEST['category_in'])){
$args['category__in'] = $_REQUEST['category_in'];
}
$homeblog_query = new WP_Query($args);
if( $homeblog_query->have_posts() ):
while($homeblog_query->have_posts()):
$homeblog_query->the_post();
get_template_part('loop-post', 'actus');
endwhile;
endif;
wp_reset_postdata();
}
die();
}
add_action('wp_ajax_wami_load_more_posts', 'wami_load_more_posts');
add_action('wp_ajax_nopriv_wami_load_more_posts', 'wami_load_more_posts');
Here is my custom loop, I need to add a sorting option and after hours of digging I cannot find a solution.
<ul class="acapellas row">
<?php
$loop = new WP_Query( array(
'post_type' => 'acapella',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'date'
));
?>
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li class="post-<?php the_ID(); ?> col-md-6">
<div class="wrap">
<h2><?php the_title() ?></h2>
<?php if(pmpro_hasMembershipLevel($level_id)) { ?>
<?php the_content(); ?>
<?php } else { ?>
<div class="pro-player">
<div class="upgrade">
<a href="<?php bloginfo('url'); ?>/pro" >Upgrade to unlock</a>
</div>
</div>
<?php } ?>
<a class="download left" href="<?php the_permalink(); ?>">Download</a>
<span class="list-date right">First added: <?php the_time('F jS, Y') ?></span><br>
<?php
global $post;
$post_type = get_post_type(get_the_ID());
$post_type_taxonomies = get_object_taxonomies($post_type);
if (!empty($post_type_taxonomies)) {
echo '<ul class="details">';
foreach ($post_type_taxonomies as $taxonomy) {
$terms = get_the_term_list(get_the_ID(), $taxonomy, '', '</li><li>', '');
if ($terms) {
echo '<li>' . $terms . '</li>';
}
}
echo '</ul>';
}
?>
</div>
</li>
<?php endwhile; endif; ?>
</ul>
You must use both orderby and order, like so:
$loop = new WP_Query( array(
'post_type' => 'acapella',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'date',
'order' => 'asc'
));
I have a categories page template, listing all categories with featured images. But I want to show only subcategories of a parent category. I don't know where to modify the template. Here is the code.
get_header(); ?>
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<h1 class="border-radius-5"><?php the_title(); ?></h1>
<div id="page" class="post-content">
<?php the_content(); ?>
<?php
$terms = get_terms("category", $args);
$count = count($terms);
$categories = array();
if ($count > 0) {
echo '<ul class="listing-cat">';
foreach ($terms as $term) {
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'show_count' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $term->slug
)
)
);
$video_from_categorie = new WP_Query( $args );
if( $video_from_categorie->have_posts() ){
$video_from_categorie->the_post();
}else{}
$term->slug;
$term->name;
?>
<li class="border-radius-5 box-shadow">
<?php echo get_post_image();?>
<span><?php echo $term->name; ?></span>
<span class="nb_cat border-radius-5"><?php echo $term->count; ?> <?php if ( $term->count > 1 ){
_e( 'videos', get_theme_name() );
}else{
_e( 'video', get_theme_name() );
} ?></span>
</li>
<?php
}
echo '</ul>';
echo '<div class="clear"></div>';
}
?>
</div><!-- #page -->
<?php endwhile; ?>
<?php endif; ?>
Pass the ID of the desired parent term/category to the child_of parameter of get_terms():
$terms = get_terms( 'category', array( 'child_of' => TERM_ID_GOES_HERE ) );