Can PHP 'include' be used to duplicate PHP + ACF section in WP? - php

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

Related

get_post_meta() not display field value for first item in wordpress loop

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!

Associate template to Advanced Custom Fields post type

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.

How to display ACF plugin fields in several pages

I created a group of fields in ACF plugin and this group belongs only to one page (for example page-contacts.php), but I want to display this group of field on several pages (for example page-main.php). Here is repeater field loop which is on the page-contact.php.
<?php if( have_rows('slider') ): ?>
<div id = "events-slider">
<?php while( have_rows('slider') ): the_row(); ?>
<div class = "events-slider__slide">
<img src="<?php the_sub_field('image'); ?>" alt>
</div>
<?php endwhile;?>
</div>
<?php endif;?>
How can I use this loop on page-main.php?
To get a field from another page, you can add the pages ID as a second parameter :
have_rows($field_name, $post_id);
Then you just need the post ID from the page, where the correct content is held -
Good luck!

Navigating through WordPress custom posts

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!

Wordpress - Posts and Pages

I am currently developing a mutlisite wordpress setup, each site is a different language (EG: site.com, site.dk etc).
The sites consist of a number of pages which contain static content, however I also want to include posts (a blog) into both of the sites.
Q1. Is it possible to create a page that shows all the posts listed by latest publish date, with a dropdown that filters by category? How do I do this? Do I need to refer to loop.php?
Basically it should return the following code for all post articles...
<article class="post">
<a href="<URL Link to Post Article>" rel="bookmark">
<figure>
<img title="<Post Title>" alt="<Post Title>" src="<http://url/PostImage.jpg>" width="900" height="600" />
</figure>
<div class="cover">
<h2>Post Title</h2>
<time pubdate="2013-03-27T21:09:59+00:00">November 18, 2012</time>
</div>
</a>
</article>
Why doesn't this work? It doesn't return anything?
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<!-- article -->
<article class="post">
<?php get_posts(); ?>
<div id="grid-switcher">
featured
latest
</div>
<div id="view-blocks">
<div id="latest-post" class="post-grid active">
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail(array(250,250)); // Declare pixel size you need inside the array ?>
<?php endif; ?>
<!-- /post thumbnail -->
<div class="cover">
<h2><?php the_title(); ?></h2>
<time pubdate="<?php the_date(); ?>"><?php the_date('Y-m-d', '<h2>', '</h2>'); ?></time>
</div>
</a>
I have already created a page template portfolio-page.php for the above but cannot findout how to loop through the posts and return them with the above code?
Q2. How do I return a dropdown list with all the categories?
Q3. How do I filter by category from the dropdown list?
Thanks for any help! :)
Sorry I am new to PHP and wordpress...
The question is a bit vague so I will just share some starting points:
A1) To get a list of posts you can use the get_posts function from wordpress. It already sorts them by the post date desc and you can also add a parameter for the category if one is picked.
A2) To get a dropdown of categories there's a function that does exactly that: wp_dropdown_categories
Check the docs as there are a lot of parameters that can be used with the get_posts function so you should be able to filter them as you wish.
I think its a good idea for you to check the database you have wordpress running on.
if you check the wp_posts table you can see the differend fields that you can filter on.
You will be better off using custom query's to filter te posts and order them

Categories