如何查询页面内容并将特定文本与Wordpress中的帖子标题进行比较?

时间:2016-08-31 14:55:05

标签: php wordpress

如果一行文字与帖子标题匹配,我正在尝试使用链接自动替换页面上的文字。 我有一个杂志网站,每个问题的页面和作为页面内容的目录,我也有文章的帖子(有时他们可以为每个问题发表1-2-3篇文章)。我希望Wordpress在问题页面上通过ToC文本进行查询,看看是否有任何标题(现在只是单行的标题)与帖子(文章)的标题匹配,如果是的话,用一个链接替换标题文章。 有一个很好的方法吗? 将欣赏任何意见。

1 个答案:

答案 0 :(得分:0)

我认为你在寻找这个。将功能放在function.php

<?php
        function my_function( $post_id ){                
            if ( ! wp_is_post_revision( $post_id ) ){

                // unhook this function so it doesn't loop infinitely
                remove_action('save_post', 'my_function');

                // update the post, which calls save_post again     
                $content_post = get_post($post_id);
                $arr = get_post_meta($post_id, 'link-array', false);
                foreach ($arr as $key => $value) {
                 $post_title_updated=str_replace( $key, $value, $content_post->post_title);
                $post_content_updated=str_replace($key, $value, $content_post->post_content);
                 }

                      $my_post = array(
                          'ID'           => $post_id,
                          'post_title'   => $post_title_updated,
                          'post_content' => $post_content_updated,
                      );

                    // Update the post into the database
                      wp_update_post( $my_post );

                // re-hook this function
                add_action('save_post', 'my_function');
            }
        }
        add_action('save_post', 'my_function');
        ?>
  

自定义字段操作检查链接Custom Fields