Rispondete anche a domande troppo elementari?
Devo aggiungerealla variabileCodice PHP:'draft', 'publish'
Si fa così?Codice PHP:$status = 'pending';
Evidentemente no perchè si è rotto tutto.Codice PHP:$status = 'pending', 'draft', 'publish'
![]()
Rispondete anche a domande troppo elementari?
Devo aggiungerealla variabileCodice PHP:'draft', 'publish'
Si fa così?Codice PHP:$status = 'pending';
Evidentemente no perchè si è rotto tutto.Codice PHP:$status = 'pending', 'draft', 'publish'
![]()
Che intendi per "aggiungere"?
Alla fine la stringa deve essere "pending, draft, publish"?
In tal caso
Se ho capito male, spiega meglio cosa devi ottenere.Codice PHP:$status = 'pending' . ', draft' . ', publish';
Non mi aspettavo una risposta, grazie davvero. Ho una funzione che guarda lo stato di pubblicazione di un post (wordpress) e mi avverte se è nello stato "pending". io vorrei estenderla alle altre possibilità (draft, pubished, etc.).
Posto l'intero codice, sono poche righe, nel caso dovessero essere aggiunte altre stringe di verifica.
Codice PHP:function keha_pending_posts_indicator( $menu ) {
$post_types = get_post_types();
if ( empty( $post_types ) ) {
return;
}
foreach ( $post_types as $type ) {
$status = 'pending';
$num_posts = wp_count_posts( $type, 'readable' );
$pending_count = 0;
if ( ! empty( $num_posts->$status ) ) {
$pending_count = $num_posts->$status;
}
// Build string to match in $menu array
if ( $type == 'post' ) {
$menu_str = 'edit.php';
} else {
$menu_str = 'edit.php?post_type='.$type;
}
// Loop through $menu items, find match, add indicator
foreach ( $menu as $menu_key => $menu_data ) {
if ( $menu_str != $menu_data[ 2 ] ) {
continue;
} else {
// NOTE: Using the same CSS classes as the plugin updates count, it will match your admin color theme just fine.
$menu[ $menu_key ][0] .= " <span class='update-plugins count-$pending_count'><span class='plugin-count'>" . number_format_i18n( $pending_count ) . '</span></span>';
}
}
}
return $menu;
}
add_filter( 'add_menu_classes', 'keha_pending_posts_indicator' );
Non so il formato dei dati ritornati dalla funzione wp_count_post(), ma intanto prova questo
Codice PHP:$pending = 'pending';
$draft = 'draft';
$published = 'published';
if ( ! empty( $num_posts->$pending ) ) {
$pending_count = $num_posts->$pending;
}
if ( ! empty( $num_posts->$draft ) ) {
$draft_count = $num_posts->$draft;
}
if ( ! empty( $num_posts->$published) ) {
$published_count = $num_posts->$published;
}
Ho fatto la modifica a dovere, ma non mostra l'alert che è avvenuto un draft/pending/publish (ho provato a mettere il post in tali status).![]()
Leggi la documentazione di WP, c'è scritto come si fa
https://codex.wordpress.org/Function...wp_count_posts
Codice PHP:$count_posts = wp_count_posts();
$published_posts = $count_posts->publish;
$draft_posts = $count_posts->draft;
dunque, per logica riagganciandomi anche a questo Codex, la nuova parte di codice dovrebbe essere così:
Ma gli alerts non compaiono. A questo punto è più grande di me.Codice PHP:$pending = 'pending';
$draft = 'draft';
$publish = 'publish';
$count_posts = wp_count_posts( $type, 'readable' );
$pending_count = 0;
if ( ! empty( $count_posts->$pending ) ) {
$draft_posts = $count_posts->pending;
}
if ( ! empty( $count_posts->$draft ) ) {
$draft_posts = $count_posts->draft;
}
if ( ! empty( $count_posts->$published) ) {
$published_posts = $count_posts->publish;
}
Non hai letto ciò che ti ho scritto, oppure non hai notato le differenze col codice precedente.
Intanto la funzione wp_count_posts() non ha parametri, poi l'accesso alle proprietà del risultato si fa per esempio con
$count_posts->draft;
senza il $ dopo ->
Non conosco sto plugin di wp, cmq dal codice che hai postato si nota che questo fa due cose:
1. Ritrova il numero di post con status pending.
2. Cicla sul menù per trovare il link di edit dei post ed aggiunge il pallino con il numero trovato in 1.
Ora, tu fa solo il punto 1. Trovi il numero di post nello stato di pending, draft, published.
A questo punto devi trovare il modo di mostrali...e qui sorge il problema: cha fai metti tre pallini accanto alla voce del menù?
Che, sinceramente, non so se è funziona (inoltre dovresti cambiare il colore dei pallini se no non si capisce a chi è riferito)Codice PHP:foreach ( $menu as $menu_key => $menu_data ) {
if ( $menu_str != $menu_data[ 2 ] ) {
continue;
} else {
// NOTE: Using the same CSS classes as the plugin updates count, it will match your admin color theme just fine.
$menu[ $menu_key ][0] .= " <span class='update-plugins count-$pending_count'><span class='plugin-count'>" . number_format_i18n( $pending_count ) . '</span></span>';
// NOTE: Using the same CSS classes as the plugin updates count, it will match your admin color theme just fine.
$menu[ $menu_key ][0] .= " <span class='update-plugins count-$draft_count'><span class='plugin-count'>" . number_format_i18n( $draft_count ) . '</span></span>';
// NOTE: Using the same CSS classes as the plugin updates count, it will match your admin color theme just fine.
$menu[ $menu_key ][0] .= " <span class='update-plugins count-$published_count'><span class='plugin-count'>" . number_format_i18n( $published_count ) . '</span></span>';
}
}
oppure devi trovare un altro modo per visualizzare i 3 valori
Ultima modifica di boots; 04-11-2016 a 10:14