带有最新帖子和高级自定义字段的Wordpress主页 - 如何?

时间:2013-10-08 21:46:44

标签: wordpress advanced-custom-fields

我希望我的主页能够显示我最近的帖子,这些帖子是我的投资组合项目。在这些项目缩略图下面,我有我的静态内容,我正在使用高级自定义字段中的Repeater插件来抓取。我无法在同一页面上工作...要么让博客工作,要么让ACF工作。从来没有,因为一个人需要是一个指定的静态页面,一个人需要是一个帖子页面。我不明白如何将它组合在一起并使其显示为一页。

我已经在阅读面板中进行了实验。 我尝试过使用front-page.php 我已经阅读了WP层次结构等 我尝试过使用模板...... 我已经尝试了wp_reset_postdata();,我在Stack Overflow上的其他地方读过这篇文章。

我必须在阅读面板中使用哪些设置。我需要使用模板文件吗?

这是我正在使用的代码,我已经在模板和不同文件之间拆分了代码,但只是为了便于在这里一起阅读它们(也许这是正确的方法,无论如何,我不会知道..)

<!-- The posts/portfolio items -->

<?php get_header(); ?>   
<div>
<?php if(have_posts()) : ?>
    <ul>
    <?php while ( have_posts() ) : the_post(); ?>
        <li>
            <!-- Permalink,title and post thumbnail here (omitted) -->
        </li>
    <?php endwhile; ?>
    </ul>
   <?php else: ?>
<h2>No Posts found</h2>
<?php endif; ?>
</div>


<!-- Now for the ACF Stuff -->

<?php if(get_field('care_list')): ?>
    <?php while(has_sub_field('care_list')): ?>

        <div class="grid_2 what-i-care-about gap">
           <div class="important-img-container">
           <?php the_sub_field('care_list_image'); ?>
        </div>
           <h3><?php the_sub_field('care_list_title'); ?></h3>
        </div>
    <?php endwhile; ?>
<?php endif; ?>

<?php get_footer(); ?>

请帮助一个沮丧的学习者!提前谢谢。

2 个答案:

答案 0 :(得分:0)

看起来您需要将“主页”的帖子ID(带有ACF转发器的帖子ID)添加到get_field()函数中,如下所示:

<?php $post_id = **post_id_of_your_homepage_here**; ?>
<?php if(get_field('care_list', $post_id)): ?>
<?php while(has_sub_field('care_list')): ?>

    <div class="grid_2 what-i-care-about gap">
       <div class="important-img-container">
       <?php the_sub_field('care_list_image'); ?>
    </div>
       <h3><?php the_sub_field('care_list_title'); ?></h3>
    </div>
<?php endwhile; ?>

这是因为$post_id参数默认为由wordpress启动的当前帖子,这意味着ACF正在您正在显示的最后一个项目组/项目上查找转发器。如果您将$post_id参数设置为主页的ID,则ACF将在该页面上查找转发器。

来源:http://www.advancedcustomfields.com/resources/functions/get_field/#parameters

答案 1 :(得分:0)

如果我理解正确,您有很多帖子,并且您希望在主页上显示带有标题和帖子缩略图的列表,然后显示您已分配到列表下方主页的自定义字段帖子?

步骤1:通过复制page.php创建新的页面模板,将名称更改为homepage.php并将其添加到顶部:

<?php 
/*  
Template Name: Homepage
*/ ?>

步骤2:Crete一个名为“Homepage”的Wordpress页面,在页面创建工具右侧边栏的属性模块中,选择“Homepage”作为页面模板。

步骤3:在您的阅读设置中,将首页从帖子页面更改为“主页”。现在您的主页就是您的“主页”页面。

步骤4:在新页面模板homepage.php上创建这样的完整代码。它将输出您的帖子列表,然后输出您的页面自定义字段:

<?php get_header(); ?>

<?php $the_query = new WP_Query( $args );

<?php if ( $the_query->have_posts() ) : ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <h2><?php the_title(); ?></h2>
        <?php the_post_thumbnail(); ?>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>
<?php endif; ?>

<?php if(get_field('repeater_field_name')): ?>
    <?php while(has_sub_field('repeater_field_name')): ?>
        <?php the_sub_field('sub_field_1'); ?>
    <?php endwhile; ?>
<?php endif; ?>

<?php get_footer(); ?>