I've created an archive page for a custom post type in Wordpress. I want to output data if certain fields match.
Within the first if statement, I want to check what product the user has signed up to (held within $product and match it against the current items custom field (held within $title).
All it's doing is seeing what the user signed up to then outputting the content (which is all the data below <?php if($match == "yes"){ ?>.
The problem I have is wp_reset_postdata() is killing the data held within $match. Is there a way around this? If I don't include the setting up of post data and don't include the reset then the rest of the page doesn't show the correct info.
I'm using Advanced Custom Fields relationship field (http://www.advancedcustomfields.com/resources/field-types/relationship/).
Any help would be much appreciated.
User signed up as: <?php $current_user = wp_get_current_user();
$board = $current_user->work;
$product = $current_user->product;
echo $product; ?>
<br />
<?php while (have_posts()) : the_post(); ?>
<?php $products = get_field('product');
if( $products ):
foreach( $products as $post): // variable must be called $post (IMPORTANT)
setup_postdata($post);
$title = $post->post_title;
echo $title;
if($product == $title) {
$match = "yes";
}
endforeach;
wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
endif; ?>
<?php if($match == "yes"){ ?>
<div id="module-area" style="margin-top:0px!IMPORTANT;">
<div id="modules-top"></div>
<div id="modules-repeat" style="position:relative;padding-left:10px;padding-right:10px;width:625px!IMPORTANT;">
<div class="topic">
<p><?php the_title(); ?></p>
</div>
<div class="description">
<p><?php the_field('description') ?></p>
</div>
<div class="date">
<p style="text-align:center!IMPORTANT;"><?php the_modified_time('d.m.y'); ?></p>
</div>
<div class="action">
<a class="train-button" href="<?php echo the_permalink(); ?>"></a>
</div>
<div class="clear"></div>
</div>
<div style="margin-bottom:5px;" id="modules-bottom"></div>
</div>
<?php } ?>
<?php endwhile; ?>
User signed up as: <?php $current_user = wp_get_current_user();
$board = $current_user->work;
$product = $current_user->product;
?>
<?php $products = get_field('product');
if( $products ):
foreach( $products as $post): // variable must be called $post (IMPORTANT)
setup_postdata($post);
$title = $post->post_title;
if($product == $title) {
wp_reset_postdata(); ?>
<div id="module-area" style="margin-top:0px!IMPORTANT;">
<div id="modules-top"></div>
<div id="modules-repeat" style="position:relative;padding-left:10px;padding-right:10px;width:625px!IMPORTANT;">
<div class="topic">
<p><?php the_title(); ?></p>
</div>
<div class="description">
<p><?php the_field('description') ?></p>
</div>
<div class="date">
<p style="text-align:center!IMPORTANT;"><?php the_modified_time('d.m.y'); ?></p>
</div>
<div class="action">
<a class="train-button" href="<?php echo the_permalink(); ?>"></a>
</div>
<div class="clear"></div>
</div>
<div style="margin-bottom:5px;" id="modules-bottom"></div>
</div>
<?php
}
endforeach;
endif; ?>
wp_reset_postdata();
Related
I am trying to get a block to split content after so many entry's have been selected by the user in the backend of my website.
This is what is is supposed to look like
However at the moment I cannot get it to break off a second line. here is the code below to show how I am rendering my block along with an image of what is being rendered
function roomFacilitiesLists(){
$roomFacilitiesList1 = get_field ("facilities_list_1") ;
$roomFacilitiesList2 = get_field ("facilities_list_2") ;
$roomFacilitiesList3 = get_field ("facilities_list_3") ;
$roomFacilitiesLeftSideImage = get_field ("room_facilities_left_side_image") ;
$roomFacilitysBackgroundImage = get_field ("room_facilitys_background_image") ;
?>
<div class="left-side-image-block-parent">
<div class="left-sided-image">
<img src="<?php echo $roomFacilitiesLeftSideImage['url']; ?>" ; />
</div>
</div>
<div class="right-side-room-facilitys">
<div class="room-facilities-lists" style="background-image: url(<?php echo $roomFacilitysBackgroundImage['url']; ?>)"> ;
</div>
<div class="main-lists-container">
<div class="column-1-list">
<?php
if( $roomFacilitiesList1 ): ?>
<ul>
<?php foreach( $roomFacilitiesList1 as $roomFacilitiesList1 ): ?>
<div><?php echo $roomFacilitiesList1; ?></div>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
<div class="column-3-list columns-2">
<?php
if( $roomFacilitiesList3 ): ?>
<ul>
<?php foreach( $roomFacilitiesList3 as $roomFacilitiesList3 ): ?>
<div><?php echo $roomFacilitiesList3; ?></div>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
<div class="column-2-list">
<?php
if( $roomFacilitiesList2 ): ?>
<ul>
<?php foreach( $roomFacilitiesList2 as $roomFacilitiesList2 ): ?>
<div><?php echo $roomFacilitiesList2; ?></div>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
</div>
Now this makes up 3 columns of data however the other 2 are not the issue here is "columns-3" which is the data output causing the issues.
I have tried using https://www.php.net/manual/en/function.str-split.php as well as https://www.php.net/manual/en/function.explode but with no prevail any help here would be great.
With this code, all the images on the relationship shows, but post object keeps repeating many times... what's wrong with this code and what's a better way of implementing it?
It's using a gallery as custom post type object, assigned to this custom post type single page.
the page is looking right but, if I debug, the <?php foreach( $images as $image_id ): ?>
loop is repeating a lot more than it should some how.
Any ideas on what's the problem here?
<?php
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<div class="employee-page">
<div class="page-content">
<div class="container">
<div class="employee-gallery">
<div class="row">
<?php $posts = get_field('employee_gallery'); ?>
<?php if( $posts ): ?>
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>
<?php
$images = get_field('gallery');
$size = 'large';
if( $images ): ?>
<?php foreach( $images as $image_id ): ?>
<div class="column col-lg-2 col-md-4 col-xs-12">
<a href="<?php echo wp_get_attachment_image_url( $image_id, 'full' ); ?>">
<?php echo wp_get_attachment_image( $image_id, $size ); ?></a>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
</main>
</div>
<?php
get_sidebar();
get_footer();
I'm using Bootstrap, I have a list of posts and I want to wrap every 2 posts on a row. Each post is wrapped on a <div col>. You can see live here.
I tried with this but it wrap the row each one post:
<?php
$num = 0;
// Check if there are any posts to display
if ( have_posts() ) : ?>
<?php
// The Loop
while ( have_posts() ) : the_post();
if($num%2) {
echo "<div class='row' style='margin-bottom: 2.3em;''>";
}
?>
<div class="col-xs-12 col-sm-6 col-md-6">
<h2 class="category-encabezado">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Enlace a <?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</h2>
<small>Hace
<?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ''; ?>
</small>
<div class="entry">
<p>
<?php the_excerpt(); ?>
</p>
<?php
$my_shortcode = get_field('audio-field');
echo do_shortcode( $my_shortcode );
?>
</div>
</div>
<?php
if($num %2) {
echo '</div>';
}
$num++
?>
<?php endwhile; // End Loop
?>
</div>
<?php
You have to put div.row Out of the Loop while
I'm trying to load all Wordpress posts into three different Divs as equal,
like this
<div class="row">
<div class="col-4">
POSTS HERE 1/3
</div>
<div class="col-4">
POSTS HERE 1/3
</div>
<div class="col-4">
POSTS HERE 1/3
</div>
</div>
I've already done something similar on other pages, but the difference is I'm only loading 6 posts there, so it's easier,
but now I wanna list all posts in the page into 3 columns,
this is the code I've used for 6 posts:
<?php
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>6)); ?>
<?php if ( $wpb_all_query->have_posts() ) : ?>
<?php query_posts('showposts=2'); ?>
<div class="col-sm-4">
<div class="article_list">
<?php $posts = get_posts('numberposts=2&offset=0'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "2") { break; } else { ?>
<article class="article_box">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<h3 class="journal_title"><?php the_title(); ?></h3>
</article>
<?php $count1++; } ?>
<?php endforeach; ?>
</div>
</div>
<?php query_posts('showposts=2'); ?>
<div class="col-sm-4">
<div class="article_list">
<?php $posts = get_posts('numberposts=2&offset=2'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count2 = 0; if ($count2 == "2") { break; } else { ?>
<article class="article_box">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<h3 class="journal_title"><?php the_title(); ?></h3>
</article>
<?php $count2++; } ?>
<?php endforeach; ?>
</div>
</div>
<?php query_posts('showposts=2'); ?>
<div class="col-sm-4">
<div class="article_list">
<?php $posts = get_posts('numberposts=2&offset=4'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count3 = 0; if ($count3 == "2") { break; } else { ?>
<article class="article_box">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<h3 class="journal_title"><?php the_title(); ?></h3>
</article>
<?php $count3++; } ?>
<?php endforeach; ?>
</div>
</div>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Try the below code. you will get post list in below format.
<?php
$loop_counter = $innerBreak = 1;
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>6));
if($wpb_all_query->have_posts()):
while($wpb_all_query->have_posts()) :
$wpb_all_query->the_post();
if($innerBreak == 1){
?>
<!-- when complete listing of 3 post open the new div -->
<div class="col-sm-4">
<?php
}
?>
<div class="article_list">
<article class="article_box">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<h3 class="journal_title"><?php the_title(); ?></h3>
</article>
</div>
<?php
//when complete listing of 3 post closed previously div.
if($loop_counter%3==0){ echo '</div>'; $innerBreak = 1;}else{$innerBreak = 0;}
$loop_counter++; endwhile;
else:
echo "<div>No Results Found</div>";
endif;
?>
Thanks to Shivendra Singh
after some small tweaks, here is the code to equally split all posts into three columns
<?php
$loop_counter = $innerBreak = 1;
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1));
if($wpb_all_query->have_posts()):
while($wpb_all_query->have_posts()) :
$wpb_all_query->the_post();
if($innerBreak == 1){
?>
<?php $posts_count = round((($wpb_all_query->post_count) / 3)+0.5); ?>
<!-- when complete listing of 3 post open the new div -->
<div class="col-sm-4">
<div class="article_list" data-parallax='{"y" : -150, "distance": 2000, "smoothness": 10}'>
<?php
}
?>
<article class="article_box">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<h3 class="journal_title"><?php the_title(); ?></h3>
</article>
<?php
//when complete listing of 3 post closed previously div.
if($loop_counter%$posts_count==0){ echo '</div></div>'; $innerBreak = 1;}else{$innerBreak = 0;}
$loop_counter++; endwhile;
else:
echo "<div>No Results Found</div>";
endif;
?>
In line $wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>6)); ?> you use 'posts_per_page'=>6.
Try to: 'posts_per_page'=>-1 it's load all posts.
Then try instead of your code:
<!-- If you use bootstrap -->
<div class="container">
<div class="row">
<?php
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>6)); ?>
<?php if ( $wpb_all_query->have_posts() ) : ?>
<?php while( $wpb_all_query->have_posts() ){ $wpb_all_query->the_post(); ?>
<div class="col-md-4">
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); // or the_content(); or any ?>
</div>
<?php } ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
</div>
I have some posts displaying from a specific category but need them to have a little different styling on every second post.
This code I have displays all posts in a list with the same styling.
How do I edit this code and make it so I can edit the styling of ONLY every second post?
As it stands there's text on the left and an image on the right - the second posts just need to switch sides - it's simple styling but I'm unsure how to break the posts up to edit #case-left & #case-right are the two divs that need to switch.
Thanks in advance.
<?php // PAGE LINK/TITLE
if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("category_name=case-study&posts_per_page=10");
if ($posts) {
foreach ($posts as $post):
setup_postdata($post);
?>
<div class="serve-inner-split">
<div id="case-split">
<div id="case-left" class=" serve-left">
<div id="case-study-content">
<h1 class="case-study-title"><?php the_title(); ?></h1>
<p><?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
</p>
READ CASE STUDY
</div>
</div>
<div id="case-right" class="serve-grey">
<?php
if ( has_post_thumbnail() ) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
</div>
</div>
</div>
<?php endforeach;
}
}
?>
<?php // PAGE LINK/TITLE
if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("category_name=case-study&posts_per_page=10");
if ($posts) {
$counter = 1;
$class = '';
foreach ($posts as $post):
setup_postdata($post);
if($counter%2 == 0) {
$class = "even-no";
} else {
$class = '';
}
?>
<div class="serve-inner-split <?php echo $class; ?>">
<div id="case-split">
<div id="case-left" class=" serve-left">
<div id="case-study-content">
<h1 class="case-study-title"><?php the_title(); ?></h1>
<p><?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
</p>
READ CASE STUDY
</div>
</div>
<div id="case-right" class="serve-grey">
<?php
if ( has_post_thumbnail() ) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
</div>
</div>
</div>
<?php $counter++; ?>
<?php endforeach;
}
}
?>
Ok, so when editing your question I saw that you want to style it so that the divs switch places. You can do that like this in php
<?php // PAGE LINK/TITLE
if (is_page()){}
$i = 0;
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("category_name=case-study&posts_per_page=10");
if ($posts) {
foreach ($posts as $post):
setup_postdata($post);
$i++;
if ($i % 2 == 0):?>
<div class="serve-inner-split">
<div id="case-split">
<?php if ( has_post_thumbnail() ):?>
<div id="case-right" class="serve-grey">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
<div id="case-left" class=" serve-left">
<div id="case-study-content">
<h1 class="case-study-title"><?php the_title(); ?></h1>
<p><?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
</p>
READ CASE STUDY
</div>
</div>
</div>
</div>
<?php else: ?>
<div class="serve-inner-split">
<div id="case-split">
<div id="case-left" class=" serve-left">
<div id="case-study-content">
<h1 class="case-study-title"><?php the_title(); ?></h1>
<p><?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
</p>
READ CASE STUDY
</div>
</div>
<?php if ( has_post_thumbnail() ):?>
<div id="case-right" class="serve-grey">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
</div>
</div>
<?php endif;
?>
<?php endforeach;
wp_reset_postdata();
} ?>
But in my opinion, that's kinda unnecessary. You can also do it like this:
<?php // PAGE LINK/TITLE
if (is_page()){}
$i = 0;
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("category_name=case-study&posts_per_page=10");
if ($posts) {
foreach ($posts as $post):
setup_postdata($post);
$class = '';
$i++;
if ($i % 2 == 0){
$class = 'right_image'
}
?>
<div class="serve-inner-split <?php echo $class; ?>">
<div id="case-split">
<div id="case-left" class=" serve-left">
<div id="case-study-content">
<h1 class="case-study-title"><?php the_title(); ?></h1>
<p><?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
</p>
READ CASE STUDY
</div>
</div>
<?php if ( has_post_thumbnail() ):?>
<div id="case-right" class="serve-grey">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
</div>
</div>
<?php endforeach;
wp_reset_postdata();
} ?>
And then float the image the other way (or use absolute positioning or whatever suits you), with the '.right_image' class in the css.