I simply have a single loop that's pulling through a CPT, but I would like to hide the whole container div of the loop, if it has no posts...
I'm trying various iterations of this if statement surrounding the container:
<?php if( have_posts() ): ?>
<?php endif; ?>
But I can't seem to get it to work properly... The full code blog is here:
<?php if( have_posts() ): ?>
<div class="container default-strip-section">
<h2><?php the_field('vacancies_title'); ?></h2>
<div class="row justify-content-center">
<?php
$args = array(
'post_type' => 'vacancies',
'posts_per_page' => 9999
// 'orderby' => 'title',
// 'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="col-md-12">
<a href="<?php the_permalink(); ?>">
<p><?php the_title() ?></p>
</a>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
</div>
</div>
<?php endif; ?>
Can anyone point out what I'm doing wrong? I'm sure I'm missing something simple! Thanks for looking!! :)
#rank's answer is nearly correct -- it needs the object instance before the_post() again:
<?php
$args = array(
'post_type' => 'vacancies',
'posts_per_page' => 9999, // If you're wanting to show all posts, use -1 here.
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
?>
<div class="container default-strip-section">
<h2><?php the_field('vacancies_title'); ?></h2>
<div class="row justify-content-center">
<?php
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<div class="col-md-12">
<p><?php the_title(); ?></p>
</div>
<?php
endwhile;
?>
</div>
</div>
<?php
endif;
wp_reset_postdata();
?>
You need to put the html of the container after the if have_posts(), where you check if there are any posts. This way the whole container is not displayed when the if is not true.
But you are using a ACF field of the post where you are showing this list of vacancies. So we save the id into a variable to be able to view the value inside of your custom query called the_query (this is not a self-explanatory / good name). Why not call it vacancies_query to have a more readable code. Putting it together it should look like this:
<?php
$current_post = get_the_ID();
$args = array(
'post_type' => 'vacancies',
'posts_per_page' => 9999
);
$vacancies_query = new WP_Query( $args );
if ( $vacancies_query->have_posts() ) : ?>
<div class="container default-strip-section">
<h2><?php the_field('vacancies_title', $current_post); ?></h2>
<div class="row justify-content-center">
<?php while ( $vacancies_query->have_posts() ) : the_post(); ?>
<div class="col-md-12">
<a href="<?php the_permalink(); ?>">
<p><?php the_title() ?></p>
</a>
</div>
<?php endwhile; ?>
</div> <!-- row -->
</div> <!-- container -->
<?php else :
/* nothing to see here */
endif;
wp_reset_postdata();
?>
Related
I'm trying to output all posts under custom post type "philosophy" on a page in a list. Alternating between categories "img-left" and "img-right".
I can get the posts to display ALL "philosophy" posts however i want to lay out the posts in two layouts depending on their custom category.
If the category is "img-right" i want the post to be shown with the text on the left and image on the right and vice-versa for "img-left".
I have tried the below code which doesn't work at all.
<?php
$args = array( 'post_type' => 'philosophy', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
if( in_category( 'img-right' ) ):
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="col-md-12"><h2>';
the_title();
echo '</h2></div><div class="row content"><div class="col-md-6"';
the_content();
echo '</div><div class="col-md-5 offset-1 float-right">';
the_post_thumbnail('array(100,100)');
echo '</div></div>';
endwhile;
endif;
?>
by removing the "if" and "endif" i have the code that lists all the posts in one layout. What I need is conditionals that can output both "img-right" and "img-left" layouts based on the post's category. The only layout shown in my example above is "img-right".
Any help would be greatly appreciated. This PHP is making my head spin!
So...with all the help from the guys answering i figured it out using the CSS approach touched on by #Mohsin.
Here is my code:
<div id="content" class="col-12" role="main">
<?php get_template_part('loops/page-content'); ?>
</div>
<div class="row">
<?php
$args = array( 'post_type' => 'philosophy', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="row content"><div class="col-md-12"><h2>';
the_title();
echo '</div><div class="col-md-6">';
the_content();
echo '</div><div class="col-md-6">';
the_post_thumbnail();
echo '</div></div>';
endwhile;
?>
I then applied this:
.row.content:nth-child(even) {
flex-direction: row-reverse;
}
and we're golden.
Thank you to everyone who helped.
If I understand correctly:
<?php $args = array( 'post_type' => 'philosophy', 'posts_per_page' => 10 ); ?>
<?php $loop = new WP_Query( $args ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="col-md-12"><h2>
<?php the_title(); ?>
</h2></div>
<div class="row content">
<?php if( in_category( 'img-left' ) ): ?>
<div class="col-md-5 offset-1 float-left">
<?php the_post_thumbnail('array(100,100)'); ?>
</div>
<?php endif; ?>
<div class="col-md-6">
<?php the_content(); ?>
</div>
<?php if( in_category( 'img-right' ) ): ?>
<div class="col-md-5 offset-1 float-right">
<?php the_post_thumbnail('array(100,100)'); ?>
</div>
<?php endif; ?>
</div>
<?php endwhile; ?>
The above code will output all of the posts, with a thumbnail to the left if it is category img-left and thumbnail to the right if in img-right (and outputting both if it is categorized as both, and neither if it is neither category. You may want different behavior, but it should be simple to adjust the conditionals).
If your template becomes anymore complicated, I would recommend moving sections out to template-parts/using functions like get_sidebar().
Hi I've made a custom post type 'work_fields' that calls in information from yet another custom post type 'members' into the post, and now I'm trying to make a PAGE TEMPLATE that shows a list of the titles of custom post type 'work_fields', and when you click a title, the whole post('work_fields') will show up on a div called 'single-post-container' below the titles. right now I've got everything working fine, but I want to display a post in the div 'single-post-container' when the page loads. (as of now, just the titles of the posts are displayed and there is nothing in the div). How do I get the div to display the most recent post of custom post type 'work_fields' on page load? This is the code for the custom page template.
<div class="row">
<div class="small-12 medium-10 large-offset-1 columns">
<h2><?php the_title(); ?></h2>
</div>
</div>
<div class="row halfsection">
<div class="small-12 medium-10 large-offset-1 columns">
<div class="category_container">
<?php
$args = array('post_type' => 'work_fields',);
$query = new WP_Query( $args );
?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<p class="category_item"><a class="post-link" rel="<?php the_ID(); ?>" href="<?php the_permalink(); ?>"><?php echo get_the_title(); ?></a></p>
<?php endwhile; endif; ?>
</div>
</div>
</div>
<div class="row">
<div class="small-12 medium-10 large-offset-1 columns">
<hr>
</div>
</div>
<div id="single-post-container">
//THIS IS WHERE THE POST CONTENTS SHOWS BUT I WANT THE MOST RECENT POST TO BE HERE ON PAGE LOAD, BEFORE I CLICK ANY OTHER POST//
</div>
Thank you! Your help is much appreciated!
Just use the WP_query twice by getting recent posts in the arguments,
$args2 = array('post_type' => 'work_fields', 'orderby' => 'ID', 'order'=> 'DESC' , 'posts_per_page' => 5);
$query2 = new WP_Query( $args2 );
?><div id="single-post-container"><?php
// The Loop
if ( $query2->have_posts() ) {
echo '<ul>';
while ( $query2->have_posts() ) {
$query2->the_post();
echo '<li>' . get_the_content() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
}
?></div><?php
Put the div outside loop. It will show the content of recent 5 posts.
If everything above the div single-post-container is working fine then for this specific div you can load most recent post by using code below. Be sure to reset previous post data using wp_reset_postdata()
Codex Documentation. https://codex.wordpress.org/Function_Reference/wp_get_recent_posts
<?php
$args = array(
'numberposts' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'work_fields',
'post_status' => 'publish',
'suppress_filters' => true
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
So I just recreated the post type markup on my page. I'm sure there's a better way to do this but for times sake I had to at least make it work. I also tried using a jquery onclick function and just click the first title after everything loads, but there was an error that just kept pushing all of the titles so I pretty much gave up.
here's the code
<div class="row">
<div class="small-12 medium-10 large-offset-1 columns">
<h2><?php the_title(); ?></h2>
</div>
</div>
<div class="row halfsection">
<div class="small-12 medium-10 large-offset-1 columns">
<div class="category_container">
<?php
$args = array(
'post_type' => 'work_fields',
);
$query = new WP_Query( $args );
?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<p class="category_item"><a class="post-link" rel="<?php the_ID(); ?>" href="<?php the_permalink(); ?>"><?php echo get_the_title(); ?></a></p>
<?php endwhile; endif; ?>
</div>
</div>
</div>
<div class="row">
<div class="small-12 medium-10 large-offset-1 columns">
<hr>
</div>
</div>
<div id="single-post-container">
<?php
$args2 = array(
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'work_fields',
'post_status' => 'publish',
'posts_per_page' => 1,
);
$query2 = new WP_Query( $args2 );
if ( $query2->have_posts() ) : ?>
<?php $post = get_post($_POST['id']); ?>
<div id="single-post post-<?php the_ID(); ?>">
<?php while ( $query2->have_posts() ) : $query2->the_post(); ?>
<div class="row section">
<div class="small-12 medium-7 large-offset-1 columns">
<h2><?php the_title(); ?></h2>
<h3>소개</h3>
<p class="halfsection"><?php the_field('work_fields_intro'); ?></p>
<h3>주요서비스</h3>
<p class="halfsection"><?php the_field('work_fields_service'); ?></p>
<h3>주요실적</h3>
<p class="halfsection"><?php the_field('work_fields_accomplishment'); ?></p>
</div>
<?php endwhile; endif; ?>
<div class="small-6 medium-3 large-2 columns large-offset-1 end">
<?php
$posts = get_field('team_member');
if( $posts ): ?>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<div class="member_container halfsection">
<div class="member"><?php the_post_thumbnail(); ?></div>
<p class="member_name"><?php the_title(); ?></p>
<ul class="members_info">
<li><?php the_field('members_position'); ?></li>
<li><?php the_field('members_e-mail'); ?></li>
<li><?php the_field('members_phone'); ?></li>
</ul>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
I have created a custom-post-type and loaded the titles on my front-page as toggle-buttons. Now I want to make sure, that the content of the toggle-div is always related to the button I clicked. I tried to realize by simply using loading the post content, but I think I need to get the post-id as a variable or something into the loop. I have simply no idea how to make this work. I am a absolute beginner in PHP, hope someone can give me a hint?
Related Website: http:www.mzk.ernst-werbeagentur.de
My Code:
<?php
$query = new WP_Query( array( 'post_type' => 'praxen' ) );
if ( $query->have_posts() ) : ?>
<div class="container-fluid wrapper-slider-head">
<div class="container-fluid slider-head fullscreen" id="img-start">
<img src="http://mzk.ernst-werbeagentur.de/wp-content/uploads/2015/10/background-start.jpg" width='1920' height='1080'>
</div>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="container-fluid slider-head fullscreen" id="img-<?php echo $countimg; ?>" style="display:none;">
<?php echo get_the_post_thumbnail( $post_id, 'full', array( 'class' => 'img-' . $countimg ) ); ?>
</div>
<?php
$countimg++;
endwhile; wp_reset_postdata(); ?>
</div>
<?php endif; ?>
<?php
$query = new WP_Query( array( 'post_type' => 'praxen' ) );
if ( $query->have_posts() ) : ?>
<div class="container links-praxen">
<div class="row">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col-sm-4"><div <?php post_class( 'jumbotron p' . $countpost ); ?>><div class="hovertogglecont"><div class="hovertogglecontinside">
<h2><?php the_title(); ?></h2>
<p><?php echo get_post_meta($post->ID, 'toggletitel', true); ?></p>
</div></div></div></div>
<?php
$countpost++; endwhile; wp_reset_postdata(); ?>
<?php endif; ?>
<div class="container-fluid wrapper-praxisinfo" style="display:none;"> <!-- This div should toggle and show the related post-content by clicking on a link from the loop above. -->
<div class="container">
<div class="close-post-content"><i class="fa fa-times" style="cursor:pointer; color:#fff;"></i></div>
<div class="post-content">
<?php
$query = new WP_Query( array( 'post_type' => 'praxen' ) );
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php the_content(); ?>
<?php
endwhile; wp_reset_postdata(); ?>
<?php endif; ?>
</div>
</div>
</div>
I want tot show the related post in the third loop.
Cheers and thank you very much,
David
once you load your page content, you cannot re-run php with some parameter after JavaScript button click. You would need to use AJAX call to get post content from some new PHP file for example post-content.php with is as parameter and then place it inside your wrapper-praxisinfo.
What I would suggest instead is to output all posts as you are doing now, and just hide ones that you dont want to see with JavaScript.
<div class="container">
<div class="close-post-content"><i class="fa fa-times" style="cursor:pointer; color:#fff;"></i></div>
<div class="post-content">
<?php
$query = new WP_Query( array( 'post_type' => 'praxen' ) );
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="post-id-<?php the_ID();?>">
<?php the_content(); ?>
</div>
<?php
endwhile; wp_reset_postdata(); ?>
<?php endif; ?>
</div>
</div>
</div>
Here every post is wrapped inside a div with post ID as a class. You just need to hide all posts and on button click show the one that was clicked by ID.
So I'm trying to pull only the latest sticky post from WordPress and only 1. I am using 'showpost=1' but for some reason if I have two posts tagged as sticky both show up?
<h2>Breaking News</h2>
<?php
query_posts('posts_per_page=1');
if (have_posts()) {
while (have_posts()) : the_post();
if ( is_sticky() ) : ?>
<div class="small-12 medium-6 large-4 columns">
<div class="img-holder">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail('sticky-hp-thumbnails');
}
?>
<?php if( get_field('sticky_sub_heading') ): ?>
<div class="tag">
<p>
<?php the_field('sticky_sub_heading'); ?>
</p>
</div>
<?php endif; ?>
</div>
</div>
<div class="small-12 medium-6 large-4 columns">
<h3><a href"<?php the_permalink() ?>">
<?php the_title(); ?>
</a></h3>
<?php if( get_field('sticky_date') ): ?>
<p class="sticky-date">
<?php the_field('sticky_date'); ?>
</p>
<?php endif; ?>
<p>
<?php the_field('sticky_summary'); ?>
</p>
Read More </div>
<?php endif;
endwhile;
}
wp_reset_query();
?>
Where am I going wrong in the above code?
Use posts_per_page instead of "showposts"
i.e.
query_posts( 'posts_per_page=1' );
Source:
https://codex.wordpress.org/Function_Reference/query_posts
Updated: Adding WP_QUERY code to fetch latest sticky post:
<?php
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="post">
<h3><?php echo get_the_title(); ?></h3></a>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
The easiest solution to solve (workaround) your problem is to remove the while loop. This way you'll print only one.
Of course, this is sub-optimal since other pages may be fetched and unused; you could try using posts_per_page=1 or post_limits=1 to see if solve the issue.
I added a code in my php and i don't know what is wrong, it shows only 2 posts but I got 10 posts in my WordPress can someone check my code?
<?php
$args = array(
'post_type' => 'post',
'category_name' => 'appetizers',
'post_status' => 'publish'
);
$Loop = new WP_Query( $args );
?>
<div class="row">
<?php while ( $Loop->have_posts() ) : $Loop->the_post();?>
<a href="<? the_permalink(); ?>">
<div class="col-sm-3">
<?php the_post_thumbnail(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php the_field('price'); ?>
</div>
</a>
<?php endwhile; ?>
</div>
add 'posts_per_page' => -1 in $args.
May be it is picking up the number of posts from the 'Setting -> Reading' option in back end.