1

On each page, I want the admin to choose which case history post summaries will be displayed depending on the post category.

My code below works but it is very clunky if you have lots of categories.

How can I change it to, say "if case-history-code specified on page = category of post then display post"?

(I am using Advanced Custom Fields, and could use Taxonomy instead of category, but their Taxonomy example just shows how to display posts with location=Melbourne.)

if (get_field('case-history-code') == 'CS1') {

    $myposts = get_posts('numberposts=3&offset=0&category_name=cs1');
    foreach ($myposts as $post) :
        setup_postdata($post);
        ?>
          <a  href="<?php the_permalink(); ?>">&nbsp;</a> <?php the_excerpt(); ?><br />
          <a href="<?php echo get_permalink(); ?>"> Read More...</a><br />
    <?php
    endforeach;
    wp_reset_query();
} else if (get_field('case-history-code') == 'CS2') {

    $myposts = get_posts('numberposts=3&offset=0&category_name=cs2');
    foreach ($myposts as $post) :
        setup_postdata($post);
        ?>
          <a  href="<?php the_permalink(); ?>">&nbsp;</a> <?php the_excerpt(); ?><br />
          <a href="<?php echo get_permalink(); ?>"> Read More...</a><br />
    <?php
    endforeach;
    wp_reset_query();
}
  • From code I understand that you are displaying post based on page's custom meta values! This code isn't working ? – Sumit Mar 12 '16 at 11:49
  • @sumit - the code above works OK (on each page, it displays a summary of the relevant posts based on category) but is very repetitive if you have lots of categories. The code by frog3862 below works perfectly. – JohnP1 Mar 12 '16 at 17:45
  • Okay great! If answer works for you then please accept the answer for the best thank to frog3862 :) – Sumit Mar 12 '16 at 17:55
1

Using a variable will make your code a little less repetitive:

$case_history_code = get_field( 'case-history-code' );
if ( $case_history_code ) {
    // make the code lowercase
    $case_history_code_lower = strtolower( $case_history_code );

    // use the lowercase inside of `get_posts`
    $myposts = get_posts( 'numberposts=3&offset=0&category_name=' . $case_history_code_lower );
    foreach ($myposts as $post) :
        setup_postdata($post);
        ?>
        <a  href="<?php the_permalink(); ?>">&nbsp;</a> <?php the_excerpt(); ?><br />
        <a href="<?php echo get_permalink(); ?>"> Read More...</a><br />
        <?php
    endforeach;
    wp_reset_query();
}
  • so-o-o-o-o many thanks!! I have been going round and round in circles with this and it works perfectly. I can't add to your reputation as I have only just joined; went to your profile page to see if I could follow twitter or something as a thank you but no info. – JohnP1 Mar 12 '16 at 17:42
  • 1
    wp_reset_query() need to be replaced by wp_reset_postdata()! – Sumit Mar 12 '16 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.