I have a few advanced custom fields pages, they all have the same structure using a repeater field. I want them all to share the same single.php file. (which i believe should be fine?)
All my php is in the right order as its working fine when i add this above if have posts:
<?php
// The Query
$args = array(
'post_type' => 'chemicals'
);
$the_query = new WP_Query( $args );
?>
But once it is removed it is not getting the post information.. Maybe I have my php structure wrong? im very new to php and wordpress so any help would be great.
<div class="section group">
<div class="col span_3_of_12">
<?php wp_nav_menu(
array(
'menu' => 'consumables',
'container' => 'div',
'container_class' => 'product-menu',
'menu_class' => 'product-menu',
)); ?>
</div>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if( have_rows('product') ): ?>
<div class="col span_3_of_12">
<?php while( have_rows('product') ): the_row();
// vars
$title = get_sub_field('product_title');
$thumbnail = get_sub_field('thumbnail');
$blurb = get_sub_field('main_blurb');
$technical = get_sub_field('technical');
$description = get_sub_field('description');
?>
<a href""><img class="open-image" src="<?php echo $thumbnail; ?>"></a>
</div>
<div class="col span_6_of_12">
<div class="product-details">
<h4><?php echo $title; ?></h4>
<p><?php echo $blurb; ?></p>
</div>
<div class="product-tech">
<div class="technical">
<h5>Technical</h5>
<p><?php echo $technical; ?></p>
</div>
<div class="technical">
<h5>Description</h5>
<p><?php echo $description; ?></p>
</div>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
I see that yours is a custom post. The file name of the template should be single-chemicals.php instead of single.php
From WordPress codex:
single-{post_type}.php If your custom post type were 'product', and/or query_var = "product", WordPress would look for
single-product.php to display the single or permalink of the post.
Related
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();
?>
Pagination template part includes common pagination function with style. The template part works for archive.php (it's for "single", you know default wp file) but doesn't work for custom post type.
Why not? How to solve it?
<?php get_header(); ?>
<main role="main">
<!-- section -->
<?php get_template_part( 'breadcrumb' );?>
<!-- Inner Pages Main Section -->
<section class="ulockd-service-details">
<div class="container">
<div class="col-md-12">
<div class="row">
<?php
/**
* Setup query to show the ‘services’ post type with ‘8’ posts.
* Output the title with an excerpt.
*/
$args = array(
'post_type' => 'team',
'post_status' => 'publish',
'posts_per_page' => 1,
);
$loop = new WP_Query( $args );
if (have_posts()): while ( $loop->have_posts() ) : $loop->the_post();
?>
<?php //if (have_posts()): while (have_posts()) : the_post(); ?>
<?php
if ( $thumbnail_id = get_post_thumbnail_id() ) {
if ( $image_src = wp_get_attachment_image_src( $thumbnail_id, 'normal-bg' ) )
?>
<div class="col-md-12 ulockd-mrgn1210">
<div class="ulockd-project-sm-thumb">
<img class="img-responsive img-whp" src="<?php printf( '%s', esc_url($image_src[0]) ); ?>" alt="">
</div>
</div>
<?php
}
?>
<div class="col-md-12 ulockd-mrgn1210">
<article class="ulockd-pd-content">
<div class="ulockd-bp-date">
<ul class="list-inline">
<li class="ulockd-bp-date-innner">On <span class="text-thm2"><?php the_time('j'); ?></span> / <?php the_time('F Y') ?></li>
<li class="ulockd-bp-comment"><span class="flaticon-nurse-head text-thm1"></span> <?php the_author_posts_link(); ?></li>
<li class="ulockd-bp-comment"><span class="flaticon-chat text-thm1"></span> <?php if (comments_open( get_the_ID() ) ) comments_popup_link( __( 'Leave your thoughts', 'html5blank' ), __( '1 Comment', 'html5blank' ), __( '% Comments', 'html5blank' )); ?></li>
<li class="ulockd-bp-comment"><span class="flaticon-black-check-box text-thm1"></span> <?php the_category(); ?></li>
</ul>
</div>
<h3><?php the_title(); ?> </h3>
<p class="project-dp-one"><?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?></p>
<a class="btn btn-lg ulockd-btn-thm2" href="<?php the_permalink(); ?>"> Read More</a>
</article>
</div>
<?php get_template_part('pagination'); ?>
<?php endwhile; ?>
<?php else: ?>
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
</article>
<?php endif; ?>
</div></div></div></section>
<?php get_footer(); ?>
</main>
First of all, you don't need to include that template inside while loop. That's wrong.
Then, if you want to have an archive page for your team post type, you need to provide 'has_archive' => true within register_post_type() function args.
Also, consider changing archive page default slug if needed. If you do so, you need to open Settings > Permalinks for resetting your permalinks structure.
Then you could either use standard archive.php for the whole team archive page and standard template-parts/content.php for one post inside the loop or rewrite either of those by creating archive-team.php or content-team.php. And the_posts_pagination() function will work on proper archive page (either archive.php or archive-team.php).
I would try two things.
Add the argument paged to your wp_query args :
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'team',
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 1,
);
Put your pagination template outside the loop :
<?php endwhile; ?>
<?php get_template_part('pagination'); ?>
I´m working with wordpress and Advanced Custom Field and I´m trying to display post objects in an acf repeater, but unfortunately it only outputs the first row of the repeater. Someone has an idea why?
Here is my Code:
<?php if( have_rows('repeater') ): ?>
<?php while( have_rows('repeater') ): the_row();
// vars
$repeater = get_sub_field('repeater'); ?>
<?php $repeaterGroup = get_sub_field('repeater_group'); ?>
<div class="box25-75">
<div class="col col1">
<?php $colLeft= $repeaterGroup['left_col']; ?>
<div class="text">
<p class="text-center">
E: <?php echo $colLeft['email']; ?><br />
T: <?php echo $colLeft['telefon']; ?>
</p>
</div>
</div>
<div class="col col2">
<div style="margin-top: 40px;">
<?php
$args = array(
'post_type' => 'html5-blank',
'post_status' => 'publish',
'posts_per_page' => '100',
);
$wp_query = new WP_Query( $args );
if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post();
// Set variables
$title = get_the_title();
$description = get_the_content();
$field = get_field_object('autor');
$colors = $field['value'];
$date = get_the_date( 'd.m.Y' );
// Output
?>
<?php // show something ?>
<?php $wp_query->reset_postdata(); ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
<div class="clear"></div>
</div>
<?php endwhile; ?>
<?php endif; ?>
I found my problem. Instead of reset the post data, I had to reset de query. So I changed the line "$wp_query->reset_postdata();" with "wp_reset_query();"
I'm trying to loop only custom post types using the WP loop, but only shows the ones I give by ID.
This is my "normal" loop right now:
<?php $args = array(
'post_type' => 'referenties', 'posts_per_page' => 5, 'order' => 'DESC',
); ?>
<?php
$number = 0;
query_posts($args);
if(have_posts()):
?>
<!-- /Carousel script -->
<div class="container">
<div class="carousel-loop">
<div id="myCarousel" class="carousel slide">
<ol class="carousel-indicators">
<?php while(have_posts()): the_post(); ?>
<li data-target="#myCarousel" data-slide-to="<?php echo $number++; ?>"></li>
<?php endwhile; ?>
</ol>
<div class="controle-buttons">
<a class="carousel-control left" href="#myCarousel" data-slide="prev"><i class="fa fa-chevron-circle-left"></i></a>
<a class="carousel-control right" href="#myCarousel" data-slide="next"><i class="fa fa-chevron-circle-right"></i></a>
</div>
<!-- Carousel items -->
<div class="carousel-inner">
<?php while(have_posts()): the_post(); ?>
<!-- Carousel nav -->
<div class="item">
<div class="col-sm-2">
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<?php the_post_thumbnail(array(150,150)); // Declare pixel size you need inside the array ?>
</div>
<div class="col-sm-4">
<h4><?php the_title(); ?></h4>
<?php $bedrijf = get_field('naam_bedrijf'); ?>
<?php $feest = get_field('feest'); ?>
<?php $link = get_field('mylink'); ?>
<?php echo '<p>Bedrijfsnaam: ' . $bedrijf . '</p>'; ?>
<?php $post_object = get_field('mylink');
if( $post_object ): $post = $post_object; setup_postdata( $post ); ?>
<p>Feest type: <a style="color:#ff6600" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
<?php endif; ?>
</div><?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<div class="col-sm-4 col-sm-offset-1">
<h4>Opmerking</h4>
<p><?php echo custom_field_excerpt_longer(); ?></p>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php echo '<p>' . wp_review_show_total() . '</p>'; ?>
</div>
<?php endif; ?>
</div>
<?php endwhile; ?>
But I only want to show post ID's: 2706, 2462, 2514, 2511 and 2505.
The loop is shown in a carousel, this works fine. But I just want the ID's to show and not all posts added.
Use something like this:
<?php
$args = array(
'post_type' => 'referenties',
'post__in' => array(2706, 2462, 2514, 2511, 2505),
'order' => 'DESC',
);
$the_query = new WP_Query($args);
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//post content output goes here
}
// Restore original Post Data
wp_reset_postdata();
} else {
// no posts found
}
The post__in () argument uses array with desired post ids to retrieve.
Don't use query_posts for custom queries. Too much that can go wrong.
Hope this helps :)
I have created custom post types and added some things through advanced custom fields.
Usually when i don't need to select from each category i would do something like this.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
<?php the_field('name_of_field'); ?>
endwhile; else: ?>
<?php endif; ?>
However now i have to select if it is one of two particular categories and then put category 'tjanster' in left column and 'produkter' in right column within my bootstrap grid.
So it will be 50% 'tjanster', 50% 'produkter'.
I have been searching like crazy for an solution but only found some code for functions.php that didn't work and 5years old posts that didn't work for me.
What am i suposed to to so i can have some similar output like this?
<div class="container">
<div class="row">
<div class="col-sm-6">
<!-- Output of category tjanster. -->
</div>
<div class="col-sm-6">
<!-- Output of category produkter. -->
</div>
</div>
</div>
For each column you need a WP_Query(). See codex for reference: http://codex.wordpress.org/Class_Reference/WP_Query.
<div class="container">
<div class="row">
<div class="col-sm-6">
<?php // tjanster
$args1 = array(
'category_name' => 'tjanster'
);
$query1 = new WP_Query( $args1 );
while ( $query1->have_posts() ) : $query1->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile;
wp_reset_postdata(); ?>
</div>
<div class="col-sm-6">
<?php // produkter
$args2 = array(
'category_name' => 'produkter'
);
$query2 = new WP_Query( $args2 );
while ( $query2->have_posts() ) : $query2->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile;
wp_reset_postdata(); ?>
</div>
</div>
</div>