Im trying to display data via a loop using ACF[advance custom fields, Option Page] in my Footer.
This works on my homepage without any problems. But if i navigate to sub-pages it breaks , meaning it would show maybe only the page_links row or on another page only the images_footer row.
It confuses me because home page works perfectly normal and shows all footer data from below.
Also i looked if there is any conflict in the custom-fields names towards the sub-pages custom-fields names and the footer but i dont find any.
Another thing if i would remove the first loop the second one works this is only on some pages, so weird.
If i keep the first while loop ,delete the second while loop, and navigate to other pages,the first while loop show on some pages and some not, how is this even possible.
*I have done some var_dumps(); after both if statements, but obviously the dump does not. So it doesnt even pick up the row -> ('page_links', 'option') or in some cases ('images_footer', 'option').
Any helpful tips would be much appreciated.
<div class="col-lg-8">
<div class="links">
<?php
if( have_rows('page_links', 'option') ):
while( have_rows('page_links', 'option') ): the_row();
?>
<p><?php the_sub_field('footer_link_name'); ?></p>
<?php endwhile; ?>
<?php endif; ?>
</div>
<div class="foot-images">
<?php
if( have_rows('images_footer', 'option') ):
while( have_rows('images_footer', 'option') ): the_row();
?>
<div><img class="img-fluid" src="<?php the_sub_field('logo_image'); ?>" alt=""></div>
<?php endwhile;endif; ?>
</div>
</div>
Following this documentation: ACF - Get values from an options page
Related
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 got lots of articles I show on my category page.
Here is the code.
<?php if(is_category(4)) {
while ( have_posts() ) : the_post(); ?>
<div class="work">
<div class="work-thumb">
<a href="<?php echo get_permalink(); ?>">
<?php the_post_thumbnail(); ?>
</a>
</div>
<div class="work-title">
<?php the_title(); ?>
</div>
</div>
<?php endwhile; // end of the loop.
} ?>
It is just loop on all articles, but it doesn't show all of them, just like 50%
What could be the problem>?
Is the page that's using that template/code the page that you have set to be the Posts Page, in admin settings?
If so, then the posts per page setting could be less than the total number of posts (and you would need pagination, or to increase this number).
If it's a custom query with the code in your question, then you need to add this to the query arguments:
'posts_per_page' => -1
Note: Even if your case is the former, you could alter the query by using the pre_get_posts filter. E.g. put this in your theme's functions.php:
add_action('pre_get_posts', 'my_filter');
function my_filter( $query ){
$query->set('posts_per_page', -1);
return $query;
}
Inside that function, you want to wrap the code inside an if statement, to do it specifically for the, for example, post type or taxonomy in question.
I am writing blog for Wordpress using Bootstrap grid. My first post is sticky, 100% width of column. Next posts should be placed undearneath it in two columns.
Every two posts are wrapped in div with .row class to keep them in line. I've done this and it worked perfectly.
<div class="col-md-8">
<div class="top-news">
// sticky post
</div>
<div class="next-news-grid">
<?php while ( have_posts() ) : the_post(); ?>
<?php if(is_sticky()) continue; ?>
<?php if( $wp_query->current_post%2 != 0 ) echo "\n".'<div class="row">'."\n"; ?>
<div class="col-md-6 next-news">
<?php if ( has_post_thumbnail() ) { ?>
<?php the_post_thumbnail('next- news-thumbnail'); ?>
<?php } ?>
<h4><?php the_title(); ?></h4>
<hr>
<?php the_content( "read more" ); ?>
</div>
<?php if( $wp_query->current_post%2 != 1 || $wp_query->current_post == $wp_query->post_count-1 ) echo '</div> <!--/.row-->'."\n"; ?>
<?php endwhile; ?>
</div>
</div>
But I want to use ajax-load-more plugin to automatically load posts on the same page after button is clicked. Unfortunately it won't wrap my posts in .row class anymore.
I copied my while loop into repeater template in plugin settings but it seems like it's ignoring the if statement parts with div appending. Therefore it looks ugly when one post is longer and is pushing another to the right AND another down making huge gaps between them.
So how can I wrap every two posts in div.row container with this plugin? Or can I use something else to achieve infinite loading with button?
The Problem:
My menu bar contains a set of categories. Posts automatically align. A new Plugin is based on Pages. The Page e.g. 'p1' is always on the main site.
The Idea:
Create a template that assigns a page to a specific category e.g. 'p1' -> 'c1'. I found a piece of code
<?php if (is_category('c1')) : ?>
The problem is that I don't know how to tell the program:
if (is_category('c1')) : show page / vice versa?>
How do I do that?
You will want a naming standard between pages and categories. Once that is done you can do the following (or something like it):
<?php if(is_category('c1)) : ?>
<?php query_posts('category_name=c1&order=asc');
if ( have_posts() ) : while( have_posts() ) : the_post();?>
<div class="pageWrapper">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
<?php endwhile; else: ?>
<p>No content was found</p>
<?php
endif;
wp_reset_query();//If writing your own queries, always a good idea to reset it. ?>
I've been trying to make a single sticky post display on the second (or third etc) position instead of the default which is the first.
I have been playing with the loop for hours but I always run into problems.
Here is what I have done so far:
<!-- Wordpress Main Loop Start -->
<?php query_posts(array("post__not_in" =>get_option("sticky_posts"))); ?>
<?php if ( have_posts() ) : $loop_count = 0; while ( have_posts() ) : the_post(); $loop_count++; ?>
<div <?php post_class(); ?>>
<h2><?php the_title(); ?></h2>
<div class="byline postinfo">by <?php the_author_posts_link(); ?></div>
<div class="postdate postinfo"><?php the_time('l F d, Y'); ?></div>
<div class="commentscounter postinfo"><?php comments_number('0', '1', '%'); ?></div>
<?php
if ($loop_count == 2) :
$sticky = get_option( 'sticky_posts' );
query_posts( 'p=' . $sticky[0] );
wp_reset_query();
else:
the_excerpt('Read More...');
endif;
?>
</div>
<?php endwhile; else: ?>
<p><?php _e('No posts were found. Sorry!'); ?></p>
<?php endif; ?>
<div class="navi">
<div class="right">
<?php previous_posts_link('Previous'); ?> / <?php next_posts_link('Next'); ?>
</div>
</div>
<!-- Wordpress Main Loop End -->
What I have done is this:
I used query_posts() to filter out all sticky posts from the loop.
Used the loop to get the posts and a counter which increments everytime a post is fetched.
On the div which displays the fetched post, I added an if statement which checks if the counter is a specific number and if it is, it rewrites the query_posts and gets the sticky one. If not, it displays the fetched post.
I then reset the query and the loop continues as it should (almost).
The problem is, after the count reaches the value I want, the loop instead of continuing from the last post, it start from the beginning.
If I remove the wp_reset_query() function, the loop just stops when the sticky post is found.
If I try to rewrite the query to include all posts, I get infinite looping because I'm already inside the loop.
I don't want to break the loop in two or three so if anyone can help fix this using one loop, that would be great!