ACF Post Object if all selected posts not 'publish' - php

I'm having trouble wrapping my head around this on a WordPress site using ACF pro. Essentially I have an ACF Post Object field that allows the user to manually select events posts that are related to the current event post and displays them on the page. The users will draft events once they have past instead of deleting them. Everything is working fine except in the instance that all the related events have been "drafted" than I don't know how to do a check to remove the parent section. So if all the selected posts are changed to draft I'm not just left with just <h2>Related Events</h2>.
This is my current code:
<?php
$featured_posts = get_field('post_summary_events');
if (!empty($featured_posts)) : ?>
<div id="related-events">
<h2>Related Events</h2>
<?php foreach ($featured_posts as $post) :
// Setup this post for WP functions (variable must be named $post).
setup_postdata($post); ?>
<?php if (get_post_status() == 'publish') : ?>
<a href="<?php the_permalink(); ?>" class="upcoming_event latest_box">
<p>
<time class="upcoming_event_date date_stamp"><?php the_field('event_date'); ?></time><br>
<span class="upcoming_event_title"><?php the_title(); ?></span>
</p>
</a>
<?php endif; ?>
<?php endforeach; ?>
<?php
// Reset the global post object so that the rest of the page works correctly.
wp_reset_postdata(); ?>
</div>
<?php endif; ?>
Wondering if there is a way to verify they are published before the loop and plug these into a variable? Sort of post status=> publish in an array then go if (!empty($featured_posts)) :, but that didn't seem to work out.

Related

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

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

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!

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

Wordpress - outputting relevent posts in sidebar.php on selected posts

This seems like something that should be really easy, I want to output the latest three events when on an event post and the latest three news items when on a news post, I've got this working in a very basic sense however I can work out how to do the following:
When on a post in events show the latest three events, when on a post in news show the latest three news posts. I've looked on here http://codex.wordpress.org/Conditional_Tags and thought that is_category('news') and is_category('events') would do the trick however these conditionals seem to only be available on category archive pages.
I currently have the following code which simple checks you're on a single post page (I'm only using posts for news and events, everything else is a page):
<?php
if ( is_single() ) {
query_posts('category_name=news&order=DSC&offset=1&orderby=ID&posts_per_page=2');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<article>
<h3><a href='<?php the_permalink() ?>'><?php the_title(); ?></a></h3>
<span class="date"><?php the_time(get_option('date_format')); ?></span>
<?php the_excerpt($strip_teaser); ?>
<a class="more" href='<?php the_permalink() ?>'>Read more</a>
</article>
<?php endwhile; endif; wp_reset_query();?>
<?php
}
else {
}
?>
Conditional comments should work, you were just one letter off (in vs. is)
You want in_category(), not is_category()
http://codex.wordpress.org/Template_Tags/in_category

Wordpress If post title already on page remove from list

This is a bit of a complex one I have been working on. I have four main categories. Formal, Fasion, Social and Wedding. I have Wordpress set up so it shows the twelve most recent posts of all these in one section.
<li>
<?php $recent = new WP_Query("category_name=social,fashion,wedding,formal&showposts=1&offset=1"); while($recent->have_posts()) : $recent->the_post();?>
<div class="show">
<a class="thumb" href="<?php the_permalink() ?>">
<?php the_post_thumbnail(); ?>
</a>
<a href="<?php the_permalink() ?>">
<section class="phototitle"><p><?php the_title(); ?></p></section>
</a>
</div>
<?php endwhile; ?>
</li>
This works fine and I use the offset function to show them over and over.
I then have the same four categories listed below this in separated boxes. So they have a bit of order.
<section class="postlist">
<h1>Formal</h1>
<hr>
<?php $recent = new WP_Query("category_name=formal&showposts=6"); while($recent->have_posts()) : $recent->the_post();?>
<?php the_title(); ?><br />
<?php endwhile; ?>
</section>
My question is how do I stop repeat posts from the top section so the postlist section will have different link titles.
I thought it might be something to do with the exclude technique and if the title already appears on the page remove it. Or if title is on page and if child of top section remove or something along those lines.
The problem is the top section is random so I can just offset it by 12.
Any input appreciated.
Cheers
You can collect the post id's you already displayed and then make use of the WP_Query exclude feature for the second query. Already displayed posts will be excluded then.

Categories