开发者

How can i have custom fields on the posts page in wordpress?

First I've created a home.php page to replace index.php and can add some custom fields on this new one and to have in it lastest 3 posts.

On home.php page I put:

<?php echo get_post_meta($post->ID, 'test', true); ?>"/>

but it doesn't works cause it tries get the post id and not the id of the page. If i put 18 (id of th开发者_运维百科e page) directly it works, but i want it dinamicaly:

 <?php echo get_post_meta(18, 'test', true); ?>"/>

And this condition is not satisfied for test:

if($post->post_type == 'page'){
    echo 'This item is a page and the ID is: '.$post->ID;
}


Heyo,

I think your problem might be that you should have that within a loop. For example if you were displaying several post from a category and displaying each custom field it might look like this:

 <?php query_posts('cat=25&showposts=3'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <li class="homeblock" style="max-width:400px;">
        <span class="artist-name">  
    <?php $key='artist'; echo get_post_meta($post->ID, $key, true); ?> 
</span>
    <div class="entry-thumbnail">
    <?php the_post_thumbnail(); ?> 
    </div>

    </li>
    <?php endwhile; endif; ?>

Otherwise (not sure how your theme is set up), what you might have to do is edit content.php and add:

<?php $key='artist'; echo get_post_meta($post->ID, $key, true); ?> 


Try:

if ($post->post_type == 'page') {
    <?php echo get_post_meta($page_id, 'test', true); ?>
}

Hope this helps :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜