1

I have a website with the custom post type sammenligne, along with some custom fields made using Advanced Custom Fields (ACF). I have modified an archive site (archive-sammenligne.php) which displays the data the way I want it, however when I try to extract the data using a WP query on a page, I get no output. If I use the standard WordPress titles, I get output. I assume there is something with the link to the custom fields; but they work on the archive page.

I need to run the query on a WordPress page, not an archive, as I would like to have post content above the loop. The code below is saved as a page template.

I run Genesis Metro Pro and WordPress.

Thanks in advance.

<?php
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
function sk_display_custom_fields() {
/*Custom fields/*
    $navn = get_field( 'navn' );
    $type = get_field( 'type' );
    $fordeler = get_field( 'fordeler' );
/*Wordpress loop*/
    $arg = array(
            'post_type' => 'sammenligne',
            'posts_per_page' => 10,
            'order' => 'DESC',
            'post_status' => 'publish'
            );
    $query = new WP_Query($arg);
    if ( $query->have_posts() ) : 
        while ( $query->have_posts() ) : $query->the_post(); 
            echo '<h2>' . $navn . '</h2>';
        endwhile;
    endif;
    wp_reset_query(); 
}
add_action( 'genesis_entry_header', 'sk_display_custom_fields' );

    genesis();
1
  • What does your CPT registration looks like? Does it work correctly in other contexts?
    – Rarst
    Commented Sep 14, 2014 at 12:25

2 Answers 2

1

Edit ://

try this:

<?php
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
function sk_display_custom_fields() {
/*Wordpress loop*/
    global $wp_query;
    query_posts(array(
    'post_type' => 'sammenligne'
    ));

    while(have_posts()) : the_post(); ?>
$navn = get_field( 'navn' );
$type = get_field( 'type' );
echo '<p>',$navn , '__', '$type','</p>');
    endwhile;
    wp_reset_query();
}
add_action( 'genesis_entry_header', 'sk_display_custom_fields' );

genesis();

UPDATE://

Ok I tested ACF's now. With the following code you can target custom fields of posts from a custom post type without being on an archive site!

<?php

    // create custom loop to query the custom posttype
    $arg = array(
    'post_type' => 'sammenligne',
    'posts_per_page' => 10,
    'order' => 'DESC',
    'post_status' => 'publish'
    );

    $query = new WP_Query($arg);

// then start the query 
if ( $query->have_posts() ) :
    while ( $query->have_posts() ) : $query->the_post();
            // now go get the fields you want ! 
            $field1 = get_field('test1');
            // output the fields in the format you want
            echo $field1;
    endwhile;
endif;
wp_reset_query(); // reset query
5
  • Thanks for your reply, but I still can't get it to work. I get a blank page, however if I change the echo in the while loop to something like 'test', I get this printed 4 times, which is the number of sammenligne-posts I have.
    – Even T
    Commented Sep 14, 2014 at 13:04
  • i didnt' notice that your are using ACF. I look into this again in the evening. Perhaps this link helps advancedcustomfields.com/resources/code-examples
    – AnC
    Commented Sep 14, 2014 at 13:26
  • 1
    i have updated the code. I think you only have to get the fields after u specified the post type in the loop args
    – AnC
    Commented Sep 14, 2014 at 13:41
  • Thanks, but still no luck. With the latest change, I just get a blank screen.
    – Even T
    Commented Sep 14, 2014 at 16:00
  • 1
    Thank you so much! Much appreciated. The code you gave me after the edit worked like a charm. I was able to pull all the custom fields. This is like a "related post" roll under my silopages, where I pull posts with custom fields based on another custom field on the pages. When I use this as a page template, I can get a roll with related posts and easily silo my WP sites. To anyone that googles this later. :)
    – Even T
    Commented Sep 15, 2014 at 13:30
0

yes it does work on my other site archive-sammenligne.php, which is the archive file for the slug sammenligne.

This is the output from Advanced Custom Fields. I've removed the other fields (I have abut 20).

if(function_exists("register_field_group"))
{
    register_field_group(array (
        'id' => 'acf_kredittkort',
        'title' => 'Kredittkort',
        'fields' => array (
            array (
                'key' => 'field_5412c743d82aa',
                'label' => 'Navn',
                'name' => 'navn',
                'type' => 'text',
                'required' => 1,
                'default_value' => '',
                'placeholder' => '',
                'prepend' => '',
                'append' => '',
                'formatting' => 'html',
                'maxlength' => '',
            ),
            array (
                'key' => 'field_5412c7e2d82ad',
                'label' => 'Maksgrense',
                'name' => 'maksgrense',
                'type' => 'number',
                'default_value' => '',
                'placeholder' => '',
                'prepend' => '',
                'append' => '',
                'min' => '',
                'max' => '',
                'step' => '',
            ),
        ),
        'location' => array (
            array (
                array (
                    'param' => 'post_type',
                    'operator' => '==',
                    'value' => 'sammenligne',
                    'order_no' => 0,
                    'group_no' => 0,
                ),
            ),
        ),
        'options' => array (
            'position' => 'acf_after_title',
            'layout' => 'no_box',
            'hide_on_screen' => array (
            ),
        ),
        'menu_order' => 0,
    ));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.