2015-08-15 68 views
0

我想显示一个自定义的边栏,如果页面是3页(所有自定义帖子类型)之一,我似乎无法弄清楚如何使这成为可能。我已经在functions.php中正确的注册了“Custom Sidebar”。如何在多个页面上调用此边栏,然后在其他页面上显示常规边栏?WordPress在特定帖子上的显示边栏

enter code here 



    <?php 
    if(is_singular('5894')) { 
     get_sidebar('Custom Sidebar');  } 
    else{ 
     get_sidebar(); //get sidebar.php 
    } 
?> 

回答

0

按照WP Codex你应该能够使用像

is_singular('book'); 

其中book是你的自定义文章类型的名称。


如果此操作无法按预期方式进行,请尝试在您的模板文件中放置另一个 Conditional Tag。也许你也在寻找 is_page_template
如果你想获得您的模板文件的详细信息,将某种类似的代码在它下面:

global $page; 
var_dump($page); 


编辑:

由于OP的评论,我认为他在寻找pre_get_posts - filter

您的代码可能类似于以下内容:

function exclude_ids_from_search_query($query) { 
    global $page; //perhaps you have to check for the pagename in if-statement 

    if ($query->is_home() && $query->is_main_query()) : //e.g. check for pagename, post_type, etc... 
     $ids = array(10, 12, 66, 70); //Post ID's included in search 
     $query->set('post__in', $ids); 
    endif; 

    return $query; 
} 

add_action('pre_get_posts', 'exclude_ids_from_search_query'); 
+0

感谢您的快速回复!我想我可能没有明确提出这个问题。我只想定位**自定义帖子类型中的某些**帖子。能够在代码中输入帖子ID并以此目标进行定位是非常好的。这可能吗? – user1214812

+0

@ user1214812你现在工作了吗? – pbaldauf

相关问题