Wordpress functions.php

时间:2017-10-25 01:11:15

标签: wordpress

我的知识非常有限,但我设法在自定义functions.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');

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

  

在[位置]的[日期],[位置]

用于“活动”类别中的帖子。

感谢您的期待。

2 个答案:

答案 0 :(得分:1)

这是一个起点。

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

答案 1 :(得分:0)

谢谢安德鲁。经过多次谷歌搜索和试用&amp;错误,我想出了以下内容。我不认为这是一个非常优雅的解决方案,或者即使语法完全正确但似乎有效:

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