0
votes

I'd like my homepage to display my latest posts which are portfolio projects of mine. Beneath these project thumbnails I've got my static content which I'm using the Repeater add-on from Advanced Custom Fields to grab. I cant get it all to work on the same page... either get the blog to work or the ACF stuff to work. Never both because one needs to be a designated static page and one needs to be a posts page. I don't understand how to tie it together and make it appear as one page.

I've experimented with the settings in the reading panel.. I've tried using front-page.php I've read up on the WP hierarchy etc I've tried using templates... I've tried wp_reset_postdata(); which I read about elsewhere on Stack Overflow.

What settings must I use in the reading panel. Do I need to use a template file?

Here's the code I'm working with, I've split the code between templates and different files already, but just for ease of reading its all together here (maybe that's the right way to do it anyway, I wouldn't know..)

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

Please help a frustrated learner! Thanks in advance.

2
Ok I've fixed it but I think it can be improved upon? I added all my projects to a category called Portfolio and included this before the if(have_posts) : <?php query_posts( array ( 'category_name' => 'Portfolio', 'posts_per_page' => -1 ) ); ?>. Then after the loop I've used wp_reset_query();. Apparently this way isn't very well recommended by Wordpress though.Allan Crabtree

2 Answers

0
votes

It looks like you're going to need to add the post id of your 'home page' (the one with the ACF repeater on it) to the get_field() function like so:

<?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; ?>

This is because the $post_id parameter defaults to the current post being brought up by wordpress, which means ACF is looking for the repeater on the last Portfolio item/post you are displaying. If you set the $post_id parameter to the ID of your homepage, ACF will instead look for the repeater on that page.

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

0
votes

If I'm understanding correctly, you have a bunch of posts and you want to display a list of them with title and post thumbnail on your homepage, and then display a custom field you've assigned to the homepage underneath the list of posts?

Step 1: Create a new page template by copying page.php, changing the name to homepage.php and adding this to the top:

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

Step 2: Crete a Wordpress page called "Homepage" and in the attributes module in the right sidebar of the page creation tool, select "Homepage" as your page template.

Step 3: In your reading settings, change the front page from posts page to "Homepage." Now your homepage is your page called "Homepage."

Step 4: Make something like this the full code on your new page template homepage.php. It will output your posts list followed by your page custom field:

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