在函数.php中添加_过滤_Content不起作用

时间:2014-05-21 作者:Radenko Zec

我想为函数中的\\u内容添加\\u筛选器。我的主题的php。我添加了只显示echo的代码,但它表明我的过滤器没有应用。

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\');
我试着这样称呼它:

add_filter(\'the_content\', \'add_mod_hatom_data\', 99);
或将位置更改为位于功能顶部。php没有成功。

我是否需要启用某处add\\u过滤器,或者它被其他函数覆盖?注意:在我的单个帖子模板中,我有:

<?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> <!-- 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("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> <!-- end #main -->

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

<?php get_footer(); ?>

3 个回复
最合适的回答,由SO网友:Matt Royal 整理而成

单帖子模板的这一部分称为循环:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
在循环中,您需要调用以下内容:

<?php the_content(); ?>
将您的单个帖子模板替换为以下内容,然后查看它现在是否有效:

<?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("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> <!-- end #main -->

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

<?php get_footer(); ?>

SO网友:Matt Royal

这是因为变量“$content”为空。您使用此筛选器的方式是正确的。将您的值放入“$内容变量”而不是“echo”,然后“return$内容”实际上会将其回显到页面。尝试以下操作:

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> 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\');

SO网友:NoriAmos

使用此选项(内容筛选器只有一个参数,必须使用1)

 add_filter(\'the_content\', \'add_mod_hatom_data\', 1);

结束

相关推荐

包含在另一个页面中的页面和SEO的重复内容问题

在最近的一个项目中,我决定创建一个页面模板(PageX),它是一种子页面的“堆栈”。此模板仅包括其他三个单独页面(PageA、PageB、PageC)的内容。我现在正试图找到避免SEO重复内容的最佳方法,因为Wordpress会自动为这3个子页面创建特定的永久链接。有没有办法告诉Wordpress这些页面不能有永久链接?