Skip to content

Instantly share code, notes, and snippets.

@radarin
Last active February 5, 2020 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radarin/44074efd10d8dc12b29d1da5ef37919f to your computer and use it in GitHub Desktop.
Save radarin/44074efd10d8dc12b29d1da5ef37919f to your computer and use it in GitHub Desktop.
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