9:48 PM
0
A: How can I display recent posts from a particular category in my header?

Andy Macaulay-BrookYou do want to "do a loop". Within your header you want to make a fresh query for just those three posts and loop through them without upsetting the main query that you need further down each page. Here's a standard loop calling three posts from your category: $wpse_235359_query = new WP_Query...

 
Ok. Thanks. Does this go in the header php along with whatever html or span formatting I need? Or should this be put into a child theme header php file? And I can duplicate this particular code multiple times for each category I want to do this with? Do I need to rename any of these things (like "wpse_235359_query"? Or is that a necessary part of the code?
 
I've expanded my answer to answer those points.
 
It doesn't have to have a different variable name for each instance called? Can it be placed in a footer, but styled up to the top of the page or should I really put it into the header file? I use a universally available theme using CSS to modify and adjust everything on the page. It has a built-in CSS option that I can infuse my custom CSS into. That's why I was asking if a child theme would be better. If I change themes at some point just to play around with something, or the theme posts an update, I don't want to lose everything I've changed.
 
@OscarGuy If parent theme provides update then you can't edit parent theme. Better option is either make changes in child theme or make changes in parent theme to remove update feature.
 
@Rishabh disabling updates is not a good idea
@OscarGuy you can re-use the variable name in this case. Place it where you want, using a child theme so you can still update your parent theme.
 
9:48 PM
Is it possible to create the child theme so that only part of the header is removed or do I have to take the contents of the original header.php and put it into the child theme file and modify it to fit my needs?
 
That's a whole different question and topic really. The child theme works by overriding whole template files in the parent. So you copy the parent header.php into the child theme, modify it, and the child header.php is used by WP instead of the parent header.php. A call to load header.php will always look in the child theme first.
 
I read up on child themes, so I just figured all that out. Thanks. I do have a different question as I've been working to modify the coding as you provided. When you put in 'your-category-slug' will that pull both the main category as well as subcategory posts or just the main category posts?
 
Make sure the code is within php tags. That should pull in child categories too, yes.
 
Sorry, this is what I have: ' <?php $cinemasight_header_query = new WP_Query( array( 'category_name' => 'academy-awards', 'posts_per_page' => 3 ) ); if ($cinemasight_header_query->have_posts()) : while($cinemasight_header_query->have_posts()) : $cinemasight_header_query->the_post(); <span class="Main category Header"><?php the_title(); ?></span> endwhile; endif; wp_reset_postdata(); '
 
Just before the span you haven't closed the php tag I think.
 
9:48 PM
The "?>" is the close tag? I have to close each and every php tag and re-open it for each new function? So it's like html in that it's not open-ended. Do I not have to put it before the "endif;" or the "endwhile;" or what have you?
 
You don't have to do each line. Only when you switch from php to HTML or back again.
 
oh. That makes sense and it would also then be that I didn't re-open on the other side of the html. With that fixed, I guess my next question is that we've called to pull three posts from that category (still unsure of the sub-category thing), but how do we pull that data for each post separately?
Once I got the php closures in, everything popped and pulled all three titles. How do I isolate one title at a time? Is there a way to set it up so I can style the first title differently than the second and third titles?
 
You'd just use CSS for that. Use the :first-child or :nth selectors to differentiate them.
 
That does not appear to be working. Here's my test site: anniversary.cinemasight.com A the top, you'll see the three links created with the above code using the following html inserted: <span class="MainCategoryHeader"><a href="cinemasight.com/"><?php the_title(); ?></a></span> I am using the following styling. It seems that the first-child part isn't working. I also tried nth-child and it didn't work. It seems to be ignoring the child part and just styling all three links the way the first is specified.
Here's the css code: .MainCategoryHeader a:first-child { background-color: #FFFFFF; color: #FFFF00; } .MainCategoryHeader a { background-color: #FFFFFF; color: #000000; }
 
Every link is a first-child in your HTML. Try .MainCategoryHeader:first-child a instead, targeting the link inside the .MainCategoryHeader which is the first child of the body. For this to work anywhere in the page, wrap these with a div too so that the first element in the set is always the first-child