Skip to content

Instantly share code, notes, and snippets.

@salvatorecapolupo
Created March 2, 2024 20:44
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 salvatorecapolupo/cf98dd480fa2833d145fe2527d1d0977 to your computer and use it in GitHub Desktop.
Save salvatorecapolupo/cf98dd480fa2833d145fe2527d1d0977 to your computer and use it in GitHub Desktop.
Wordpress A-Z Listing (plugin 2024)
<?PHP
/**
*
* Plugin Name: AZ Listing 2
* Description: The cat is under the ground
* Author: Salvatore Capolupo
* Version: 1.0
* Author URI: http://www.trovalost.it
*/
//pagina recensioni customizzata 2 marzo 2024
function custom_category_index_1286737() {
$categoria_attiva = 7;
// 7 è l'id della categoria su cui voglio attivare l'effetto
// Verifica se la query è per una categoria e se è la query principale
if ( is_category( $categoria_attiva ) && is_main_query() ) {
// Ottieni tutti i post nella categoria corrente
if ( false === ( $posts = get_transient( 'posts_1278678367868768173' ) ) ) {
$posts = get_posts( array(
'cat' => $categoria_attiva, // get_query_var('cat'),
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1 // op bop a loo bop a lop bom bom
) );
set_transient( 'posts_1278678367868768173', $posts, 1 * HOUR_IN_SECONDS );
}
// Stampa l'indice alfabético
$index = array();
foreach ( $posts as $post ) {
$title = $post->post_title;
$first_letter = strtoupper( substr( $title, 0, 1 ) );
$index[ $first_letter ][] = $post;
}
// Stampa l'indice
echo '<h2> Dalla A alla Z </h2><hr/>';
foreach ( $index as $l => $posts ) {
echo '<a style="font-size:20px;" href="#recensioni' . $l . '"> '.$l .' </a>, ';
}
echo '<ul><hr/>';
foreach ( $index as $letter => $posts ) {
echo '<h2><a name="recensioni'. $letter .'">' . $letter . '</a></h2>';
echo '<ul>';
foreach ( $posts as $post ) {
$author_id = $post->post_author;
$author = get_the_author_meta( 'display_name', $author_id );
echo '<li> <a href="' . get_permalink( $post->ID ) . '">' . $post->post_title . '</a> (di '.$author.') </li>';
}
echo '</ul>';
}
echo '</ul>';
}
echo "<h2></h2><hr/>";
}
add_action( 'loop_start', 'custom_category_index_1286737' );
@salvatorecapolupo
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment