Wordpress单页模板与动态投资组合部分


Wordpress one-page template with dynamic portfolio section

我整个上午都在寻找答案,但一无所获。我正在建立一个一页的WordPress模板。我有一个主页,它使用了一个名为one-page.php的页面模板,这是在所有其他页面。下面是该模板中的php:

<?php 
            $args = array(
                'post_type' => 'page',
                'order' => 'ASC'
            );
            $the_query = new WP_Query( $args );         
        ?>
        <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> 
        <?php get_template_part( 'content', 'page' ); ?>
        <?php endwhile; endif; ?>

这段代码工作得很好。所有的部分可以使用content-page.php模板部分,不包括投资组合部分,我想使用另一个模板部分,带来所有的投资组合自定义的帖子类型。

我尝试在one-page.php和content-page.php中添加条件if语句,如下所示:

    <?php if ( is_page( 'portfolio' ) ) : ?>
    //My portfolio custom post type loop is here
    <? endif; ?>

但这也不起作用-我认为这是因为is_page()函数将检查正在显示的当前页面,这是主页。而不是找出查询当前正在处理的页面-但我不确定。

谁能帮助我理解我将如何有条件地加载投资组合部分到一个单独的模板部分?

您可以实现这个检查页段,您可以在循环中获得它。如果它是"portfolio"(或任何您保存的),则加载content-portfolio.php,否则加载content-page.php。像这样:

     if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
     if ("portfolio" === $post->post_name) { 
         get_template_part('content', 'portfolio');
     } else {
         get_template_part('content', 'page');
     }
 endwhile; endif;

设置文件条件

<?php
if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
    if (is_page('portfolio')) {
        get_template_part('content', 'portfolio');
    } else {
        get_template_part('content', 'page');
    }
endwhile; endif;
?>

创建一个名为content-portfolio.php的模板文件,该文件是content-page.php的副本,并将以下代码放入其中。如果它显示'call',说明你的模板正在工作。

portfolio.php

<?php
echo "portfolio.php";
die('Call');
?>