admin管理员组文章数量:1122832
I want to get content from one custom field of different pages along side their page titles and I'm using the following code:
<?php
$mypages = get_pages( array(
'sort_order' => 'ASC',
'sort_column' => 'post_title',
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'meta_key' => '',
'meta_value' => '',
'authors' => '',
'child_of' => 53,
'parent' => -1,
'exclude_tree' => '',
'number' => '',
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
) );
foreach( $mypages as $page ) {
$content = $page->post_content;
$new = $page->get_post_custom_values('new_file');
if ( ! $content ) // Check for empty page
continue;
$content = apply_filters( 'the_content', $content );
?>
<h2><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h2>
<a style="display:block; text-align: center;" title="Download Now" href="<?php $new; ?>"><img src="<?php bloginfo('template_url'); ?>/images/new-file.png" alt="Download the File" /></a>
<?php
}
?>
I'm able to get page title but not getting my custom fields value. Please guide me in this regard. Your help'll be highly appreciated in this regard.
I want to get content from one custom field of different pages along side their page titles and I'm using the following code:
<?php
$mypages = get_pages( array(
'sort_order' => 'ASC',
'sort_column' => 'post_title',
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'meta_key' => '',
'meta_value' => '',
'authors' => '',
'child_of' => 53,
'parent' => -1,
'exclude_tree' => '',
'number' => '',
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
) );
foreach( $mypages as $page ) {
$content = $page->post_content;
$new = $page->get_post_custom_values('new_file');
if ( ! $content ) // Check for empty page
continue;
$content = apply_filters( 'the_content', $content );
?>
<h2><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h2>
<a style="display:block; text-align: center;" title="Download Now" href="<?php $new; ?>"><img src="<?php bloginfo('template_url'); ?>/images/new-file.png" alt="Download the File" /></a>
<?php
}
?>
I'm able to get page title but not getting my custom fields value. Please guide me in this regard. Your help'll be highly appreciated in this regard.
Share Improve this question asked Sep 18, 2013 at 8:00 user1666698user1666698 354 silver badges11 bronze badges 2- all the cusatom post vlaues will stored in the post meta so you need to use th get_post_meta() you can study more about this in wordpress codex – Vikas Gautam Commented Sep 18, 2013 at 8:41
- get_post_custom_values can also take a post id parameter to retrieve values from any post/page. <?php get_post_custom_values($key, $post_id); ?> – Matt.C Commented Sep 18, 2013 at 8:43
1 Answer
Reset to default 0I think your problem is here:
$new = $page->get_post_custom_values('new_file');
You must use:
$new = get_post_custom_values('new_file', $page->ID);
You must note also that get_post_custom_values() return an array, so $new will be an array. I think in your case is better to use get_post_meta() in this way:
$new = get_post_meta($page->ID, 'new_file', true);
本文标签: Getting content from custom fields of different pages on a single page
版权声明:本文标题:Getting content from custom fields of different pages on a single page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736284839a1927335.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论