Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Worpress Menu Eintrag bei leerer Kategorie ausblenden
<?php
// Diese Funktion wird nur im Frontend angewendet
function shortcode_posts_function(){
// Parameter
$args = array(
'category' => '43', // Kategorie ID
'numberposts' => 1 // Anzahl der zu ladenden Beiträge
);
//Posts holen
$posts = get_posts($args);
//Inhalte sammeln
$content = "";
foreach ($posts as $post) {
$content .= $post->post_title;
}
// auswerten
if ($content == "" ){
// CSS schreiben, wenn Content leer
add_action('wp_head', 'invisible_menu');
function invisible_menu() {
echo "<style>
.menu-item-748 {
visibility: hidden;
}
</style>";
}
}
if ($content != "" ){
// CSS schreiben, wenn Content vorhanden
add_action('wp_head', 'show_menu');
function show_menu() {
echo "<style>
.menu-item-748 {
font-weight: bolder;
}
</style>";
}
}
}
// Funktion beim Laden der Seite ausführen
add_action( 'init', 'shortcode_posts_function' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.