Custom fields showing on other posts - php

I'm using Wordpress & Advanced Custom Fields and I have one field called "show_additional_information", which if set to "Yes" should show an extra subset of information. However, when those fields are populated, they are displaying on top of one another on all record-of-the-month post types. I think I need to somehow set it to pull from only the displayed post but am unsure on how to do so. Any help would be much appreciated
<?php query_posts(array(
'post_type' => 'record-of-the-month', // post type with custom fields for display
'meta_key' => 'show_additional_information', // custom field to display extra info
'meta_compare' => '=',
'meta_value' => 'Yes',
)
); ?>
<?php while ( have_posts() ) : the_post(); ?>
<section class='bg-cover invert-section triple-margin-bottom' style='background-image: url(<?php the_field('background_image_2'); ?>);'>
<div class='container'>
<div class='row'>
<div class='col-md-2'></div>
<div class='col-md-8 text-center'>
<?php the_field('press_quote'); ?>
</div>
<div class='col-md-2'></div>
</div>
</div>
</section>
<section class='padding-top double-padding-bottom double-margin-bottom border-bottom'>
<div class='container'>
<div class='row'>
<div class='col-md-1'></div>
<div class='col-md-10 double-padding-right'>
<?php the_field('artist_bio'); ?>
</div>
<div class='col-md-1'></div>
</div>
</div>
</section>
<?php endwhile; ?>

What type of custom field are you using? If you were using the true/false selector you could use:
$args = array(
'post_type' => 'record-of-the-month',
'meta_query' => array(
array(
'key' => 'field_name',
'value' => '1',
'compare' => '=='
)
)
);
$wp_query = new WP_Query( $args );
while ( have_posts() ) : the_post();
//
Either way, try using WP_query over query_posts

Figured it out by putting separate if statements before the individuals fields (see code below).
<?php if( $field = get_field('background_image_2') ): ?>
<section class='bg-cover invert-section triple-margin-bottom' style='background-image: url(<?php the_field('background_image_2'); ?>);'>
<div class='container'>
<div class='row'>
<div class='col-md-2'></div>
<div class='col-md-8 text-center'>
<?php the_field('press_quote'); ?>
</div>
<div class='col-md-2'></div>
</div>
</div>
</section>
<?php endif; ?>
<?php if( $field = get_field('artist_bio') ): ?>
<section class='padding-top double-padding-bottom double-margin-bottom border-bottom'>
<div class='container'>
<div class='row'>
<div class='col-md-1'></div>
<div class='col-md-10 double-padding-right'>
<?php the_field('artist_bio'); ?>
</div>
<div class='col-md-1'></div>
</div>
</div>
</section>
<?php endif; ?>

Related

ACF repeater field not rendering the rows

I have this website that I'm working on for a client. I jumped into the project and I have to create the accordion on the template file for specific pages. I just started getting deeper into PHP so I found a solution online in this article https://wpbeaches.com/create-an-acf-repeater-accordion-in-wordpress/
Now, my problem is this:
I created the repeater field with two subfields https://prnt.sc/xfg3lv and I choose that it only shows on this page template https://prnt.sc/xfg6lw
Then I created the fields on the actual page with that template https://prnt.sc/xfgdhp and inserted that piece of code from the article into the template file. Of course, I modified the names with my field names. The problem is that on the front it is not showing the rows even if they exist and just skips to the else statement. I will paste the complete code of that template page so if anyone can help it would be awesome. I'm busting my head for two days now on this issue.
The code (I marked where my code starts and ends):
<?php
/**
* Template Name: Services
*
**/
global $post;
$post_slug = $post->post_name;
$grandParent_title=get_page(get_ancestors($post->ID,'page')[0] )->post_title;
$grandParent_Image=wp_get_attachment_url( get_post_thumbnail_id(get_ancestors($post->ID,'page')[0] )) ;
get_header();
global $wp;
$current_slug = add_query_arg( array(), $wp->request );
$url_slug=explode('/', $current_slug);
$uri_string=$url_slug[1];
$_SESSION['current_url']=$uri_string;
function find_string_in_array ($arr, $string) {
return array_filter($arr, function($value) use ($string) {
return strpos($value, $string) !== false;
});
}
if(find_string_in_array ($url_slug, 'poly')==true)
{
$nav_theme_location='polystyrene';
$BannerClass='';
$post_type='polystyrene';
$galleryClass='';
}elseif(find_string_in_array ($url_slug, 'fiber')==true){
$nav_theme_location='fiberglass';
$BannerClass='fiberbanner';
$post_type='fiberglass_type';
$galleryClass='fiber';
}elseif (find_string_in_array ($url_slug, 'wood')==true) {
$nav_theme_location='woodwork';
$BannerClass='woodworkbanner';
$post_type='woodworks';
$galleryClass='wood';
}
elseif (find_string_in_array ($url_slug, 'creative')==true) {
$nav_theme_location='creative_production';
$BannerClass='creativebanner';
$post_type='creative_services';
$galleryClass='creative';
}elseif (find_string_in_array ($url_slug, 'f-and-b')==true) {
$nav_theme_location='fb';
$BannerClass='fandbbanner';
$post_type='creative_services';
$galleryClass='';
}else{
$nav_theme_location='';
$BannerClass='';
$post_type='';
$galleryClass='';
}
?>
<section class="banner inner-banner <?= $BannerClass;?>">
<!-- <div class="bannerBox" style="background-image: url(<?php // echo get_the_post_thumbnail_url())?get_the_post_thumbnail_url():;?>)"> -->
<div class="bannerBox" style="background-image: url(<?= (get_the_post_thumbnail_url()!='')?get_the_post_thumbnail_url():$grandParent_Image;?>)">
<div class="bannerContent">
<div class="overlay"></div>
<h1><?= $grandParent_title;?></h1>
</div>
</div>
</section>
<section class="secondrynavigation">
<div class="container">
<div class="innerMenu">
<?php
$args=array(
'theme_location'=>$nav_theme_location,
'container' =>false,
'menu_class' =>'',
);
wp_nav_menu($args);
?>
</div>
</div>
</section>
<section class="pad">
<div class="container-fluid">
<div class="innerHeading">
<h3><?php the_title();?></h3>
<p><?php
$content_post = get_post($post->ID);
echo $content = $content_post->post_content;?></p>
</div>
</div>
<div class="gallery <?= $galleryClass?>">
<div class="container">
<div class="row">
<?php
// echo $post_type;exit;
$loops = new WP_Query(array('posts_per_page'=> -1,
'post_type' => $post_type,
'orderby' => 'id',
'order' => 'asc',
'category_name'=>$uri_string));
// echo "<pre>";
// print_r($loops);exit;
if($loops->have_posts()):
while ($loops->have_posts()) :
$loops->the_post();
global $post;
$post_slug = $post->post_name;
?>
<div class="col-lg-4">
<a href="<?= get_permalink();?>">
<div class="galleryBox">
<div class="overlay">
<p><i>Read More</i></p>
</div>
<div class="image" style="background-image: url(<?= get_the_post_thumbnail_url();?>)"></div>
<div class="galleryText">
<h5><?php the_title();?></h5>
</div>
</div>
</a>
</div>
<?php endwhile;
endif;?>
</div>
</div>
</div>
</section>
<!--MY CODE-->
<?php
if( have_rows('services_accordion') ):
// loop through the rows of data for the tab header
while ( have_rows('services_accordion') ) : the_row();
// $title = get_sub_field('services_title');
// $description = get_sub_field('servises_description');
?>
<button class="accordion"><?php echo get_field('services_title'); ?></button>
<div class="panel">
<p><?php echo get_field('services_description'); ?></p>
</div>
<?php
endwhile;
else :
echo 'In progress';
endif; ?>
<!--MY CODE END-->
<?php get_footer();?>
In the end this is the result on the page https://prnt.sc/xfh0z7.
Thanks in advance!
Try wrapping your content into:
<?php while (have_posts()) : the_post(); ?>
// all your content related to the post including repeater code.
<?php endwhile; // End of the loop.?>
This way repeater will be in scope of your current post/page. Alternatively you can indicate post id as an argument in the repeater function.

WordPress Pagination Won't Switch Pages

I have been searching for a solution to this issue for over a week and I haven't been able to find anybody else with the same trouble.
I am working on a custom WP theme that somebody else built. There is a single-page template that I need to implement paging on for one of the secondary loops. I have been attempting to use the built-in paginate_links() function, as well as other methods. The pagination links show up, but when I click on a pagination link it doesn't go to that page in the pagination. Instead the original page is reloaded (i.e. instead of going to thewebsite.com/my-page/page/2/, it reloads thewebsite.com/my-page/).
The previous dev used this filter in functions.php to load the correct template:
add_filter('single_template', create_function('$t', 'foreach( (array) get_the_category() as $cat ) { if ( file_exists(TEMPLATEPATH . "/single-{$cat->slug}.php") ) return TEMPLATEPATH . "/single-{$cat->slug}.php"; } return $t;' ));
And here is my template file:
<?php
/**
* Template Name: Project Template
*/
get_header('news'); ?>
<article role="main" class="projectpage">
<div class="container">
<section class="pagecontent">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<section class="overview">
<h1><?php the_title(); ?></h1>
<div>
<?php the_content(); ?>
</div>
<div>
<?php if(get_post_meta($post->ID, 'pagelink', true)): ?>
Read the Overview
<?php endif; ?>
</div>
</div><!--end row-->
</section><!--end overview-->
<?php endwhile ?>
<?php wp_reset_postdata() ?>
<? endif ?>
<section class="related">
<div>
<h1> Related Resources </h1>
<h2> Explore our library of articles and resources </h2>
</div>
<div class="row">
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 relatedlinks">
<section class="projectcategories">
<h3> Categories </h3>
<ul>
<?php wp_list_categories( array(
'orderby' => 'id',
'show_count' => true,
'use_desc_for_title' => false,
'child_of' => 93,
'title_li' => ' '
) ); ?>
</ul>
</section>
<section class="project-search" role="search">
<form method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="hidden" name="cat" id="cat" value="93" />
<input type="text" size="16" name="s" placeholder="search keywords" class="search-box" />
<input type="submit" value="Go" class="go"/>
</form>
</section>
<section class="otherprojects">
<h3> Other Projects </h3>
<?php
$args = array(
'category__in' => 91,
'post__not_in' => array( $post->ID )
);
// the query
$query = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $query;
// The Loop
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<?php the_title(); ?>
<? endwhile ;
/* Restore original Post Data */
wp_reset_postdata();
endif;
$wp_query = NULL;
$wp_query = $temp_query;
?>
</section>
</div><!--end col 1-->
<div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
<section class="articles">
<?php
// THIS IS THE SECTION WHERE I NEED THE PAGINATION
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = [
'posts_per_page' => 3,
'paged' => $paged,
'post_type' => 'post',
'order' => 'DESC',
'post__not_in' => array( $post->ID ),
'tax_query' => [
[
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => '93',
],
],
];
$custom_query = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
if ( $custom_query->have_posts() ) {
while ( $custom_query->have_posts() ) {
$custom_query->the_post(); ?>
<div class="row">
<div class="col-md-2 col-sm-2 col-xs-2 divider">
<p class="date"><?php the_time('M j') ?></p>
</div><!--end col-->
<div class="col-md-4 col-sm-4 col-xs-4">
<div class="articleimg">
<?php if ( has_post_thumbnail()) {?>
<?php the_post_thumbnail('blog-thumb'); ?>
<?php } ?>
</div><!--end blogimg-->
</div><!--end col-->
<div class="col-md-6 col-sm-6 col-xs-6">
<div class="blogcontent">
<h3><?php the_title();?></h3>
<p><?php the_excerpt(); ?></p>
// read more
</div><!--end blogcontent-->
</div><!--end col-->
</div><!--end row-->
<?php }
}
echo paginate_links(array(
'total' => $wp_query->max_num_pages
));
$wp_query = NULL;
$wp_query = $temp_query;
wp_reset_postdata(); ?>
</section><!--end articles-->
</div><!--end col 2-->
</div> <!--end row-->
</section><!--end related-->
<!-- ANNOUNCEMENTS -->
<!--ANNOUNCEMENT SECTION -->
<!-- dynamic content --filters posts by category and only shows 'member' posts with a limit of six posts being
displayed-->
<section id="announcement-front" class="clearfix">
<div class="container">
<div>
<?php $query = new WP_Query('posts_per_page=1&category_name=advertisement');
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post(); ?>
<a href="<?php the_permalink()?>" <?php the_content();?> </a>
<?php endwhile ?>
<? endif ?>
<?php wp_reset_postdata() ?>
</div><!--end row-->
</div><!--container-->
</section><!--end announcement-->
</section> <!--end page content -->
</div><!--end container-->
</article>
<?php get_footer(); ?>
I realize there is a whole galaxy of WordPress pagination tutorials and threads out there, but I haven't been able to find one yet that solves this particular problem.
I think your code should mostly work. You don't need to worry about all the saving and switching of $wp_query though - have you tried this simpler snippet? It worked ok in my environment (though I had to modify the query args slightly to get it to return any results).
If that's not working can you post the HTML it generates?
<section class="articles">
<?php
// THIS IS THE SECTION WHERE I NEED THE PAGINATION
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = [
'posts_per_page' => 3,
'paged' => $paged,
'post_type' => 'post',
'order' => 'DESC',
'post__not_in' => array( $post->ID ),
'tax_query' => [
[
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => '93',
],
],
];
$custom_query = new WP_Query( $args );
if ( $custom_query->have_posts() ) {
while ( $custom_query->have_posts() ) {
$custom_query->the_post(); ?>
<div class="row">
<div class="col-md-2 col-sm-2 col-xs-2 divider">
<p class="date"><?php the_time('M j') ?></p>
</div><!--end col-->
<div class="col-md-4 col-sm-4 col-xs-4">
<div class="articleimg">
<?php if ( has_post_thumbnail()) {?>
<?php the_post_thumbnail('blog-thumb'); ?>
<?php } ?>
</div><!--end blogimg-->
</div><!--end col-->
<div class="col-md-6 col-sm-6 col-xs-6">
<div class="blogcontent">
<h3><?php the_title();?></h3>
<p><?php the_excerpt(); ?></p>
// read more
</div><!--end blogcontent-->
</div><!--end col-->
</div><!--end row-->
<?php }
}
echo paginate_links(array(
'total' => $custom_query->max_num_pages
));
wp_reset_postdata(); ?>
</section><!--end articles-->
</div><!--end col 2-->
</div> <!--end row-->
</section><!--end related-->
Thanks, Jo! That article pointed me in the right direction. I checked in dev tools and I was indeed getting a 301. I tried the code snippet from the article you pointed me to, and it didn't quite work, so I googled "fix pagination with redirect_canonical" and this was the first article that popped up. I took the function from there, threw it in to functions.php, et voilà! It's the same method, but, I think, not passing a custom post type into the conditional. I hope this can help someone in the future. It was a real pain. It was also a reminder of how very little I really know about WP and how much I want to learn about it! Thanks again.
Here's the code:
add_filter('redirect_canonical','custom_disable_redirect_canonical');
function custom_disable_redirect_canonical($redirect_url) {
if (is_paged() && is_singular()) $redirect_url = false;
return $redirect_url;
}

How do you print an entire post into a page (wordpress)

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>

Wordpress Custom post type_show post counts

I am working in a custom WP theme.I need to show each posts under individual categories, which is working fine.
I changed the category to taxonomy.Now, i want to show more info under each category name,but, i cannot understand,where should i put my code in the loop.
Specially the post counts under each categories.
<?php
/*
Template Name: Home Page
*/
get_header();
global $redux_demo;
?>
<div class="sroll"><div class="container">
<marquee><p> <?php echo $redux_demo['main-option-marquee']; ?></p></marquee>
</div></div>
<div class="container">
<div class="row">
<div class="col-sm-9"> </br>
<div class="content mCustomScrollbar" style="height: 690px;">
<?php
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
) );
foreach($terms as $cat){
$cata_name = $cat->name;
$term_id = $cat->term_id;
?>
<div class="col-sm-6 col-md-4 col-lg-3 p10">
<div class="box">
<?php
//echo '<h3>'.$catname[0]->cat_name.'</h3>';
?><h3><a href="<?php echo home_url('index.php/category/'.$cata_name) ?>">
<?php echo $cata_name; ?></a></h3> <?php
$catqueryy = new WP_Query( 'cat='.$term_id.'&posts_per_page=4' );
while($catqueryy->have_posts()) : $catqueryy->the_post();
?>
<p class="post_title"><?php echo ''.__(get_the_title(),'rockon').''; ?></p>
<p class="post_cont"><?php echo get_the_excerpt(); ?></p>
<?php
endwhile;
?>
</div>
</div>
<?php } ?>
</div></br>
</div>
<div class="col-sm-3">
<h1></h1>
<?php get_sidebar(); ?>
<h1></h1>
</div>
</div>
</div>
<?php
get_footer();
?>
Custom taxonomy try:
$the_query = new WP_Query( array(
'post_type' => 'CUSTOM_POST_TYPE',
'tax_query' => array(
array(
'taxonomy' => 'CUSTOM_TAXONOMY',
'field' => 'id',
'terms' => TERM_ID
)
)
) );
$count = $the_query->found_posts;
https://wordpress.org/support/topic/counting-posts-within-categories
For Example:
<?php
/*
Template Name: Home Page
*/
get_header();
global $redux_demo;
?>
<div class="sroll"><div class="container">
<marquee><p> <?php echo $redux_demo['main-option-marquee']; ?></p></marquee>
</div></div>
<div class="container">
<div class="row">
<div class="col-sm-9"> </br>
<div class="content mCustomScrollbar" style="height: 690px;">
<?php
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
) );
foreach($terms as $cat){
$cata_name = $cat->name;
$term_id = $cat->term_id;
?>
<div class="col-sm-6 col-md-4 col-lg-3 p10">
<div class="box">
<?php
//echo '<h3>'.$catname[0]->cat_name.'</h3>';
?><h3><a href="<?php echo home_url('index.php/category/'.$cata_name) ?>">
<?php echo $cata_name; ?></a></h3>
<?php
$catqueryy = new WP_Query( 'cat='.$term_id.'&posts_per_page=4' );
$count = $catqueryy->found_posts;
?>
<h3><?php echo "Post Count : ".$count; ?></h3>
<?php
while($catqueryy->have_posts()) : $catqueryy->the_post();
?>
<p class="post_title"><?php echo ''.__(get_the_title(),'rockon').''; ?></p>
<p class="post_cont"><?php echo get_the_excerpt(); ?></p>
<?php
endwhile;
?>
</div>
</div>
<?php } ?>
</div></br>
</div>
<div class="col-sm-3">
<h1></h1>
<?php get_sidebar(); ?>
<h1></h1>
</div>
</div>
</div>
<?php
get_footer();
?>
You can check this Link enter link description here
if it helps.. I guess, some one will write this code here as I also need to understand how it will work after you pass a query.
Actually you do not need to count anything by manually written code. If you look at the get_terms() description, you can see that WP counts this for you (if you set 'pad_counts' to true (or 1)). With this turned on there will be a "count" key and a number value in your response array with each category.
You can just "echo" it where you want to.
This way your
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
) );
should look like this:
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
'pad_counts' => true,
) );
Notice that I added 'pad_counts' => true, to the query, so you will have the number you are looking for - without writing too much code.
If you want to do it manually, I suggest that you create a loop that fills an array with 'category => number' elements, and look up the required key-value pairs in the loop that writes out the HTML.
I was also searching for something similar to this question. I think, you need a code to display the post counts of each categories.

ACF CPT two columns with each category

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>

Categories