Wordpress 函数中条件语句的语法.php



我的知识非常有限,但我设法在自定义函数.php文件中添加一个过滤器,以便在发布内容之前显示一些自定义字段短代码:

//Insert custom event fields at the beginning of post content
    function custom_event($content) {
    $beforecontent = '<strong>[acf field="date"] [acf field="time"] [acf field="location" ] </strong>';
    $fullcontent = $beforecontent . $content;
    return $fullcontent;
}
add_filter('the_content', 'custom_event');

如果我想以帖子属于特定类别为条件,任何人都可以帮助我使用正确的语法吗?我想我可以使用in_category('x')函数,我只是不确定语法。我最终想在变量之间添加一些文本,以便它显示

在 [日期] 在 [时间] 在 [地点]

对于"活动"类别中的帖子。

谢谢期待。

这是一个起点。

function custom_event($content) {
    global $post;
    if( in_category( 'category-name-slug-id', $post ) ) {
        $beforecontent = '<strong>[acf field="date"] [acf field="time"] [acf field="location" ] </strong>';
        $fullcontent = $beforecontent . $content;
        return $fullcontent;
    }
    return $content;
}
add_filter('the_content', 'custom_event');

谢谢安德鲁。经过大量的谷歌搜索和反复试验,我想出了以下内容。我不认为这是一个非常优雅的解决方案,或者即使语法完全正确,但它似乎有效:

function custom_event($content) {
    global $post;
    if( get_field( "date" ) != "" || get_field( "time" ) != "" || get_field( "location" ) != "" ){ 
        $open = "&#0091;";
    }
    if( get_field( "date" ) != ""){
        $ondate = 'On '.get_field( "date" );
    }
    if( get_field( "time" ) != ""){
        $attime = 'at '.get_field( "time" );
    }
    if( get_field( "location" ) != ""){
        $atlocation = 'at '.get_field( "location" );
    }
    if( get_field( "date" ) != "" || get_field( "time" ) != "" || get_field( "location" ) != "" ){ 
        $close = "&#0093; ";
    }
    if( in_category( 'events', $post ) ) {
        $beforecontent = "<strong>$open$ondate $attime $atlocation$close</strong><br><br>";
        $fullcontent = $beforecontent . $content;
        return $fullcontent;
    }
    return $content;
}
add_filter('the_content', 'custom_event');

最新更新