1

I have a magazine front page that updates with every issue, and the front page shows the current issue posts which are stickied. So for example, this month all the posts are in a category titlted 'Issue 01' and next month, I will create 'issue 02' which will replace the 'issue 01' sticky posts on the front page.

I am trying to show the category name of the posts that are pulled out in the query but cant seem to do this outside of the loop (i only want to show it once at the top before the loop starts).

Here is my query to call the sticky posts and an attempt to show the category name which doesnt work for me…

<?php // Get Current Issue Articles
    $currentissueposts = array(
        'posts_per_page'      => 6,
        'post__in'            => get_option( 'sticky_posts' ),
        'ignore_sticky_posts' => 1
        );

        $currentissue = new WP_Query( $currentissueposts ); 

        if ( $currentissue->have_posts() ) : ?>
            <div class="the-header">
                <h3><?php the_category(); ?></h3>
            </div><!-- #the-header -->  
        <?php while( $currentissue->have_posts() ) : $currentissue->the_post(); ?>
                <a href="<?php the_permalink() ?>" rel="bookmark">
                <ol class="current-index-container">
                    <li class="the-title"><?php the_title(); ?></li>
                    <li class="the-author"><?php the_field('sub_head_1'); ?></li>
                    <li class="the-subtitle"><?php the_field('sub_head_2'); ?></li>
                </ol></a>
        <?php endwhile; 
        wp_reset_query();
        endif; ?>

I've looked over docu in terms of single_post_title and get_the_category but can't seem to get it to work only once. Any help is much appreciated!

2

Here is an idea. Note, this will only work if all posts are in the desired category and if all posts have only category

The posts are returned in an array which you can access through

$currentissue->posts 

With this in mind, you can get the first post ID as follows

$currentissue->posts[0]->ID

Now, you can add that to a variable

$id = $currentissue->posts[0]->ID    

You can now use get_the_category to retrieve the category this post belongs to. Remember, this will be the category that all posts belongs to according to your question

Something like this would work just ouside your loop, just inside your if statement

$category = get_the_category( $id ); 
echo $category[0]->cat_name;
  • Thanks Pieter, that seems to work. So it is just calling the category of the first post and echoing that. – antonanton Dec 11 '14 at 12:51
  • Yes, exactly. The reasoning here as said, all posts have the same category. It should show Issue 3 on the next issue if the current issue is number 2 :-) – Pieter Goosen Dec 11 '14 at 12:54
1
    <?php print get_the_category(get_the_ID())[0]->name; ?>

get_the_category() - gets an array of posts with information on categories`. get_the_category(get_the_ID()) - gets an array containing information on category for specific post only. get_the_category(get_the_ID())[0] - gets first result from the array, which is an object

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.