【问题标题】:Display Custom Post template name in wordpress在 wordpress 中显示自定义帖子模板名称
【发布时间】:2013-07-31 07:30:48
【问题描述】:

我需要显示自定义帖子模板名称。 实际上我想为每个自定义帖子模板加载不同的 css。 为此,我需要自定义帖子模板名称,但无论如何我都找不到。 这是我的目录结构。

index.php
single.php
header.php
footer.php
fullpage-post.php // This is custom template
two-column-right-menu-post.php
two-column-left-menu-post.php 

现在在标题中我想要链接这个

if($custom_template->name == 'two-column-right-menu-post'){
?> <link href = 'style.php'> <?php
}else{
?> <link href = 'style1.php'> <?php
}

我怎样才能做到这一点。 google了一下,没找到解决办法。

【问题讨论】:

  • 您想在页面上获取帖子详细信息或想从前端输入帖子
  • 调用你可以使用的帖子 query_post('showposts');而(有_posts()):the_post(); the_title() 结束; wp_reset_query();
  • 显示帖子不是问题,我将模板附加到特定帖子。如果附加了该模板,则在标题中我想要一个条件,加载属于该模板的特定样式表。
  • 以这种方式包含您的样式表并在顶部 当您选择页面模板时,它将显示在管理员中

标签: wordpress wordpress-theming custom-post-type


【解决方案1】:

你应该看看get_page_template

不是你包含 css 错误的一面。你不应该使用&lt;link&gt; 来包含样式,但你应该使用wp_enqueue_style,这允许缓存和缩小之类的东西。
具有wp_enqueue_script

的javascript大致相同

一个很好的教程:http://halfelf.org/2012/jquery-why-u-no-enqueued/

【讨论】:

  • 这似乎没有帮助
【解决方案2】:

在深入研究 wordpress 数据库后,我找到了解决方案。就是这样。

因为我在我的应用程序中添加了 custom-post-template 插件,并且能够创建模板并将其附加到帖子中,所以我找到了这个解决方案。

$post_meta      =   get_post_meta($post->ID);
$post_template  =   $post_meta['custom_post_template'][0];

这将返回后模板的名称。现在条件可以这样使用

if($post_template == 'two-column-right-menu-post.php'){
    ?> <link href = 'style.php'> <?php
}else{
    ?> <link href = 'style1.php'> <?php
}

或者这是更好的解决方案

$post_template  =   get_post_meta( $post->ID, 'custom_post_template', true );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 2021-10-01
    相关资源
    最近更新 更多