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.
Related
I'm building a theme and on my category.php page I want to show several full posts (let's say 3, but need to be able to change this to 2 or 1 easily), and then the rest of the posts in the category as title links.
I have quite a bit of HTML in my loop for styling my posts and adding custom fields so sorry about all the code, but this is what my category.php page looks like now. I've tried a few things that haven't worked so have edited this to show my original code which just has a normal list of posts. I'm somewhat new to editing The Loop so would appreciate as much explanation/clarity as possible.
<?php
/**
* The template for displaying Category Archive pages.
*/
get_header(); ?>
<div id="primary" class="<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>">
<div id="feature-container" class="full-width-container">
<div class="full-width-container content-page" id="tagline-wrapper">
<div id="left-black"></div>
<div class="page-width-container">
<div id="tagline-box">
<h1 class="category-title">Transactions</h1>
</div>
</div>
</div>
</div>
<div id="content-wrapper">
<div id="project-menu" class="page-width-container">
<?php wp_nav_menu( array( 'theme_location' => 'project-types' ) ); ?>
</div>
<div id="content" role="main" >
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="story-container" class="module-container">
<div class="our-story">
<div class="story-image">
<?php
// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
</div>
<div class="story-text">
<article class="post" id="post-<?php the_ID(); ?>">
<div class="entry-container">
<h2><?php the_title(); ?></h2>
<div class="project-details">
<p><span class="details-location"><?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_project-location', true);
wp_reset_query();
?></span><br />
<span class="details-funding"><?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_funding-type', true);
wp_reset_query();
?> | <?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_funding-source', true);
wp_reset_query();
?></span><br />
<span class="details-value"><?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_project-value', true);
wp_reset_query();
?></span></p>
</div>
<div class="entry">
<?php the_content(); ?>
<?php wp_link_pages(array('before' => __('Pages: ','html5reset'), 'next_or_number' => 'number')); ?>
</div>
<?php edit_post_link(__('Edit this entry','html5reset'), '<p>', '</p>'); ?>
</div>
</article>
</div>
</div>
</div>
<?php endwhile; endif; ?>
</div><!-- #content -->
</div>
</div><!-- #primary -->
<?php get_footer(); ?>
You can achive above thing using following code:
First you have to loop all post and and put counter when it reach more then 2 its stop to print a content.but title will be there always.
<?php $countPost=1;?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<h2 id="post-<?php the_ID(); ?>">
<?php the_title(); ?></h2>
<?php if($countPost>2) : /*Condition for Content*/
the_content();
endif;
?>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft">
<?php posts_nav_link('','','« Previous Entries') ?>
</div>
<div class="alignright">
<?php posts_nav_link('','Next Entries »','') ?>
</div>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center"><?php _e("Sorry, but you are looking for something that isn't here."); ?></p>
<?php endif; ?>
</div>
For more details please refer :
https://codex.wordpress.org/The_Loop_in_Action
I figured out a bit of a workaround solution on my own, although it relies on using plugins/widgets which isn't what I'd prefer.
I simply set the Reading settings to display 2 posts, and then below the Loop I added a widget area and used the Recent Posts Extended widget to display a list of titles/links. This widget allows you to skip a certain amount of posts in the list, so I set it to start at post #3. There was no option to show posts from the current category only, so I had to use the Widget Context plugin as well and make individual widgets with a specific category to show on each corresponding category page. As I said, a bit of a convoluted solution, but the end result is exactly what I wanted to achieve.
I've been trying to get this to work all day. I want add some custom HTML depending on what category the post is in Wordpress from a separate directory to keep things clean. I can't seem to load the html file.
The relevant code is:
<div class="container">
<?php while ( have_posts() ) : the_post(); ?>
<div class="row clearfix">
<?php if (function_exists('breadcrumbs')) breadcrumbs(); ?>
<div class="title clearfix">
<?php global $post; $category = get_the_category($post->ID); setPostViews($post->ID); ?>
<h1><?php echo $category[0]->name;?></h1>
</div>
<div class="detailBlock clearfix">
<h1><?php the_title()?></h1>
<div class="detail-inner">
<?php $featured_img = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full', true);
if($featured_img) {
echo "<div class='detail-img'><img src='$featured_img[0]' alt='".get_the_title()."'></div>";
echo $category[0]->ID ;
}
the_content();
?>
<?php
/* TEST CODE */
global $post; $category = get_the_category($post->ID);
if($category[0] == '6'){
include( get_template_directory_uri() '/call-to-actions/cta-weight-loss-fem.html');
}
?>
</div>
</div>
<?php endwhile;?>
</div>
Change:
if($category[0] == '6'){
to
if($category[0]->ID == '6'){
and that should work.
Note: you don't need the line above to get the category as you already have it from before. A better idea would be to store the value as a variable.
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.
In a custom WordPress theme, I am using WP_Query to pull custom post type info on the home page. I am storing some data in variables, ending the custom query and loop, resetting the query, and then calling the variables later in the page.
oEmbed is working for the custom post types on the single post pages. On the home page, though, if I include a YouTube link for instance, oEmbed does not work when I get the custom post type content through the variable.
Does storing content in this way bypass oEmbed? I haven't been able to find a specific conversation on this.
You can see the home page template here:
<?php
/*
Template Name: Home
*/
get_header();
?>
<!-- Row for main content area -->
<section id="top">
<div class="row">
<article id="npo-info" class="small-12 small-centered large-6 large-uncentered columns">
<?php // loop for latest weekly NPO to store info for later use
$args = array( 'post_type' => 'weeklyNPO', 'posts_per_page' => 1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$NPOtitle = get_the_title();
$NPOcontent = get_the_content();
$NPOexcerpt = get_the_excerpt();
$NPOthumbnail = get_the_post_thumbnail();
$NPOid = get_the_ID();
endwhile;
?>
<header>
<h4>Karmasapien presents</h4>
<h1><?php echo $NPOtitle; ?></h1>
</header>
<section>
<p><?php echo $NPOexcerpt; ?></p>
<hr/>
<label><p>Because Giving Feels Good</p></label>
<?php dynamic_sidebar( 'Donation Progress Bar' ); ?>
<?php the_meta(); ?>
<?php wp_reset_query(); ?>
</section>
<footer>
<em id="ks-clock" src=""></em>
<?php echo do_shortcode( '[fergcorp_cdt max=1]' ); ?>
</footer>
</article>
</div>
</section> <!-- end top -->
...other magical things happen, and then...
<section id="weekly-npo">
<div class="image-bg"></div>
<div class="row">
<article <?php post_class('small-12 small-centered large-10 columns') ?>>
<header>
<h2>More About <?php echo $NPOtitle; ?></h2>
</header>
<section>
<?php echo $NPOcontent; ?>
</section>
<hr/>
<footer class="row">
<div class="small-5 small-centered large-3 columns">
<?php echo $NPOthumbnail; ?>
</div>
</footer>
</article>
</div>
</section>
<?php get_footer(); ?>
Cheers,
Sounds like you have filter/shortcode in the_content, try one of these and I believe the problem will be solved, apply_filters
$NPOcontent = apply_filters('the_content', get_the_content());
Or use do_shortcode
$NPOcontent = do_shortcode(get_the_content());
I need a slider working inside a loop for that I am using the below code
while ( have_posts() ) : the_post(); ?>
<div id="content">
<?php
$attachments = get_children( array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'order' => '',
'post_mime_type' => 'image')
);
echo "aaaaa".count($attachments);
?>
<?php if(count($attachments)!=0){ ?>
<div id="portfolio-gallery">
<ul id="slideshow_detail">
<?php
foreach ( $attachments as $att_id => $attachment ) {
$getimage = wp_get_attachment_image_src($att_id, 'room-gallery', true);
$roomimage = $getimage[0];
echo '<li>
<h3></h3>
<span>'.$roomimage.'</span>
<p></p>
<img src="'.$roomimage.'" alt="" class="thumb-slide" />
</li>';
}
?>
</ul>
<div id="wrapper1">
<div id="fullsize">
<div id="imgprev" class="imgnav" title="Previous Image"></div>
<div id="imglink"></div>
<div id="imgnext" class="imgnav" title="Next Image"></div>
<div id="image"></div>
<div id="information">
<h3></h3>
<p></p>
</div>
</div>
<div id="thumbnails">
<div id="slideleft" title="Slide Left"></div>
<div id="slidearea">
<div id="slider-room"></div>
</div>
<div id="slideright" title="Slide Right"></div>
</div>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
<!--
$('slideshow_detail').style.display='none';
$('wrapper1').style.display='block';
var slideshow_detail=new TINY.slideshow_detail("slideshow_detail");
window.onload=function(){
slideshow_detail.auto=true;
slideshow_detail.speed=5;
slideshow_detail.link="linkhover";
slideshow_detail.info="information";
slideshow_detail.thumbs="slider-room";
slideshow_detail.left="slideleft";
slideshow_detail.right="slideright";
slideshow_detail.scrollSpeed=4;
slideshow_detail.spacing=25;
slideshow_detail.active="#fff";
slideshow_detail.init("slideshow_detail","image","imgprev","imgnext","imglink");
}
//-->
</script>
</div>
<?php endwhile;?>
By using this code slider is working only once;I need a slider that working under a loop .
ie I have to add multiple post,each post having multiple images .I have to show details of each post and showing images of each post using a slider
I feel maybe the while loop, beginning at the top could be the problem. Why not use an if statement. I'm not familiar with your "while" syntax. I've only ever seen the following:
<?php while( someCondition() ): ?>
//statements in here
<?php endwhile; ?>
Or maybe try an if statement instead:
<?php if ( have_posts() ): ?>
//all slider code here
<?php } else { ?>
//give a 'there are no posts' message
<?php endif; ?>
also what wordpress plugin are you running? Without knowing that, it's a bit difficult to trouble shoot this problem.