2017-02-15 64 views
2

我正在运行BeTheme,但添加了自定义文章类型。在index.php上,我需要从常规文章和我创建的自定义文章类型“latest-news”中抽取。我不知道如何设置它,以便index.php从我的帖子和我的自定义帖子类型中提取。

在我的主题,index.php文件是从该模板文件中的函数拉动,https://github.com/pingram3541/betheme/blob/master/includes/content-post.php

在部分开始对这个文件的第10行:

if(! function_exists('mfn_content_post')){ 
function mfn_content_post($query = false, $style = false, $images_only = false){ 
    global $wp_query; 

我敢肯定我需要改变什么$args变量$wp_query正在拉动,所以array('post_type' => array('post')也有'latest-news'但我不知道在哪里可以找到这个,或者如果这甚至是正确的方法。

编辑: 我只是想添加以下代码functions.php

function add_custom_post_type_to_query($query) { 
    if ($query->is_home() && $query->is_main_query()) { 
     $query->set('post_type', array('post', 'latest-news')); 
    } 
} 
add_action('pre_get_posts', 'add_custom_post_type_to_query'); 

这没有奏效。

+1

这是正确的做法。你是否希望你的新查询搜索普通帖子或只有自定义帖子类型? –

+0

两者。理想情况下,它会从“发布”和“最新新闻”发布类型以及按日期排序。 – user3183717

回答

1

文件的第21行:

if(! $query) $query = $wp_query; 

尝试编辑以

$args=[ 
'post_type' => ['post', 'latest-news'], 
]; 
if(! $query) $query = new WP_Query($args); 

让我知道,如果这个工程,这取决于它可能会产生意外结果的时间。

+0

这没有奏效,它打破了网站。 受你答案的启发,我试着将这一行改为: '$ args ['post_type'] =''post','latest-news'';如果(!$ query)$ query = new WP_Query($ args);' 哪一个也没有用,尽管它不会破坏网站,只是让它不会显示帖子。 – user3183717

+1

'我错过了分号]后,尝试将其更改为$ args = ['post_type'=> ['post','latest-news']];'告诉我这是破坏网站还是改变任何东西 –

+0

成功了!谢谢!我也应该发现这个错误,但是我非常感谢你的帮助。 – user3183717