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();
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();
Share
Improve this question
edited Nov 5, 2019 at 14:27
butlerblog
5,1213 gold badges28 silver badges44 bronze badges
asked Sep 14, 2014 at 11:28
Even TEven T
113 bronze badges
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
Reset to default 1Edit ://
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
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,
));
}