【问题标题】:Wordpress homepage with latest posts and Advanced Custom fields - How?带有最新帖子和高级自定义字段的 Wordpress 主页 - 如何?
【发布时间】:2013-10-16 01:06:03
【问题描述】:

我希望我的主页显示我的最新帖子,这些帖子是我的投资组合项目。在这些项目缩略图下,我有我的静态内容,我正在使用来自 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(); ?>

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

【问题讨论】:

  • 好的,我已经修复了它,但我认为它可以改进吗?我将我所有的项目添加到一个名为 Portfolio 的类别中,并将其包含在 if(have_posts) 之前:&lt;?php query_posts( array ( 'category_name' =&gt; 'Portfolio', 'posts_per_page' =&gt; -1 ) ); ?&gt;。然后在循环之后我使用了wp_reset_query();。显然,Wordpress 不太推荐这种方式。

标签: wordpress advanced-custom-fields


【解决方案1】:

看起来您需要将“主页”(带有 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

【讨论】:

  • @Allan-Crabtree,这可能会有所帮助...要在 wordpress 安装的首页上使用此代码,您可以按照以下步骤操作:1)创建新页面。 2)转到设置->阅读并选择新页面作为首页。 3)创建一个page template,并将您的代码添加到其中。 4)转到您设置为首页的页面的页面编辑器,然后在页面属性下选择您的新模板。
【解决方案2】:

如果我的理解正确,您有一堆帖子,并且您想在主页上显示带有标题和帖子缩略图的列表,然后在列表下方显示您分配给主页的自定义字段帖子数?

第1步:复制page.php创建一个新的页面模板,改名为homepage.php并将其添加到顶部:

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

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

第 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(); ?>

【讨论】:

    猜你喜欢
    • 2019-06-21
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    • 2021-02-11
    • 1970-01-01
    相关资源
    最近更新 更多