【问题标题】:create a function and modify a post on the wordpress site based on the post id创建一个函数并根据帖子 id 在 wordpress 网站上修改帖子
【发布时间】:2022-01-18 11:22:22
【问题描述】:

我想用这种方式编辑我的一篇帖子的内容,但它不起作用。

这个过滤器会影响 php 输出还是原始 php 文件?

add_filter( 'the_content', 'multiple_string_replacements');
function multiple_string_replacements ( $contentt ) {
    if ( is_single('15467') && in_the_loop() && is_main_query() ) {
   
    $condition = true;
  
    if($condition){
        $urlsdothejob = "link.com";
        
        $text = array(
        "$variable1" => "$urlsdothejob",
        "$variable2" => "$urlsdothejob",
    );

    $contentt = str_ireplace(array_keys( $text ), $text, $contentt);

    };
    }
    return $contentt;
}

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    像这样尝试,希望它对你有用。

    while(have_posts()):
    
        $ID = get_the_ID();
    
        $data1 = get_post_meta($ID,'meta_key',true);
        $data2 = get_post_meta($ID,'meta_key',true);
    
        if($data1 == $data2) {
            update_post_meta($ID,'meta_key','meta_value');
        }
    
    endwhile;
    

    【讨论】:

    • 谢谢,那不是我的问题,请检查一下。我使用这个过滤器 add_filter('the_content', 'multiple_string_replacements');函数multiple_string_replacements ( $contentt ) { if ( is_single('15467') && in_the_loop() && is_main_query() ) { $condition = true; if($condition){ $urlsdothejob = "link.com"; $text = array("$variable1" => "$urlsdothejob", "$variable2" => "$urlsdothejob", ); $contentt = str_ireplace(array_keys( $text ), $text, $contentt); }; } 返回 $ 内容; } 不工作
    • 我认为它不像你使用它的方式那样工作
    【解决方案2】:

    如果您知道要查找的条件,您应该能够编写IF 语句来匹配。

    if ($constantvalue == "anothervalue") {
      // make local changes 
    }
    

    你能举例说明你正在尝试做什么吗?这么少的信息很难理解这个请求。

    【讨论】:

    • 我可以做 if 语句,但是我无法根据 single.php 文件中的 id 访问单个帖子,我想在这个主题文件中更新它,就像( $the_query->have_posts() ) { echo '
        '; while ( $the_query->have_posts() ) { $the_query->the_post()->$postid=2585): 然后做一个文件修改
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多