Support

Account

Home Forums Front-end Issues Displaying a category ID

Solving

Displaying a category ID

  • I’ve got a custom post type called Promo Boxes which I use in conjunction with the relationship field from ACF to allow my client to construct left and right sidebars for individual pages by choosing which Promo Box to display on them.

    One requirement is for a Promo Box that contains the latest five news stories from a specific category. The custom post type is showing the categories but I can’t get the ID from the chosen category to display on the page.

    To be specific I want the category ID to go into the place marked %ENTER% below.

    <?php query_posts( array ('posts_per_page' => 5, 'order'=> 'DESC', 'orderby' => 'date', 'category__in' => '%ENTER%' ) ); ?>    
      <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
        <li class="title"><?php the_title(); ?></li>
        <?php endwhile; ?>
      <?php endif; wp_reset_query(); ?>  
  • BTW, I realise this may well be a question for the creator of the custom post type plugin, feel free to send me that way if you think this is not in your remit.

    I’m asking it here because I wonder if there’s something I can do with the custom field to get the category ID out.

    • Elliot

    • July 11, 2013 at 4:50 pm

    Hi @debatewise

    Can I ask you to clarify what post types have what custom fields?

    Does the Promo Box post type have a ‘taxonomy’ custom field? If so, what is the name of this field and what is the return type?

    Next, where does the code come from that you have shown? Is that the code that is run for a Promo Box?

    Most likely, all you need to do is use the get_field on the taxonomy custom field to find the term ID selected

  • Hi Elliot,

    the post type Promo Boxes (promo-boxes) has:

    • Content (Wysiwyg Editor)
    • Image (image)
    • Link (text)
    • Alternative title (text)
    • Latest news or reports (Radio button)

    The promo box CPT uses the standard WP categories

    The code above is what I’m running to return the latest five news stories – though that particular segment just the latest five from all categories, not a specific one.

    Thanks for your help with this.

    • Elliot

    • July 11, 2013 at 5:14 pm

    If you want to get the category of your Promo Box, just use this code:

    
    $terms = get_the_terms( $post->ID, 'category' );
    $term = array_pop( $terms );
    
    
    query_posts( array ('posts_per_page' => 5, 'order'=> 'DESC', 'orderby' => 'date', 'category__in' => $term->term_id )
    
  • That threw up an error:
    Warning: array_pop() expects parameter 1 to be array, boolean given in D:\Dropbox\NooMedia Upload\Burma Campaign\burmacampaign.dev\wp-content\themes\roots\templates\sidebar-right-section.php on line 33

    On line 33 is: $term = array_pop( $terms );

    The full code I’m using is

    <?php 
      $terms = get_the_terms( $post->ID, 'category' ); 
      $term = array_pop( $terms ); 
      query_posts( array 
        (
        'posts_per_page' => 5, 
        'order'=> 'DESC', 
        'orderby' => 'date', 
        'category__in' => $term->term_id 
        ) 
      ); 
    ?> 

    Below the error was a list of the latest five news stories from all categories.

    • Elliot

    • July 12, 2013 at 5:39 pm

    Seems like the $terms array is empty… You can test it with this code:

    
    <?php 
    
    $terms = get_the_terms( $post->ID, 'category' );  
    
    echo '<pre>';
    	print_r( $terms );
    echo '</pre>';
    die;
    
    ?>
    
    Can you confirm that 'category' is the correct taxonomy and $post->ID is the correct post ID?
    
  • I’m not exactly sure where this is supposed to go, so forgive the ignorance but if placed above or between any of the following nothing is returned:

    <?php if(get_field('latest_news_or_reports', $post_object->ID) == "news"): ?>
      <?php query_posts( array ('posts_per_page' => 5, 'order'=> 'DESC', 'orderby' => 'date', 'category__in' => '' ) ); ?> 
      <?php if(have_posts()) : while(have_posts()) : the_post(); ?> 

    If placed below I get:

    Array
      (
      	[36] => stdClass Object
      (
        [term_id] => 36
        [name] => Events
        [slug] => events
        [term_group] => 0
        [term_taxonomy_id] => 36
        [taxonomy] => category
        [description] => 
        [parent] => 0
        [count] => 3
        [object_id] => 2470
      )
    
    )

    Which seems to indicate its grabbing the details from the most recent post (which is something in the Events category with an ID of 2470).

    • Elliot

    • July 14, 2013 at 4:27 pm

    Hi @debatewise

    You mentioned in your previous comment that the ‘full’ code you are using is:

    
    <?php 
      $terms = get_the_terms( $post->ID, 'category' ); 
      $term = array_pop( $terms ); 
      query_posts( array 
        (
        'posts_per_page' => 5, 
        'order'=> 'DESC', 
        'orderby' => 'date', 
        'category__in' => $term->term_id 
        ) 
      ); 
    ?> 
    

    The code I suggested was to test the variables you were using.

    I don’t know what this code is:

    
    <?php if(get_field('latest_news_or_reports', $post_object->ID) == "news"): ?>
      <?php query_posts( array ('posts_per_page' => 5, 'order'=> 'DESC', 'orderby' => 'date', 'category__in' => '' ) ); ?> 
      <?php if(have_posts()) : while(have_posts()) : the_post(); ?> 
    

    is that what you are using?

    Please explain clearly

  • Apologies Eliot, I’m still a real newbie with PHP, and the WordPress loop for that matter. I’m away right now though, so will get back to you with answers next week.

  • I think the problem might be I’m not sure where your code should be added in order for me to test the variables. I’ve probably complicated things by (ironically) trying to display a simplified code example. The full and complete code is below.

      <?php $post_objects = get_field('right_1'); if( $post_objects ): ?>
      <div class="promoWrapper">
        <div class="promoBox clearfix">
        	<?php $post_objects = get_field('right_1'); if( $post_objects ): ?>
    				<?php foreach( $post_objects as $post_object): ?>
              <h2>
                <?php if(get_field('pb-link', $post_object->ID) != ""): ?> <!-- Gets the link for the promo box -->
                  <a href="<?php the_field('pb-link', $post_object->ID); ?>">
                <?php endif; ?><!-- pb-link ENDS -->
                <?php if(get_field('alternative_title', $post_object->ID) != ""): ?><!-- Gets the alternative title if there is one -->
                  <?php the_field('alternative_title', $post_object->ID); ?>
                <?php else : ?>
                  <?php echo get_the_title($post_object->ID); ?>
                <?php endif; ?><!-- alternative_title ENDS -->
                <?php if(get_field('pb-link', $post_object->ID) != ""): ?></a><?php endif; ?>
              </h2>
              <?php if(get_field('pb-image', $post_object->ID) != ""): ?>
              <a href="<?php the_field('pb-link', $post_object->ID); ?>">
              <img src="<?php the_field('pb-image', $post_object->ID); $size = "promo-box" ?>" class="promoImage" alt="<?php echo get_the_title($post_object->ID); ?>" title="<?php echo get_the_title($post_object->ID); ?>" />
              </a>
              <?php endif; ?><!-- pb-image ENDS -->
              <div><?php the_field('pb-content', $post_object->ID); ?></div>
              <?php if(get_field('pb-link', $post_object->ID) != ""): ?><span><a class="moreLink" href="<?php the_field('pb-link', $post_object->ID); ?>">More...</a></span><?php endif; ?>
              
          
              <!-- If the promo box is showing latest news or reports -->    
              <?php if(get_field('latest_news_or_reports', $post_object->ID) == "news"): ?>
                <?php query_posts( array ('posts_per_page' => 5, 'order'=> 'DESC', 'orderby' => 'date', 'category__in' => '' ) ); ?> 
                <?php if(have_posts()) : while(have_posts()) : the_post(); ?>   
                  <li class="title"><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></li>     		
                  <?php endwhile; ?>
                <?php endif; wp_reset_query(); ?>          
              <?php endif; ?><!-- News/Reports promoBox ENDS -->          
              
              
            <?php endforeach; ?>
          <?php endif; ?>
        </div><!-- .promoBox ENDS -->
      </div><!-- .promoWrapper ENDS -->
      <?php endif; ?><!-- right_1 ENDS -->
    • Elliot

    • July 22, 2013 at 12:31 pm

    Hi @debatewise

    2 mian issues.

    1. You are setting the variable $post_objects twice. Why?

    2. I can’t see in your code where you are loading the category for.

    The issue is this part right?

    
    <!-- If the promo box is showing latest news or reports -->    
              <?php if(get_field('latest_news_or_reports', $post_object->ID) == "news"): ?>
                <?php query_posts( array ('posts_per_page' => 5, 'order'=> 'DESC', 'orderby' => 'date', 'category__in' => '' ) ); ?> 
                <?php if(have_posts()) : while(have_posts()) : the_post(); ?>   
                  <li class="title"><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></li>     		
                  <?php endwhile; ?>
                <?php endif; wp_reset_query(); ?>          
              <?php endif; ?><!-- News/Reports promoBox ENDS -->
    

    If not, please correct me.

    You have set

    
    'category__in' => ''
    

    Which I’m guessing you want to populate with a custom field value?

    What is the name of the custom field that contains the category name?

  • Hi Elliot,

    1. You are setting the variable $post_objects twice. Why?
    Most probably because I don’t know what I’m doing :). I’ve removed the extraneous one on my code now.

    2. I can’t see in your code where you are loading the category for.
    I think this is my problem, I’m not sure how to load the category. What I’ve done is:

    1. Created a custom field type (Promo Boxes) – which includes the categories built-in taxonomy
    2. Created custom fields and tied them to the Promo Boxes CPT
    3. Created a post in Promo Boxes and assigned it to a category
    4. Tried to grab the category ID from this particular post

    Does that make sense?

    There is no custom field containing the category name, should there be?

    • Elliot

    • July 24, 2013 at 2:48 pm

    Hi @debatewise

    I think we need to go back to the start of this project and learn to crawl before you try to run.

    Can you please re-explain the issue without refering to any code or work you had previously done. Lets start fresh so I can understand the issue.

    * You have a CPT called ‘Promo Boxes’
    * ‘Promo Boxes’ suports the ‘category’ taxonomy
    * Promo Boxes has some custom fields. What are they? Are they relevant to this issue?
    * You are tyring to load the category ID. Where from. What URL are you looking at? What template file is running?

    Thanks
    E

  • Thanks E, I really appreciate your help with this.

    • You have a CPT called ‘Promo Boxes’ – Yes
    • ‘Promo Boxes’ suports the ‘category’ taxonomy – Yes
    • Promo Boxes has some custom fields. What are they? Are they relevant to this issue? – I’m not sure if they’re relevant, but just in case: Content (Wysiwyg Editor), Image (image), Link (text), Alternative title (text), Latest News or Reports? (Radio button)
    • You are tyring to load the category ID. Where from. – I’m not exactly sure how to answer this question, but the user may choose a category for a Promo Box, I’d like this category to be loaded, if Latest News or Reports? == Yes
    • What URL are you looking at? – http://burma.23.co.uk/campaigns/crisis-in-arkan-state/ – specifically the first box in the right column (News). Happy to give you admin access if that helps
    • What template file is running – I’m using the Roots theme and it’s base.php template, alongside four other custom templates: section.php (in the root folder, necessary for Roots, primarily used to import the three subsequent templates); left-section.php (the left sidebar); content-section.php (the centre column), right-section.php (the right sidebar).

    Does that give you what you need?

    • Elliot

    • July 25, 2013 at 1:06 pm

    Hi @debatewise

    Thanks for the info, but again, I am still confused as to what you are trying to do.

    You say:

    You are tyring to load the category ID. Where from. – I’m not exactly sure how to answer this question, but the user may choose a category for a Promo Box, I’d like this category to be loaded, if Latest News or Reports? == Yes

    What do you mean by this????

  • Perhaps screenshots will help.

    Page 1 is a screenshot of the Promo Box CPT with all fields shown and the categories area highlighted.

    Page 2 shows how the Promo Box is assigned to a page using a ACF Relationship (a probably important element I forgot to mention before now!). Towards the bottom of this page is the field Right 1. “Crisis in Arkan State – Latest News” is the relevant Promo Box I’m bringing in to the top right position on http://burma.23.co.uk/campaigns/crisis-in-arkan-state/.

    • Elliot

    • July 26, 2013 at 3:10 pm

    Hi @elliot

    Yes, I can see that you have selected a post object (which is titles the same as the category it is in.)

    I am now more confused as you have multiple relationship fields…

    Can you please start this question again with clarity. And please state what is NOT working, and what IS working.

    This is a guess, but on each page, you have ‘right’ and ‘left’ modeuls. In your right 1 module, you want to load in the category which is assigned to the post object of the same name?

    Are you using a promo box as a dummy holder for the taxonomy? Or are you loading any extra data from the promo box? Why are your promoboxes called the same as your category?

  • The Burma Campaign wanted to construct their own pages so that a page could have two or three promo boxes on the left, some centre column content, and two or three promo boxes on the right.

    I’ve attempted to solve this by creating a Promo Box CPT and then linking this to a page template with the ACF Relationship Field. In most places this works fine. Different promo boxes can be added to the same page. And the same promo box (Donate for example) can be added to different pages.

    The sticking point is category specific news. News is stored as posts and each has a category (or categories) assigned to it. The challenge is to find a way to allow the top five posts of a certain category (say those categorised: “Crisis in Arkan State”), to be displayed on a specific page (in this case, the Crisis in Arkan Stage page).

    I’ll happily go into more detail about how I’ve attempted to solve this problem but thought I’d stop here for now in case there’s a better way of achieving the result I’m looking for.

    • Elliot

    • July 29, 2013 at 1:43 pm

    Hi @elliot

    Thanks for the clarification.

    I think this extends outside the scope of this support forum.

    This forum is for help with any issue s/ bugs with the ACF software.

    It seems like ACF is running great for you, you just need general WP help in loading in a connection between post objects and categories.

    ACF is giving you the correct data, you just need to use it correctly.

    I’m sorry, but I am unable to help you further with this. Please consider consulting a freelance WP developer or posting on stack exchange

  • Thanks Elliot, I thought it might have been beyond the range of what you could offer, thanks for your time trying to help.

Viewing 21 posts - 1 through 21 (of 21 total)

You must be logged in to reply to this topic.

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.