I am using the ACF plugin to create custom fields for my wordpress site. Everything is going well with zero issues when I keep my code all on the same page. However, when I break the code into template parts, the ACF’s for each section’s hero and custom fields stopped outputting data. It’s almost as if being in template parts is making the fields read as empty, but when I reorder the get_template_parts php (ie, move content-rolls before content-pizza or vice versa), whichever part comes first displays correctly while the second does not.
<?php get_template_part('template-parts/home/content', 'callout'); ?>
<?php get_template_part('template-parts/home/content', 'menu'); ?>
<?php get_template_part('template-parts/home/content', 'pizza'); ?>
<?php get_template_part('template-parts/home/content', 'rolls'); ?>
The same thing happens if I move content-menu.php under content-pizza.php – the ACF’s stop showing that anything is filled into them on the backend. I suspect the issue is in the content-pizza.php (content-rolls.php is the same code copied, with the correct variables filled in), but I can’t see what the issue is.
The content-pizza.php page coding is:
<?php
/**
* Template part for displaying pizza content in page-home.php
*
* #link https://developer.wordpress.org/themes/basics/template-hierarchy/
*
* #package JJs_Pizza
*/
// Variables
$pizza_hero = get_field('pizza_hero');
$pizza_custom = get_field('pizza_custom');
$pizza_args = array(
'posts_per_page' => 50,
'post_type' => 'food',
'category_name' => 'pizza',
'orderby' => 'post_id',
'order' => 'ASC'
);
?>
<!-- Pizza Section -->
<section class="menu-pizza mt-4" id="pizza">
<div class="row no-gutters p-3">
<div class="col">
<?php if( !empty($pizza_hero) ) : ?>
<?php echo($pizza_hero); ?>
<?php endif; ?>
</div>
</div>
<div class="row p-3 justify-content-center">
<?php if ( !empty($pizza_custom) ) : ?>
<div class="col-lg-4 col-sm-6 col-xs-12 custom-menu pt-2">
<?php echo($pizza_custom);?>
</div>
<?php endif; ?>
<?php $loop = new WP_Query($pizza_args);?>
<?php while( $loop ->have_posts() ) : $loop->the_post(); ?>
<div class="col-lg-4 col-sm-6 col-xs-12 menu p-4">
<div class="row">
<div class="col d-flex justify-content-center">
<?php if (get_field('image')):?>
<img src=”<?php the_field('image'); ?>" class="img-fluid">
<?php endif; ?>
</div>
</div>
<div class="row">
<div class="col">
<h3 class="text-center"><?php the_title();?></h3>
<p class="text-center mx-auto">
<?php
$labels = get_field(‘labels’);
if( $labels && in_array(‘vegetarian’, $labels) ) {
echo(‘<i class="fas fa-leaf"></i>');
}
?>
<?php
$labels = get_field(‘labels’);
if( $labels && in_array(‘spicy’, $labels) ) {
echo(‘<i class="fas fa-fire"></i>');
}
?>
</p>
<?php if (get_field('description')):?>
<p class="text-center"><em><?php the_field('description');?></em></p>
<?php endif; ?>
</div>
</div>
</div>
<?php endwhile;?>
</div>
</section><!-- End Pizza Section -->
I also uploaded the code I have so far to a live site at https://demo.jjs-pizza.com/ but so far, the only dynamic sections I’ve coded are the pizza and pizza rolls section so you can get an idea of the issue.
Thank you in advance for any help! If worse comes to it, I’ll just nix the template parts and work from the same page, but for the sake of cleaner code, I’m hoping for a solution.
You are calling WP_Query in your templates and not resetting the post data afterwards. I suspect it is something to do with this, although it is difficult to tell without seeing all of the code, how the fields are set up and on what pages, and how it all works together.
If we look at what's happening in the code:
First, your content-pizza.php template part gets executed
It gets the pizza_hero and pizza_custom fields for the current post (presumably set on the homepage?)
Then it does a custom WP_Query and loops through all of the posts in it
Next up your content-rolls.php template part gets called. Assuming is is the same code as content-pizza...
it then to get the pizza_hero and pizza_custom fields for the current post. However because the code doesn't reset the post data after the WP_Query, the current post is still the last post from that loop.
I'm not sure what post this is supposed to be calling get_field on, but I suspect it isn't the post that is still set up as the current post after the custom query.
When you are finished with a custom loop, you can call wp_reset_postdata to reset the post data after you have finished the loop for that query, like this:
<?php $loop = new WP_Query($pizza_args);?>
<?php while( $loop ->have_posts() ) : $loop->the_post(); ?>
// do stuff...
<?php endwhile;?>
<?php wp_reset_postdata(); /* RESET POST DATA HERE */ ?>
You can find out more about wp_reset_postdata in the WP Code Reference for it
Try adding wp_reset_query() after each loop! You need to destroy the existing query to run a new one.
Related
I have a WordPress custom field that I use to assign a custom class to some scrollable elements to obtain a landor style effect. I have a problem with the get_post_meta() function, It will display only the field value for the second post of the loop, but not the first one. I'm using a custom post type, but I don't think that this is the problem. Is there any solution?
Here is the code:
<?php $item = new WP_Query( ['post_type' => 'home-slider', 'posts_per_page' => 3] ); ?>
<?php if( $item->have_posts() ): while( $item->have_posts() ): $item->the_post(); ?>
<?php $class = get_post_meta($post->ID,'class',true); ?>
<div class="col-sm-12 col-md-12 col-lg-12 img-<?php echo $class; ?>"> <!-- custom class -->
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12 img-text">
<h1 class="">Hello</h1>
<p class="lead">Nice to meet you.</p>
</div>
</div>
<img class="img-fluid bg-img" src="<?php the_post_thumbnail_url(); ?>">
</div>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
You need change the $post->ID to get_the_ID().
Hope help.
I wasn't able to find a solution to the problem. To test if this was an issue related to the post type I've registered a new one and I've added the same field to manage the custom classes needed. The result is that all works fine, I think that there is a problem with the post type where I was trying to add the custom field, because I've added the custom field support later than the post type was created and inserted inside the wordpress database.
Thanks for the help!
I'm creating a custom WP theme - within the site, there is an employee section. Using 'Advanced Custom Fields' repeater, I've made it possible for WP users to go to a page and add/change/delete employee members.
I'm wanting this employee section to be added to other places on the website, but only needs to be updated in one place - rather than having to go into several pages to make the same changes.
I'm relatively new to WP dev and PHP, but here's what I've tried:
I've created a new php file with only the employee section:
<?php /* Template Name: StaffSection */ ?>
<h1>Testing</h1><!-- This line runs fine -->
<?php<!-- None of this runs -->
// check if the repeater field has rows of data
if( have_rows('employees') ):
// loop through the rows of data
while ( have_rows('employees') ) : the_row(); ?>
<div class="col-lg-3 col-md-6 gap">
<a href="<?php the_sub_field('employee-link'); ?>">
<img class="leadership-img" src="<?php the_sub_field('employee-image'); ?>">
<h4 class="position"><?php the_sub_field('employee-name'); ?></h4>
</a>
<p class="position"><?php the_sub_field('employee-title'); ?></p>
</div>
<?php endwhile;
else :
// no rows found
endif; ?>
On the page that I'm wanting to 'include' this section on:
<section id="leadership" class="section">
<div class="container-fluid">
<div class="wrapper">
<div class="row leadership-section">
<?php include 'staff-section.php'; ?>
</div>
</div>
</div>
</section>
Within WP, I've created a new WP page and linked it to the 'StaffSection' template that I created. I have 'Advanced Custom Fields' on that page to pull content that WP users define.
I know the 'include' function is working with that test h1 tag, but any idea why it isn't reading the php repeater loop below that?
It could be that if ( have_rows('employees') )... etc etc is returning false because there is no 'employees' repeater that belongs to the post object defined within the loop.
One solution I use to make a field that is displayed across many pages is to create a secondary query to retrieve the repeater.
For instance, we can do this.
1. create a post with the category 'employees'
2. Go to ACF and set the logic so the repeater only appears on posts with category 'employees'
3. Query post object with category 'employees'
4. Access repeater from within the query
<?php
$repeater_query = new WP_Query(array('category_name' => 'employees'))
if ($repeater_query->have_posts() ) {
while ($repeater_query->have_posts() ) {
$repeater_query->the_post();
// check if the repeater field has rows of data
if( have_rows('employees') ):
// loop through the rows of data
while ( have_rows('employees') ) : the_row(); ?>
<div class="col-lg-3 col-md-6 gap">
<a href="<?php the_sub_field('employee-link'); ?>">
<img class="leadership-img" src="<?php the_sub_field('employee-image'); ?>">
<h4 class="position"><?php the_sub_field('employee-name'); ?></h4>
</a>
<p class="position"><?php the_sub_field('employee-title'); ?></p>
</div>
<?php endwhile;
else :
// no rows found
endif;
}
} wp_reset_postdata();
I hope this helps. Cheers
I'm trying to use the advanced custom fields plugin and associate it with a partial (probably not the right word) that I wrote that would display the fields from the custom post type.
How do I associate a php file with the custom post type?
The objective is to be able to embed the partial in a post.
Here is my partial: cta-partial.php
<?php
/*
Template Name: CTA-Partial
*/
?>
<!-- cta markup -->
<!-- if there is an article then load -->
<?php
$args = array(
'post_type' => 'cta_post_type'
);
$query = new WP_Query( $args ); ?>
<?php if( $query->have_posts() ) : while( $query->have_posts() ) :
$query->the_post();?>
</div>
</div>
<!-- end the markup so as to allow full-width -->
<div class="article" style="background-image: url('<?php the_field('cta_background'); ?>')">
<div class="custom-background">
<div class="columns small-12 medium-6 image-container">
<div class="hide-for-medium">
<img src="<?php the_field('cta_background'); ?>">
</div>
</div>
<div class="columns small-12 medium-6 mobile-style">
<h1> <?php the_field('cta_title');?></h1>
<p><?php the_field('cta_text'); ?></p>
<button><?php the_field('button_label')?></button>
</div>
</div>
</div>
<div class="row">
<div class="columns small-12 medium-12 content-wrapper">
<!-- continue content loop -->
<?php endwhile; endif; wp_reset_postdata(); ?>
This is the file i'd like to render the php associated with my custom fields post type.
Here is the documentation for ACF: https://www.advancedcustomfields.com/resources/
I've been scouring the docs and i'm not sure if it's possible. Thank you for the help so far, still researching.
image of embedded cta in post
I assumed that what you want is assigned a page to that file. You can use a archive-template.php type file.
https://codex.wordpress.org/Creating_an_Archive_Index
You can pass a post id (or user id, or term, or other field) to an ACF field to retrieve the field located elsewhere. And as a side note, it looks like WP_Query is overkill for this. Try this:
<?php
$cta_posts = get_posts( array(
'posts_per_page' => 1,
'post_type' => 'cta_post_type'
) );
if( $cta_posts ){
foreach( $cta_posts as $cta_post ):
?>
<div class="article" style="background-image: url(<?php the_field( 'cta_background', $cta_post->ID ); ?>)">
...
</div>
<?php
endforeach;
}
My posts_per_page assumes you just want one CTA on the page. And if that's the case, a post type may also be overkill. You may want to look at an ACF options page instead, put your fields on the options page, and then call them with:
<div class="article" style="background-image: url(<?php the_field( 'cta_background', 'option' ); ?>)">
I don't think you need to have a query in your cta-partial.php partial.
If you are using <?php get_template_part('cta-partial.php'); ?> assigned variables aren't available in the partial.
If you use <?php include( locate_template('cta-partial.php') ); ?> instead, then you do have access to variables assigned from the page or post.
In your partial, you might need to adjust your the_field() usage to pull in current post id. Like this:
<?php the_field('cta_background', $page_or_post_id); ?>
Just setup that $page_or_post_id in the original page where you call in the partial.
Having a little issue with some PHP code, my skills in PHP aren't the strongest and I'm a little stuck at the moment.
I am working on a site which uses posts for a news page.
We are building a blg page, and I want to use posts as well, to make it simpler for the user, however I cant seem to get the page to use just one category, its pulling every single post no matter what the category.
Here is my code for the page template.
<div class="container">
<div>
<br /><?php echo pageWidgets($post->ID,'main',400); ?>
</div>
<?php /*?> <div class="contentTop2 newspad">
<?php
$args = array( 'numberposts' => 1,'category_name'=> "blog", 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<?php $exclude_first_post = $post->ID; ?>
<div class="postNews">
<?php if ( has_post_thumbnail() ) { /* loads the post's featured thumbnail, requires Wordpress 3.0+
echo '<div class="featured-thumbnail">'; the_post_thumbnail(); echo '</div>';
} ?>
<div class="newsRightPost">
<div class="newsTitleTop"><?php the_title(); ?></div>
<div class="newsDateTop">Posted <?php the_date(); ?></div>
<div class="newsExcerptTop"><?php the_excerpt(); ?></div>
<div class="readMoreText">Read More >></div>
</div>
<div class="clearBoth"> </div>
</div>
<?php endforeach; ?>
<div class="clearBoth"> </div>
</div>
<?php */?>
I thought 'category_name'=> "blog" would sort it out for me, however it doesn't seem to work at all.
I get the blog category, but I also get every other category.
Can anyone point me in the right direction?
Thanks guys!
I want to redesign my website using WordPress and Bootstrap. I did most of the work, however, I have trouble navigating through my pages.
Before I tell you anything, just want you to know that my "tutorials" is "tutorijali" and my "tutorial" is "tutorijal". So just added that "j" there (because I'm not from England or English-speaking country).
OK, so, I made a custom post type "tutorijal". But, I noticed that when the custom post type's name is "tutorijal", I get the URL like this :
domain-name/tutorijal/
...
Since "tutorijal" is singular and "tutorijali" is plural, I changed the custom post type's name to "tutorijali" (so that my URL goes as I want it to go).
Then I made page-tutorijali.php and applied that theme (which I named "Tutorijali theme") to my XHTML and CSS page. But, what happens is that I have a conflict.
In WordPress, I have a Wordpress page named "Tutorijali" and my custom post type's name is also "tutorijali".
You can see my navigation structure here and notice that I have a WordPress page which is named the same as my custom post type, "Tutorijali" : http://imgur.com/2zgH5dD
If I go to
domain-name/tutorijali/xhtml-and-css
I get my default post template and not the "Tutorijali theme" I applied. However, that "Tutorijali theme" is applied on Tutorijali WordPress page, without me ever setting it to be applied.
So, if you go to
domain-name.com/tutorijali
you can see the ordered list with all of the tutorials, however, if you go to a specific sub-item of the WordPress page "Tutorijali", you get the default post template, even if you specifically change that WordPress page (from the Admin panel) to "Tutorijali template".
Maybe my WordPress page and my custom post type can't share same name, I'm not sure.
Now, I when the user goes to
domain-name/tutorijali
I'd like him to have only the names of tutorial series, but if he goes on one specific serie, let's say XHTML and CSS, then the URL would be
domain-name/tutorijali/xhtml-and-css
and the user would get all of the XHTML and CSS tutorials in ordered list.
I have a parent custom post which is named "XHTML and CSS" (as my tutorial serie name) and it's child elements (also custom posts named "tutorijali") are specific tutorials from that serie.
I also have one other problem, and that is that my list is also displaying the parent post, so my list goes like this :
XHTML and CSS (since it's "order" value is set to 0)
Installing the program ("order" set to 1 and so on)
Tags
Basic elements of a website ...
So I'd like to get rid of that and obtain the structure I wanted (on Tutorijali - tutorial serie names, on specific tutorial serie - list of the tutorials).
Here's the code for my page-tutorijali.php :
<?php
/*
Template Name: Tutorijali Template
*/
?>
<?php get_header(); ?>
<div class="container">
<div class="row">
<div class="col-md-12">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="page-header">
<h1><?php the_title(); ?></h1>
</div>
<?php the_content(); ?>
<?php endwhile; else :?>
<div class="page-header">
<h1>O ne!</h1>
</div>
<p>Na ovoj stranici nema sadržaja.</p>
<?php endif; ?>
</div>
</div>
<div class="row">
<div class="col-md-12">
<?php
$args = array( 'post_type' => 'tutorijali', 'order' => 'asc', 'posts_per_page' => -1 );
$the_query = new WP_Query( $args );
echo "<ol>";
if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
echo "<li>";
echo "<a href='".get_permalink()."'>".get_the_title()."</a>";
echo "</li>";
endwhile; endif;
echo "</ol>";
?>
</div>
</div>
<?php get_footer(); ?>
I also have a question regarding my single-tutorijali.php. I have created it to display these custom post pages differently than "normal" posts. I link to the previous and to the next tutorial well, however, I don't know how I can link from a tutorial to the list.
So let's say the user is at the tutorial "Installing the program", URL would go :
domain-name/tutorijali/xhtml-and-css/installing-the-program
and if he clicks on the link to take him back to the list, he would go to :
domain-name/tutorijali/xhtml-and-css
but I have trouble doing that, so maybe you can tell me how I can do that.
Here's the code for my single-tutorijali.php :
<?php get_header(); ?>
<div class="container">
<div class="row">
<div class="col-md-12">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="page-header">
<h1><?php the_title(); ?></h1>
</div>
<?php the_content(); ?>
<?php endwhile; else :?>
<div class="page-header">
<h1>O ne!</h1>
</div>
<p>Na ovoj stranici nema sadržaja.</p>
<?php endif; ?>
</div>
</div>
<div class="row">
<div class="col-md-12 yt-link">
<p> <a class="btn brn-large btn-primary" href="<?php the_field('link'); ?>"> Pogledajte tutorijal na Youtube -u </a> </p>
</div>
</div>
<div class="row">
<div class="col-xs-12 prev-next">
<?php previous_post_link('%link', '<span class="glyphicon glyphicon-circle-arrow-left"></span>'); ?>
/-- this is that link I was talking about, one that takes you back to the list of tutorials
<span class="glyphicon glyphicon-th-list"></span>
/--
<?php next_post_link('%link', '<span class="glyphicon glyphicon-circle-arrow-right"></span>'); ?>
</div>
</div>
<?php get_footer(); ?>
Thank you for reading!