douqiao8370 2016-08-31 14:55
浏览 37

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

I'm trying automatically replace text on a page with a link if a line of text matches a post title. I have a magazine website with pages for each issue and with table of contents on it as a page content, also I have posts with articles (sometimes they can have 1-2-3 published articles for each issue). I want Wordpress to query through ToC text on an issue page and see if any of the titles (for now lines separeted with just
) match titles of the posts (article) and if so replace the title with a link to an article. Is there a nice way to do it? Will appreciate any input.

  • 写回答

1条回答 默认 最新

  • dql7588 2016-08-31 15:21
    关注

    I think you looking for this. Put below function in 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');
            ?>
    

    for custom fields operation check link Custom Fields

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用