add_filter para the_content en functions.php no funciona

21 may 2014, 10:29:38
Vistas: 17.9K
Votos: 2

Quiero agregar un add_filter para the_content en el functions.php de mi tema. He añadido código que simplemente muestra un echo pero parece que mi filtro no se aplica.

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> fue modificado por última vez: <span class="updated"> '.

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

 add_filter('the_content', 'add_mod_hatom_data');

He intentado llamarlo así:

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

O cambiar su posición al inicio del functions.php sin éxito.

¿Necesito habilitar add_filter en algún lugar o está siendo anulado por alguna otra función? NOTA: En mi plantilla de post individual tengo:

<?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> <!-- fin del encabezado del artículo -->

                        <footer>
                            <!-- <?php the_tags('<p class="tags"><span class="tags-title">' . __("Etiquetas","wpbootstrap") . ':</span> ', ' ', '</p>'); ?> -->
                        </footer> <!-- fin del pie de página del artículo -->

                    </article> <!-- fin del artículo -->

                    <?php endwhile; ?>          

                    <?php else : ?>

                    <article id="post-not-found">
                        <header>
                            <h1><?php _e("No encontrado", "wpbootstrap"); ?></h1>
                        </header>
                        <section class="post_content">
                            <p><?php _e("Lo sentimos, pero el recurso solicitado no se encontró en este sitio.", "wpbootstrap"); ?></p>
                        </section>
                        <footer>
                        </footer>
                    </article>

                    <?php endif; ?>

                </div> <!-- fin de #main -->

            </div> <!-- fin de #content -->

<?php get_footer(); ?>
4
Comentarios

Por favor, mejora el código del bucle dentro de la plantilla. ¿Utilizas las funciones por defecto de WordPress the_content() para obtener el contenido en el front end? Si no es así, entonces no es posible activar este hook. Quizás debas mejorar tu solución para mostrar el contenido mediante apply_filter. Consulta también la documentación del hook.

bueltge bueltge
21 may 2014 11:17:15

ese 'echo' posiblemente termina en algún lugar antes de que se muestre el HTML del sitio; intenta añadir la cadena al $content como return 'rrr'.$content;

Michael Michael
21 may 2014 11:28:54

El problema es que mi filtro no se está aplicando. He comentado el código en la función y puesto un echo para demostrar que la función no se llama en el single post.

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

@bueltge Sospecho que vas por buen camino pero no estoy seguro de entender. Creo que el problema está relacionado porque en la plantilla de entrada individual uso the_post() y no hay the_content()?

Radenko Zec Radenko Zec
21 may 2014 12:55:40
Todas las respuestas a la pregunta 3
3

Esto ocurre porque la variable '$content' está vacía. La forma en que estás usando este filtro es correcta. En lugar de usar 'echo', coloca tus valores en la variable '$content' y luego el 'return $content' efectivamente lo imprimirá en la página. Prueba esto:

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> fue modificado por última vez: <span class="updated"> '.

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

 add_filter('the_content', 'add_mod_hatom_data');
21 may 2014 11:33:37
Comentarios

Esto tampoco funciona. He comentado el código en esta función y he puesto un echo para probar que la función no se llama. También he probado tu código 'perrrrr...' no se muestra aunque lo ponga en la variable $content. Sospecho que esto está relacionado porque en la plantilla de la entrada única tengo the_post() y no the_content() o algo así?

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

¿Puedes agregar el código de tu plantilla de entrada única para que podamos verlo? (Todo el código)

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

Claro que sí. He actualizado la pregunta con la plantilla simplificada.

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

Esta parte de la plantilla de entrada individual se llama el bucle:

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

Dentro del bucle necesitas llamar al contenido:

<?php the_content(); ?>

Reemplaza tu plantilla de entrada individual con esto y verifica si funciona ahora:

<?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>

            </header> <!-- fin del encabezado del artículo -->

            <footer>
                <!-- <?php the_tags('<p class="tags"><span class="tags-title">' . __("Etiquetas","wpbootstrap") . ':</span> ', ' ', '</p>'); ?> -->
            </footer> <!-- fin del pie de artículo -->

        </article> <!-- fin del artículo -->

        <?php endwhile; ?>          

        <?php else : ?>

        <article id="post-not-found">
            <header>
                <h1><?php _e("No Encontrado", "wpbootstrap"); ?></h1>
            </header>
            <section class="post_content">
                <p><?php _e("Lo sentimos, pero el recurso solicitado no se encontró en este sitio.", "wpbootstrap"); ?></p>
            </section>
            <footer>
            </footer>
        </article>

        <?php endif; ?>

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

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

<?php get_footer(); ?>
21 may 2014 13:21:07
Comentarios

Gracias, funcionó. Tengo el título y el contenido iguales, así que solo necesito reemplazar the_title con the_content.

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

Usa esto (el filtro the_content solo tiene un parámetro y debes usar 1)

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