0

The below is just a scenario to provide context so you understand what im trying to accomplish, However please keep your answers related to the title if possible and a css solution/function.

I have a podcast site that im working on. And i have "next" and "prev" buttons on the podcast post pages that take the user in order to the next podcast episode by date and these buttons only display for the podcast post category.

(so just regular wordpress blog posts in a category called "podcast")

My issue is the most recent podcast post (for example podcast Ep# 150) would display a "next" episode button. - Being that Ep#150 is the newest podcast post there should be no "Next" button displayed. Right now its taking you to "Ep# 1" when you press it which makes sense, but i would rather not direct users to older episodes on our newest posts.

So i just want to "display:none;" that button on the most recent post in the podcast category.

Thanks for any help!

<span class="prev-ep-wrap">
  <span class="prev-ep">
   <?php next_post_link_plus( array( 'order_by' => 'post_date', 'loop' => true, 'tooltip' => 'Previous Episode', 'in_same_cat' => true, 'ex_cats' => '30, 11', 'link' => 'Previous Episode' ) );?>
  </span>
</span>
<span class="next-ep-wrap">
  <span class="next-ep">
   <?php previous_post_link_plus( array( 'order_by' => 'post_date', 'loop' => true, 'tooltip' => 'Next Episode', 'in_same_cat' => true, 'ex_cats' => '30, 11', 'link' => 'Next Episode' ) );?>
  </span>
</span>
  • 1
    Possible duplicate of Disable "previous link" in first post and "next link" in last post – kero Oct 15 '17 at 16:46
  • How are you displaying them currently? – kero Oct 15 '17 at 16:46
  • Hi guys thanks for the replies! However dont get lost in my scenario which i just provided for context. My title is clear. How do i include css on the most recent post of a specific category? Thanks. – Patrick Oct 15 '17 at 16:49
  • But that is the thing - (at least to my knowledge) there is no simple way to achieve this in pure CSS – kero Oct 15 '17 at 16:54
  • Theres no simple way to insert css into a page specified by its post date and category id? – Patrick Oct 15 '17 at 16:56
1

From the context of your question I understand that you don't use next_post_link and previous_post_link for the implementation of the Next and Prev buttons, so you have to use CSS to hide them appropriately.

next_post_link and previous_post_link simply don't display the links if the current post is the last/first on the loop, so I think it would be better to add them in your template instead of using CSS.

You can check the documentation here:

  • I have updated my question with my code, hope that helps! <3 – Patrick Oct 15 '17 at 16:52
0

I highly suggest using L.Milo's answer, since this will actually solve the problem and adding custom CSS will only masquerade it.


To add custom CSS to the latest post of a category, the following should work. It does the following

  1. Get the id of the latest post of that category using wp_get_recent_posts()
  2. Check if currently a single post is displayed and if so, check if it has the id of the latest post (ie., it is the latest post)
  3. If so, enqueue another stylesheet.
<?php

function wpse_enqueue_on_latest_post() {
    $args = array(
        'numberposts' => 1,
        'category' => 12,// id of the category
    );
    list($latest) = wp_get_recent_posts($args);

    if (is_single( $latest['ID'] )) {
        wp_enqueue_style( ... );
    }
}
add_action('init', 'wpse_enqueue_on_latest_post');

Alternatively, you could add a custom body class and use the same stylesheet, where you differentiate exactly via that class

<?php

function wpse_add_latest_post_body_class($classes) {
    $args = array(
        'numberposts' => 1,
        'category' => 12,// id of the category
    );
    list($latest) = wp_get_recent_posts($args);

    if (is_single( $latest['ID'] )) {
        $classes[] = 'latest-of-category-xxx';
    }
    return $classes;
}
add_filter('body_class', 'wpse_add_latest_post_body_class');

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.