I have loop that displays posts. I want to add some html if there will be no posts. Like if there is no posts ---> show my html if/else.
<?php $args = array(
'posts_per_page' => 1000,
'post_type' => 'contract',
'author' => $current_user->ID,
'post_status' => 'publish' );
?>
<?php $get_category_posts = get_posts( $args ); ?>
<?php foreach ( $get_category_posts as $post ) : setup_postdata( $post ); ?>
<li class="jail-app-v-0-4 contract-item">
<div class="contract-item-inner">
<a class="hoverable" href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</div>
</li>
<?php endforeach; ?>
Try this code out please:
<?php $args = array(
'posts_per_page' => 1000,
'post_type' => 'contract',
'author' => $current_user->ID,
'post_status' => 'publish' );
?>
<?php $get_category_posts = get_posts( $args );
if(!empty($get_category_posts)):
?>
<?php foreach ( $get_category_posts as $post ) : setup_postdata( $post ); ?>
<li class="jail-app-v-0-4 contract-item">
<div class="contract-item-inner">
<a class="hoverable" href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</div>
</li>
<?php endforeach; ?>
<?php else: ?>
NO POSTS FOUND!
<?php endif; ?>
Related
This is my code here -
<div class="gallery-posts">
<?php
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'numberposts' => 4
);
$product_posts = get_posts( $args );
?>
<?php foreach ( $product_posts as $post ) : setup_postdata( $post ); ?>
<?php $videos = rwmb_meta( '_video-link' );
foreach ( $videos as $video ); ?>
<div
class="card-container video-link"
data-link="<?php echo $video['src']; ?>"
title="<?php the_title(); ?>"
description="<?php echo ( get_post_meta( get_the_ID(), '_description', true ) ); ?>"
thumbnail-link="<?php the_post_thumbnail_url() ?>">
<figure class="gallery-image">
<?php the_post_thumbnail(); ?>
<div class="gallery-image-text">
<p><?php the_title() ?></p>
<i class="fas fa-play"></i>
</div>
</figure>
</div>
<?php endforeach; wp_reset_postdata(); ?>
</div>
I want to set it up so that Next/Prev shows whenever there are more than 4 posts.
With the regular Word Press loop I know you can just do the_posts_pagination but that doesn't work here.
What needs to be added for pagination to work with this loop?
Change your code like this:
<div class="gallery-posts">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'posts_per_page' => 4,
'paged' => $paged
);
$product_posts = get_posts( $args );
foreach ( $product_posts as $post ) : setup_postdata( $post );
$videos = rwmb_meta( '_video-link' );
foreach ( $videos as $video ):
?>
<div class="card-container video-link" data-link="<?php echo $video['src']; ?>" title="<?php the_title(); ?>" description="<?php echo ( get_post_meta( get_the_ID(), '_description', true ) ); ?>" thumbnail-link="<?php the_post_thumbnail_url() ?>">
<figure class="gallery-image">
<?php the_post_thumbnail(); ?>
<div class="gallery-image-text">
<p><?php the_title() ?></p>
<i class="fas fa-play"></i>
</div>
</figure>
</div>
<?php
next_posts_link( 'Older Entries', $product_posts->max_num_pages );
previous_posts_link( 'Next Entries »' );
wp_reset_postdata();
endforeach;
?>
</div>
Newbie here,
I'm trying to display the list of my post depends on the category. But it doesn't display my post. I tried the different type of array, but no luck. I named my page taxonomy-blog_category.php
I call the page via site.com/blog_category/category
here's my current code and I know I'm so close but can't figure it out.
Here is the array:
<div class="row">
<?php $ctr = 1;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$custom_args = array(
'post_type' => 'blog_post',
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'blog-category',
'field' => 'slug',
'terms' => array('business','people','technology'),
),
),
);
Here is how I display the post
$custom_query = new WP_Query( $custom_args ); ?>
<?php if ( $custom_query->have_posts() ) : ?>
<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<div class="col-md-4 text-center">
<div class="content-container">
<div class="wrap">
<figure class="tint t2">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id() ); ?>" width="317px" height="240">
</figure>
</div>
<h2><?php the_title(); ?></h2>
<h3>By <?php the_field('author'); ?> | <span><?php echo get_the_date(); ?></span></h3>
<?php $content = get_field('content'); echo mb_strimwidth($content, 0, 200, '...');?>
<div class="read-more-btn">
read more
</div>
</div>
</div>
<?php $ctr++; endwhile; ?>
I Don't know if this is necessary but here's my code for pagination:
<div class="pagination-holder">
<ul class="pagination">
<?php
if (function_exists(custom_pagination)) {
custom_pagination($custom_query->max_num_pages,"",$paged);
}
?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</ul>
</div>
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'
));
My Wordpress site has a problem:
In my blog-loop only the first post has content displayed; for the rest the_content(); never seemed to be called (checked with "inspect element").
Any probs in my code?
php/html:
<section id="blogPosts" class="clearfix">
<?php $myposts = get_posts('');
foreach($myposts as $post) :
setup_postdata($post);
?>
<div id="post-<?php the_ID(); ?>" class="post clearfix">
<div class="postHeader clearfix">
<a href="<?php the_permalink();?>">
<h2><?php the_title(); ?></h2>
</a>
<h4><?php the_date(); ?></h4>
</div>
<div class="postTags clearfix">
<ul class="tagContainer clearfix">
<?php the_tags( '<li><div class="tagInline">',
'</div></li><li><div class="tagInline">',
'</div></li>'); ?>
</ul>
</div>
<div class="blogContent">
<?php the_content();?>
</div>
<div class="editPost">
<?php edit_post_link('<h4>Redigera detta inlägg', '', '</h4>'); ?>
</div>
<?php wp_link_pages(array('before' => 'Pages: ', 'next_or_number' => 'number')); ?>
<?php //comments_template(); ?>
</div>
<?php endforeach; wp_reset_postdata(); ?>
</section>
Check all other posts for having content in it..
Posts can be get by
<?php $posts_array = get_posts( $args ); ?>
Default Usage of args:
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true ); ?>
Access all post data
<?php
$args = array( 'posts_per_page' => -1 );
$allposts = get_posts( $args );
foreach ( $allposts as $post ) :
setup_postdata( $post ); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endforeach;
wp_reset_postdata(); ?>
This is the simple code to get all the posts. You can check the code by print the data ( print_r($post); ) to check what you are getting in the loop.
If you're talking about the page that has been assigned to Posts under Wordpress Reading Settings, you can get rid of most of your code and simplify it:
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; endif; ?>
Just put whatever information you want between the while statement and you'll be good. See the Wordpress codex if you want to get more specific.
Heres my code:
<?php
$args = array(
'child_of' => 15,
'sort_order' => 'DESC',
'sort_column' => 'post_date',
'post_type' => 'page',
'post_status' => 'publish' );
$events = get_pages( $args );
if (have_posts()) :
while (have_posts()) : the_post();
foreach ( $events as $page ) : setup_postdata($page); ?>
<?php if (has_post_thumbnail( $post->ID ) ); ?>
<?php $date = get_the_date( $d ); ?>
<div id="post">
<div class="post_excerpt">
<h2><?php the_title(); ?> / <?php echo $date; ?></h2><?php echo the_ID($post->ID); ?>
<p><?php the_excerpt(); ?></p>
</div>
<div class="post_thumb">
<?php the_post_thumbnail(); ?>
</div>
<div class="post_line"></div>
</div>
<?php
endforeach;
endwhile;
endif;
wp_reset_postdata();
?>
It should be displaying all the child pages of the "Events" page, but instead its showing itself a few times. I've looked through the code over and over trying different tweaks to get it to work, but nothing has so far. Any help would be very appreciated!
Of course just as I post this I figure it out... I had to change this: foreach ( $events as $page ) : setup_postdata($page); ?> to this: foreach ( $events as $post ) : setup_postdata($post); ?>