Support

Account

Home Forums General Issues How to show older field from same category

Solving

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

  • can you give sample code?

  • 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
      */
      
    }
    
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘How to show older field from same category’ is closed to new replies.