【问题标题】:Custom layouts for custom post types? (wordpress)自定义帖子类型的自定义布局? (wordpress)
【发布时间】:2013-07-31 10:33:30
【问题描述】:

我打算创建一些不同的自定义帖子类型,但我希望它们具有与普通帖子不同的布局。

普通帖子本身有两种不同的外观,一种用于索引页面,另一种用于点击进入永久链接页面。

对于自定义帖子,我想做同样的事情(两种不同的布局,都不同于普通帖子),但由于某种原因,我的代码似乎没有什么不同。

到目前为止,我一直在使用自定义帖子模板插件,并尝试在 post-[postype].php 文件中编写代码,但两者似乎都无效。

对于我的 single.php,代码如下 -

<div id="primary" class="content-area">
    <div id="content" class="site-content" role="main">

    <?php while ( have_posts() ) : the_post(); ?>
<!--- post wrapper home/post --->   
<?php if ( is_home()) { echo '<div class="fullposthome">' ; } 

else {  echo '<div class="fullpost">' ; }
?>

<?php if( get_post_meta($post->ID, 'imgtest', true) ) { ?> <!--- made following div appear if said custom field presetn ---->
<div class="testbox"><img src="<?php the_field('imgtest'); ?>" alt="" width="100%" height="auto"/></div> <!--- div with custom field inside --->
<?php } ?>

<?php if ( is_home()) { echo '<div class="contenttextboxhome">' ; } 

else {  echo '<div class="contenttextbox">' ; }
?>

        <?php get_template_part( 'content', 'single' ); ?>

</div>  

        <?php temptheme1_content_nav( 'nav-below' ); ?>

        <?php
            // If comments are open or we have at least one comment, load up the comment template
            if ( comments_open() || '0' != get_comments_number() )
                comments_template();
        ?>

    <?php endwhile; // end of the loop. ?>

    </div><!-- #content -->

</div><!--- post wrapper --->

</div><!-- #primary -->

对于自定义帖子,我尝试更改行 对于我尝试过的自定义帖子,因为这是我假设名称引用 [content/single.php] - 这是在 underscore.me / _S 框架中,请注意,我也将尝试使用 Thematic 框架,但由于 _S 更简单,所以我更容易按照我想要的方式构建它。

所以我想我的问题是我的编码哪里出了问题,或者如何正确使用自定义帖子模板插件?

【问题讨论】:

    标签: php plugins wordpress custom-post-type


    【解决方案1】:

    如果您的自定义帖子类型是“产品”

    然后

    1. 存档文件应该是:archive-products.php
    2. 分类应该是:taxonomy-product_category_slug.php
    3. 单个文件应该是:single-products.php

    如果你想要不同的内容模板,那么

    创建类似 content-product.php 的文件

    在你的 single.php 中使用get_template_part( 'content', 'product' );

    对于当前情况,如果一切正常,则只需创建内容文件,对其进行自定义。

    【讨论】:

    • 我已经有一个内容文件了?我将尝试这种方法,但我不想从 content.php 中创建一个单独的文件,因为它已经被使用了吗?
    • 如果您只想更改自定义帖子的结构,那么您必须创建另一个内容-{custom-post}.php
    • 我应该如何在 php 中引用它(内容电影)?作为其当前的 { 'content', 'movies'} 并将变为 {'movies','movie'}
    • 到目前为止,我在永久链接页面上获得了自定义外观,但在索引页面上没有自定义外观(对于自定义帖子类型的帖子)
    • 如果您在 single.php 中使用 get_template_part( 'content', 'movies' ); 并且如果您的目录中不存在 content-movies.php,它将查找 content。 php 文件。例如,只需复制您的 content-single.php 并将其保存为 content-movies.php 并将您的 get_template_part( 'content', 'single' ); 替换为 single.php 中的 get_template_part( 'content', 'movies' );
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    相关资源
    最近更新 更多