Home › Forums › General Issues › How to show older field from same category
Example in post loop,
New post (newest posting)
-> field A
-> field B
-> field X (from older post)
-> field Y (from older post)
Older post
-> field X
-> field Y
Other post
-> …
-> …
I’m not completely sure what you’re trying to do, so I’m assuming some things.
<?php
$count = 0; // keep track of how many posts were shown
// the standard WP loop
if (have_posts()) {
while(have_psits()) {
the_post();
$count++; increment count
if ($count == 1) {
// showing first post
// code here to show fields A & B
} else {
// showing older posts
// code here to show fields x & y
}
}
}
?>
here complete logic,
Post 1 (category Tech)
– custom field A (key_a)
– custom field B (key_b)
– custom field C (key_c)
–print custom field from Post 3–
– custom field X (key_x)
– custom field Y (key_y)
– custom field Z (key_z)
———-
Post 2 (category Life)
– custom field A_1 (key_a_1)
– custom field B_2 (key_b_2)
– custom field C_3 (key_c_3)
–print custom field from Post 4–
– custom field X_1 (key_x_1)
– custom field Y_2 (key_x_2)
– custom field Z_3 (key_x_3)
———-
Post 3 (category Tech)
– custom field X (key_x)
– custom field Y (key_y)
– custom field Z (key_z)
–print custom field from Post 5–
– custom field J (key_j)
– custom field K (key_k)
– custom field L (key_l)
———-
Post 4 (category Life)
– custom field X_1 (key_x_1)
– custom field Y_2 (key_x_2)
– custom field Z_3 (key_x_3)
–print custom field from more post–
– …..
———-
Post 5 (category Tech)
– custom field J (key_j)
– custom field K (key_k)
– custom field L (key_l)
–print custom field from more post–
– …..
More post…
This won’t be possible with just ACF. You’re going to need to do some custom queries in WP.
For each post you’re going to need to do a query to get the next post from the same category and then show the fields from that post. I would suggest that you take a look at http://codex.wordpress.org/Class_Reference/WP_Query
The best I can do here would be pseudo code. This forum is really for helping with questions about ACF and what you need is help with basic WP queries and loops. It would be large project for me to code everything that you need here.
// this would be the main loop in your template
while(have_posts)) {
the_post();
// show the fields for this post
/*
get the category of this post - wp_get_post_terms()
get the date of this post - get_the_date()
do a WP_Query to get the next older post in same category
new WP_Query() with tax and date queries to get one post
Nested post loop to loop over secondary query and show fields
*/
}
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
The most recent ACF Chat Friday featured a live demo of how to register CPTs directly in the plugin, one of our most requested features. Check out the summary below for a replay of the demo, and don’t forget to register for the next session! https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 9, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.