WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom Fields are not displaying in index.php (5 posts)

  1. Mayeenul Islam
    Member
    Posted 1 year ago #

    I'm using a custom theme designed by me using a tutorial.

    The problem is, it's not displaying custom fields, I entered with Posts. To get the custom fields, I used:

    <!-- CODE FOR FEATURED IMAGE FROM CUSTOM FIELD NAME "fimage" -->
    <img src="<?php echo get_post_meta($post->ID, 'fimage', $single=true) ?>" border="0" taget="blank">

    and

    <!-- CODE FOR REPORTER FROM CUSTOM FIELD NAME "reporter" -->
    <div><?php echo get_post_meta($post->ID, 'reporter', $single=true); ?></div>

    The first one displaying (visible only using FireBug):

    <img border="0" taget="blank" src="">

    The second one is displaying (visible only using FireBug):

    <div></div>

    With the same code Custom Fields are visible in Twenty Eleven. So, NO PLUGIN IS RESPONSIBLE for this. The problem is with the Theme I created.
    But the problem is: there is no code to add_theme_function() for Custom Fields. I think the theme is not supporting Custom Fields.

    Q: HOW TO ADD CUSTOM FIELD OPTION INTO CUSTOM THEME?

    Thanks in advance for 'Code-Poets'!

  2. esmi
    Forum Moderator
    Posted 1 year ago #

    Is this code inside the Loop?

  3. Mayeenul Islam
    Member
    Posted 1 year ago #

    Yap.

    Functions.php:

    <!-- ALL THE NEWS WILL BE SHOWN IN THIS POST FORMAT -->
    <?php
    function news_format(){
    ?>	
    
    	  <?php /* Create a div with a unique ID thanks to the_ID() and semantic classes with post_class() */ ?>
                    		<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                            <!-- CODE FOR FEATURED IMAGE FROM CUSTOM FIELD NAME "fimage" -->
                            <img src="<?php echo get_post_meta($post->ID, 'fimage', $single=true) ?>" border="0" taget="blank">
                            <?php if (has_post_thumbnail()) {  the_post_thumbnail();  } // FEATURED IMAGE  ?>
                            <?php /* an h2 title */ ?>
                        	<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'your-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
                            <!-- CODE FOR REPORTER FROM CUSTOM FIELD NAME "fimage" -->
                            <div><?php echo get_post_meta($post->ID, 'reporter', $single=true); ?></div>
                            <?php //the_meta(); ?>
                            <?php //var_dump(get_post_meta()); // CODE RESULT CHECKER IN <pre></pre> tag ?> 
    
    						<?php /* Microformatted, translatable post meta */ ?>
                            <div class="entry-meta">
                                <span class="meta-prep meta-prep-author"><?php _e('By ', 'your-theme'); ?></span>
                                <span class="author vcard"><a class="url fn n" href="<?php echo get_author_link( false, $authordata->ID, $authordata->user_nicename ); ?>" title="<?php printf( __( 'View all posts by %s', 'your-theme' ), $authordata->display_name ); ?>"><?php the_author(); ?></a></span>
                                <span class="meta-sep"> | </span>
                                <span class="meta-prep meta-prep-entry-date"><?php _e('Published ', 'your-theme'); ?></span>
                                <span class="entry-date"><abbr class="published" title="<?php the_time('Y-m-d\TH:i:sO') ?>"><?php the_time( get_option( 'date_format' ) ); ?></abbr></span>
                            <?php edit_post_link( __( 'Edit', 'your-theme' ), "<span class=\"meta-sep\">|</span>\n\t\t\t\t\t\t<span class=\"edit-link\">", "</span>\n\t\t\t\t\t" ) ?>
    						</div><!-- .entry-meta -->
    
    					<?php /* The entry content */ ?>
                        <div class="entry-content">
                        	<!-- CODE FOR FEATURED IMAGE FROM CUSTOM FIELD NAME "fimage" -->
                            <img src="<?php echo get_post_meta($post->ID, 'fimage', $single=true) ?>" border="0" taget="blank">
    						<?php the_content( __( 'বিস্তারিত <span class="meta-nav">&raquo;</span>', 'your-theme' )  ); ?>
                            <?php wp_link_pages('before=<div class="page-link">' . __( 'Pages:', 'your-theme' ) . '&after=</div>') ?>
                        </div><!-- .entry-content -->
    
    					<?php /* Microformatted category and tag links along with a comments link */ ?>
                        <div class="entry-utility">
                            <span class="cat-links"><span class="entry-utility-prep entry-utility-prep-cat-links"><?php _e( 'Posted in ', 'your-theme' ); ?></span><?php echo get_the_category_list(', '); ?></span>
                            <span class="meta-sep"> | </span>
                            <?php the_tags( '<span class="tag-links"><span class="entry-utility-prep entry-utility-prep-tag-links">' . __('Tagged ', 'your-theme' ) . '</span>', ", ", "</span>\n\t\t\t\t\t\t<span class=\"meta-sep\">|</span>\n" ) ?>
                            <span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'your-theme' ), __( '1 Comment', 'your-theme' ), __( '% Comments', 'your-theme' ) ) ?></span>
                            <?php edit_post_link( __( 'Edit', 'your-theme' ), "<span class=\"meta-sep\">|</span>\n\t\t\t\t\t\t<span class=\"edit-link\">", "</span>\n\t\t\t\t\t\n" ) ?>
                        </div><!-- #entry-utility -->
                    </div><!-- #post-<?php the_ID(); ?> -->
    <?php
    	}
    ?>

    Index.php:

    <?php /* Start Two Column Support */ ?>
                    <?php $counter = 1; ?>
                    <?php query_posts( 'cat=3&showposts=5' ); //search news for cat=3 ?>
                    <?php the_post(); ?>
                    <?php /* Two Column output open the div class */ ?>
                        <div class="column1<?php echo $class; echo "one1"; ?>">
                        	<?php news_format(); ?>
                        </div> <!-- End Two Column Support close div class -->
    					<div id="frontAd" style="width:100%; background:#ededed;">
                        PLACE FOR AD
                        </div>
    				<?php $counter = 2; $divplacer = 1; ?>
    				<?php while ( have_posts() ) : the_post() ?>
                        <?php /* Two Column Support get class */ ?>
    					<?php $class = ( $divplacer % 2  ? ' one' : '' ) ?>
                        <?php /* Two Column output open the div class */ ?>
    					<div class="column<?php echo $class;// echo $first; ?>">
    						<?php news_format(); ?>
    					</div> <!-- End Two Column Support close div class -->
    				<?php $divplacer++; //Update Divplacer  ?> 
    
                    <?php endwhile; ?>
  4. esmi
    Forum Moderator
    Posted 1 year ago #

    Try adding global $post; to the top of that function.

  5. Mayeenul Islam
    Member
    Posted 1 year ago #

    Thanks esmi. I got the concept. Your solution works. :)
    Thanks very much for the assistance.
    It's resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic