I was thinking this would be an easy solution to accomplish, but I'm stuck since Main Pages in the Front-End are arrayed automatically.

Basically I created individual pages to display specific Custom Post Type posts with different Template pages. What I want to accomplish is, if no posts were created for this specific CPT, than the page will not show in the Main Navigation links.

Is this possible? Each page is using a unique URL (i.e. site.com/specials). So if no 'Special' post has been created, I do not want that page to show. Is there a function I can create to accomplish this?



1 Answer 1


So I've come up with a solution - although I am not sure if this is the best, as it outputs CSS, something I prefer not to do.

In the header.php of the theme:

<?php $published_posts = wp_count_posts('CPT1')->publish;
    if ( $published_posts == 0 ):
    echo '<style>.page-item-8{display:none;}</style>';
endif ?>

<?php $published_posts = wp_count_posts('CPT2')->publish;
    if ( $published_posts == 0 ):
    echo '<style>.page-item-9{display:none;}</style>';
endif ?>

This is for 2 separate CPTs. As you can see, it checks if the CPT has a post. If not - the CSS will display in the .

However, I am wondering if there may be a way to accomplish this same thing through the functions.php of the theme file?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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