add_filter the_content in functions.php non funziona

21 mag 2014, 10:29:38
Visualizzazioni: 17.9K
Voti: 2

Voglio aggiungere un add_filter per the_content nel functions.php del mio tema. Ho inserito del codice che dovrebbe semplicemente visualizzare un echo ma sembra che il mio filtro non venga applicato.

function add_mod_hatom_data($content) {
   // $t = get_the_modified_time('F jS, Y');
   //$author = get_the_author();
   // $title = get_the_title();
   //if(is_single()) {
    echo 'perrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr';
   //   $content .= '<div class="hatom-extra"><span class="entry-title">'.$title.'</span> was last modified: <span class="updated"> '.

//$t.'</span> by <span class="author vcard"><span class="fn">'.$author.'</span></span></div>';
   // }
    return $content;
    }

 add_filter('the_content', 'add_mod_hatom_data');

Ho provato a chiamarlo così:

add_filter('the_content', 'add_mod_hatom_data', 99);

O a cambiare la posizione in cima al functions.php senza successo.

Devo abilitare da qualche parte add_filter o viene sovrascritto da qualche altra funzione? NOTA: Nel mio template dei post singoli ho:

<?php get_header(); ?>      
            <div id="content" class="clearfix row">
                <div id="main" class="col-sm-8 clearfix" role="main">
                    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                    <article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article" itemscope itemtype="http://schema.org/BlogPosting">

                        <header>

                            <div class="page-header clip_content single-post">
                                            <div class="page-header">
                                                <h1 class="single-title-wg" itemprop="headline">
                                                                                                        <?php $category = get_the_category();
                                                                                                        if ($category[0]) {
                                                                                                          echo '<b>'.$category[0]->cat_name.' '.$post->ID.'</b>';
                                                                                                        }
                                                                                                        ?>
                                                </h1>
                                                                                            <h4 class="single-title" itemprop="headline">
                                                                                            <p class="lead">                                                                                                         
                                                                                                                <?php the_title(); ?>
                                                        </p>
                                                                                                           </h4>

                                            </div>

                <div class="item_footer">
                                                        </div>            
                                <?php $category = get_the_category();
                                if ($category[0]) {?>
                                <p class="authorParagraph">
                                    <?php echo '<a href="' . get_category_link($category[0]->term_id) . '" class="clearfix">' . $category[0]->cat_name . '</a></p>';
                                    }
                                    ?>
                            </div>
                            </div>

                        </header> <!-- fine intestazione articolo -->

                        <footer>
                            <!-- <?php the_tags('<p class="tags"><span class="tags-title">' . __("Tags","wpbootstrap") . ':</span> ', ' ', '</p>'); ?> -->
                        </footer> <!-- fine piè di pagina articolo -->

                    </article> <!-- fine articolo -->

                    <?php endwhile; ?>          

                    <?php else : ?>

                    <article id="post-not-found">
                        <header>
                            <h1><?php _e("Not Found", "wpbootstrap"); ?></h1>
                        </header>
                        <section class="post_content">
                            <p><?php _e("Sorry, but the requested resource was not found on this site.", "wpbootstrap"); ?></p>
                        </section>
                        <footer>
                        </footer>
                    </article>

                    <?php endif; ?>

                </div> <!-- fine #main -->

            </div> <!-- fine #content -->

<?php get_footer(); ?>
4
Commenti

Per favore migliora il sorgente del loop all'interno del template. Utilizzi le funzioni predefinite di wp the_content() per ottenere il contenuto sul front end? Se non è così, allora non è possibile attivare questo hook. Forse devi migliorare la tua soluzione per l'output del contenuto tramite apply_filter. Vedi anche la documentazione dell'hook.

bueltge bueltge
21 mag 2014 11:17:15

quell'echo potrebbe finire da qualche parte prima che l'html del sito venga generato; prova ad aggiungere la stringa al $content come return 'rrr'.$content;

Michael Michael
21 mag 2014 11:28:54

Il problema è che il mio filtro non viene applicato. Ho appena commentato il codice nella funzione e messo echo per dimostrare che la funzione non viene chiamata nei singoli post.

Radenko Zec Radenko Zec
21 mag 2014 12:54:05

@bueltge Sospetto che tu stia andando nella giusta direzione ma non sono sicuro di aver capito. Penso che il problema sia correlato al fatto che nel template del singolo post uso the_post() e non c'è the_content() ?

Radenko Zec Radenko Zec
21 mag 2014 12:55:40
Tutte le risposte alla domanda 3
3

Questo accade perché la variabile '$content' è vuota. Il modo in cui stai utilizzando questo filtro è corretto. Invece di usare 'echo', inserisci i tuoi valori nella variabile '$content' e poi il 'return $content' effettivamente li mostrerà sulla pagina. Prova così:

function add_mod_hatom_data($content) {
   // $t = get_the_modified_time('F jS, Y');
   //$author = get_the_author();
   // $title = get_the_title();
   //if(is_single()) {
    $content = 'perrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr';
   //   $content .= '<div class="hatom-extra"><span class="entry-title">'.$title.'</span> è stato modificato l'ultima volta: <span class="updated"> '.

//$t.'</span> da <span class="author vcard"><span class="fn">'.$author.'</span></span></div>';
   // }
    return $content;
    }

 add_filter('the_content', 'add_mod_hatom_data');
21 mag 2014 11:33:37
Commenti

Anche questo non funziona. Ho commentato il codice in questa funzione e ho messo echo per dimostrare che la funzione non verrà chiamata. Ho testato anche il tuo codice 'perrrrr...' non viene visualizzato anche se l'ho inserito nella variabile $content. Sospetto che sia correlato al fatto che nel template del singolo post ho the_post() e non the_content() o qualcosa del genere?

Radenko Zec Radenko Zec
21 mag 2014 12:57:28

Puoi aggiungere nel tuo template del singolo post il codice così possiamo vederlo. (Tutto il codice)

Matt Royal Matt Royal
21 mag 2014 13:04:45

Certo. Ho aggiornato la domanda con il template semplificato.

Radenko Zec Radenko Zec
21 mag 2014 13:10:37
1

Questa parte del template del singolo post è chiamata loop:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

All'interno del loop è necessario richiamare il contenuto:

<?php the_content(); ?>

Sostituisci il tuo template del singolo post con questo e verifica se ora funziona:

<?php get_header(); ?>      
<div id="content" class="clearfix row">
    <div id="main" class="col-sm-8 clearfix" role="main">
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

        <article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article" itemscope itemtype="http://schema.org/BlogPosting">

            <header>

                <div class="page-header clip_content single-post">
                    <div class="page-header">
                        <h1 class="single-title-wg" itemprop="headline">
                            <?php $category = get_the_category();
                            if ($category[0]) {
                              echo '<b>'.$category[0]->cat_name.' '.$post->ID.'</b>';
                            }
                            ?>
                        </h1>
                        <h4 class="single-title" itemprop="headline">
                        <p class="lead"> <?php the_title(); ?></p></h4>
                        <p><?php the_content(); ?></p>

                    </div>

                    <div class="item_footer">
                    </div>            
                    <?php $category = get_the_category();
                    if ($category[0]) {?>
                    <p class="authorParagraph">
                        <?php echo '<a href="' . get_category_link($category[0]->term_id) . '" class="clearfix">' . $category[0]->cat_name . '</a></p>';
                        }
                        ?>
                </div>
                </div>

            </header> <!-- end article header -->

            <footer>
                <!-- <?php the_tags('<p class="tags"><span class="tags-title">' . __("Tags","wpbootstrap") . ':</span> ', ' ', '</p>'); ?> -->
            </footer> <!-- end article footer -->

        </article> <!-- end article -->

        <?php endwhile; ?>          

        <?php else : ?>

        <article id="post-not-found">
            <header>
                <h1><?php _e("Non trovato", "wpbootstrap"); ?></h1>
            </header>
            <section class="post_content">
                <p><?php _e("Spiacenti, ma la risorsa richiesta non è stata trovata su questo sito.", "wpbootstrap"); ?></p>
            </section>
            <footer>
            </footer>
        </article>

        <?php endif; ?>

    </div> <!-- end #main -->

</div> <!-- end #content -->

<?php get_footer(); ?>
21 mag 2014 13:21:07
Commenti

Grazie, ha funzionato. Ho titolo e contenuto uguali quindi devo solo sostituire the_title con the_content.

Radenko Zec Radenko Zec
21 mag 2014 14:11:22
0
-1

Usa questo (il filtro the_content ha solo un parametro e devi usare 1)

add_filter('the_content', 'add_mod_hatom_data', 1);
21 lug 2020 00:35:31