• Since yesterday I’ve been trying to display custom field data in my sidebar.
    But for some reason it’s not working…

    I followed this tutorial:
    http://www.bloggingtips.com/2008/11/02/wordpress-custom-fields/

    At a page I put in the custom field the key:sidebar and value: just some text.

    In the sidebar I’ve put the following code:
    <?php echo get_post_meta($post->ID, 'sidebar', TRUE); ?>

    But it won’t display anything 🙁 What could be the problem?

    The page is a index ( displays all posts ) And I make use of the plugin called Pages Links To.

    Anybody has this problem before?

Viewing 10 replies - 1 through 10 (of 10 total)
  • The page is a index ( displays all posts )

    For which one of those posts do you want to display the Custom Field?

    Might try adding this just before to get the custom field for the first post displayed:

    <?php
    global $posts;
    echo get_post_meta($posts[0]->ID, 'sidebar', TRUE);
    ?>

    Thread Starter Paul_NGC

    (@paul_ngc)

    Thanks for the reply. I added the code to sidebar.php, but nothing happens unfortunately.

    This is the code of my sidebar.php:

    <div id="sidebar">
    	<ul>
    	<?php
    global $posts;
    echo get_post_meta($posts[0]->ID, 'sidebar', TRUE);
    ?>
    	</ul>
    
            <ul>
    		<?php wp_register(); ?>
    		<li><?php wp_loginout(); ?></li>
    		</ul>
    		</li>
    		<?php endif; ?>
    	</ul><!-- end ul -->
        </div><!-- end sidebar -->

    In the sidebar is also a Category Widget from WordPress.

    If you are using widgets in your sidebar you may need to put that in a ‘code widget’ such as one provided by Otto’s PHP Code Widget.

    Thread Starter Paul_NGC

    (@paul_ngc)

    I just tried Otto’s Widget. Pasted the php you gave me earlier in the widget, but still no result 🙁

    This is the code from the index.php. Maybe the problem lays in here.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="nl">
    <head profile="http://gmpg.org/xfn/11">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title> &raquo; Nieuws</title>
    <link rel="stylesheet" href="http://localhost:8888/wordpress/wp-content/themes/simpleblocks/style.css" type="text/css" media="screen" />
    <link rel="alternate" type="application/rss+xml" title=" RSS Feed" href="http://localhost:8888/wordpress/feed/" />
    <link rel="pingback" href="http://localhost:8888/wordpress/xmlrpc.php" />
    <meta name='robots' content='noindex,nofollow' />
    <link rel="alternate" type="application/rss+xml" title=" &raquo; Nieuws Categorie Feed" href="http://localhost:8888/wordpress/category/nieuws/feed/" />
    <link rel='stylesheet' id='contact-form-7-css'  href='http://localhost:8888/wordpress/wp-content/plugins/contact-form-7/stylesheet.css?ver=2.0.7' type='text/css' media='all' />
    <script type='text/javascript' src='http://localhost:8888/wordpress/wp-includes/js/jquery/jquery.js?ver=1.3.2'></script>
    <link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://localhost:8888/wordpress/xmlrpc.php?rsd" />
    <link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://localhost:8888/wordpress/wp-includes/wlwmanifest.xml" />
    <link rel='index' title='' href='http://localhost:8888/wordpress' />
    <meta name="generator" content="WordPress 2.8.5" />
    <script type='text/javascript'>AC_FL_RunContent = 0;</script><script type='text/javascript' src="http://localhost:8888/wordpress/wp-content/plugins/dynamic-headers/AC_RunActiveContent.js"></script>
    </head>
    
    <body>
    
    <div id="page">
    	<div id="header">
        <img src="http://localhost:8888/wordpress/wp-content/header-images/romijnders.jpg" alt="romijnders.jpg" title="romijnders.jpg" />    <div class="title">
    				<h2><a href="http://localhost:8888/wordpress/zinkwerk-van-berkel/" rel="bookmark" title="Permanent Link to Zinkwerk Van Berkel">Zinkwerk Van Berkel</a></h2>
    			</div>
    	</div>
    	<div id="menu">
    			<ul>
    			<li class="page_item page-item-19"><a href="http://localhost:8888/wordpress/organisatie/" title="Organisatie">Organisatie</a></li>
    <li class="page_item page-item-8"><a href="http://localhost:8888/wordpress/oplossingen/" title="Oplossingen">Oplossingen</a></li>
    <li class="page_item page-item-24"><a href="http://localhost:8888/wordpress/service/" title="Service">Service</a></li>
    <li class="page_item page-item-22 current_page_parent"><a href="http://localhost:8888/wordpress/category/nieuws/" title="Nieuws">Nieuws</a></li>
    <li class="page_item page-item-2"><a href="http://localhost:8888/wordpress/contact/" title="Contact">Contact</a></li>
    <li class="page_item page-item-28"><a href="http://localhost:8888/wordpress" title="Home">Home</a></li>
    			</ul>
    	</div><div id="container">
    
    <div id="rightsidebar">
    	<ul>
    	<li id="search-3" class="widget widget_search"><h2 class="widgettitle">Zoek op Romijnders.nl</h2>
    <li id="search">
    	<form id="searchform" action="http://localhost:8888/wordpress" method="get">
    		<input type="text" id="searchinput" name="s" class="searchinput" value="Keywords..." onfocus="if (this.value == 'Keywords...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Keywords...';}" />
    		<input type="submit" id="searchsubmit" value="" />
    	</form>
    </li></li>
    <li id="text-3" class="widget widget_text">			<div class="textwidget"><br />
    <a href="http://localhost:8888/wordpress/contact/" target="_self"><img class="alignnone size-full wp-image-223" title="storing" src="http://localhost:8888/wordpress/wp-content/uploads/2009/12/storing.jpg" alt="storing" width="180" height="55" /></a>
    
    <a href="http://localhost:8888/wordpress/contact/" target="_self"><img class="alignnone size-full wp-image-223" title="storing" src="http://localhost:8888/wordpress/wp-content/uploads/2009/12/groen.jpg" alt="groen" width="180" height="55" /></a>
    
    <a href="http://localhost:8888/wordpress/oplossingen/energiebesparing/" target="_self"><img class="alignnone size-full wp-image-222" title="energie" src="http://localhost:8888/wordpress/wp-content/uploads/2009/12/energie.jpg" alt="energie" width="180" height="55" /></a>
    
    <a href="http://localhost:8888/wordpress/over/login/" target="_self"><img class="alignnone size-full wp-image-221" title="login" src="http://localhost:8888/wordpress/wp-content/uploads/2009/12/login.jpg" alt="login" width="180" height="55" /></a>
    
    <a href="http://localhost:8888/wordpress/organisatie/devils/" target="_self"><img class="alignnone size-full wp-image-224" title="devils" src="http://localhost:8888/wordpress/wp-content/uploads/2009/12/devils.jpg" alt="devils" width="179" height="55" /></a></div>
    		</li>
    	</ul>
    </div><!-- end sidebar2 -->
    
    <div id="sidebar">
    	<ul>
    	<li id="categories-3" class="widget widget_categories"><h2 class="widgettitle">Categorieën</h2>
    		<ul>
    	<li class="cat-item cat-item-3 current-cat"><a href="http://localhost:8888/wordpress/category/nieuws/" title="Alle berichten opgeslagen onder Nieuws bekijken">Nieuws</a> (5)
    </li>
    	<li class="cat-item cat-item-4"><a href="http://localhost:8888/wordpress/category/vacature/" title="Alle berichten opgeslagen onder Vacature bekijken">Vacature</a> (4)
    <ul class='children'>
    	<li class="cat-item cat-item-14"><a href="http://localhost:8888/wordpress/category/vacature/account/" title="Alle berichten opgeslagen onder Account bekijken">Account</a> (1)
    </li>
    	<li class="cat-item cat-item-13"><a href="http://localhost:8888/wordpress/category/vacature/elektro-vacature/" title="Alle berichten opgeslagen onder Elektro bekijken">Elektro</a> (1)
    </li>
    	<li class="cat-item cat-item-12"><a href="http://localhost:8888/wordpress/category/vacature/gelderland-vacature/" title="Alle berichten opgeslagen onder Gelderland bekijken">Gelderland</a> (2)
    </li>
    </ul>
    </li>
    		</ul>
    </li>
    <li id="execphp-3" class="widget widget_execphp"><h2 class="widgettitle">Test Cat</h2>
    
    			<div class="execphpwidget"></div>
    		</li>
        </ul>
    </div><!-- end sidebar3 -->
    
    <div id="main">
    				<div class="post" id="post-212">
    
                <div class="title2">
    				<h1><a href="http://localhost:8888/wordpress/zinkwerk-van-berkel/" rel="bookmark" title="Permanent Link to Zinkwerk Van Berkel">Zinkwerk Van Berkel</a></h1>
    				<div class="postmeta">
    					<span class="postmeta_category"><a href="http://localhost:8888/wordpress/category/nieuws/" title="Ale berichten bekijken in Nieuws" rel="category tag">Nieuws</a></span>
    				</div><!-- end postmeta -->
    			</div><!-- end title2 -->
    
    			<div class="entry">
    				<p>Gemeentemuseum Arnhem<br />
    Toen het Nijmeegse bedrijf Van Berkel nog niet bij de Romijnders Groep hoorde, was directeur Frans van Berkel zelf verantwoordelijk voor het dakwerk. Na zijn afscheid was er niemand binnen het bedrijf die gespecialiseerd was in het dakwerk. Peereboom: “ Dat is toen bij mij op het bordje terechtgekomen.” Alle aanvragen binnen onze regio komen bij Van Berkel terecht. Bij het museumproject in Arnhem zijn Bas Janssen en Theo Megens de twee monteurs die het zink hebben aangebracht. <a href="http://localhost:8888/wordpress/zinkwerk-van-berkel/#more-212" class="more-link">Lees verder >></a></p>
    			</div><!-- end entry -->
    
    			<div class="additionalinfo">
    							</div><!-- end additional info -->
    
    		</div><!-- end post -->
    				<div class="post" id="post-209">
    
                <div class="title2">
    				<h1><a href="http://localhost:8888/wordpress/retail-pakket-romijnders/" rel="bookmark" title="Permanent Link to Retail Pakket Romijnders">Retail Pakket Romijnders</a></h1>
    				<div class="postmeta">
    					<span class="postmeta_category"><a href="http://localhost:8888/wordpress/category/nieuws/" title="Ale berichten bekijken in Nieuws" rel="category tag">Nieuws</a></span>
    				</div><!-- end postmeta -->
    			</div><!-- end title2 -->
    
    			<div class="entry">
    				<p>Electra, airco, sanitair, CV, zwakstroom-installatie:<br />
    De retail-unit van Romijnders biedt winkeliers alle ‘gebouwgebonden installaties’ onder één noemer.</p>
    <p>Vanaf mei 2007 biedt Romijnders Horssen een compleet pakket aan voor de detailhandel. Samen met de andere vestigingen van Romijnders verzorgt Horssen de gehele W&E installatie bij (her) inrichting van winkels. Marcel Willems, vestigingsleider van Horssen en Henk Veens, projectleider voor de DA drogisterijen over de nieuwe aanpak. <a href="http://localhost:8888/wordpress/retail-pakket-romijnders/#more-209" class="more-link">Lees verder >></a></p>
    			</div><!-- end entry -->
    
    			<div class="additionalinfo">
    							</div><!-- end additional info -->
    
    		</div><!-- end post -->
    				<div class="post" id="post-206">
    		</div><!-- end post -->
    
        	<div class="navi">
    			<div class="left"></div>
    			<div class="right"></div>
    		</div><!-- end navi -->
    
    			</div><!-- end main -->
    
    </div><!-- end container -->
    <div id="footer">
    	<div class="footer_left">
    		<h3>Romijnders &middot; 2009 - 2010 </h3>
    	</div>
    	<div class="footer_right">
    		<h3>[email protected] </h3>
    	</div>
    </div><!-- end footer -->
    <script type='text/javascript' src='http://localhost:8888/wordpress/wp-includes/js/jquery/jquery.form.js?ver=2.02m'></script>
    <script type='text/javascript' src='http://localhost:8888/wordpress/wp-content/plugins/contact-form-7/contact-form-7.js?ver=2.0.7'></script>
    </div><!-- end page -->
    </body>
    </html>

    If you change:

    echo get_post_meta($posts[0]->ID, 'sidebar', TRUE);

    to

    echo get_post_meta(123, 'sidebar', TRUE);

    where 123 is a valid post id that has that custom field do you get output?

    Thread Starter Paul_NGC

    (@paul_ngc)

    Thanks for all the quick replies 🙂
    But also this didn’t work.

    This is how the code of the sidebar looks now in index.php

    <div id="sidebar">
    	<ul>
    	<li id="categories-3" class="widget widget_categories"><h2 class="widgettitle">Categorieën</h2>
    		<ul>
    	<li class="cat-item cat-item-3 current-cat"><a href="http://localhost:8888/wordpress/category/nieuws/" title="Alle berichten opgeslagen onder Nieuws bekijken">Nieuws</a> (5)
    </li>
    	<li class="cat-item cat-item-4"><a href="http://localhost:8888/wordpress/category/vacature/" title="Alle berichten opgeslagen onder Vacature bekijken">Vacature</a> (4)
    <ul class='children'>
    	<li class="cat-item cat-item-14"><a href="http://localhost:8888/wordpress/category/vacature/account/" title="Alle berichten opgeslagen onder Account bekijken">Account</a> (1)
    </li>
    	<li class="cat-item cat-item-13"><a href="http://localhost:8888/wordpress/category/vacature/elektro-vacature/" title="Alle berichten opgeslagen onder Elektro bekijken">Elektro</a> (1)
    </li>
    	<li class="cat-item cat-item-12"><a href="http://localhost:8888/wordpress/category/vacature/gelderland-vacature/" title="Alle berichten opgeslagen onder Gelderland bekijken">Gelderland</a> (2)
    </li>
    </ul>
    </li>
    		</ul>
    </li>
    <li id="execphp-3" class="widget widget_execphp"><h2 class="widgettitle">Test Cat</h2>
    
    			<div class="execphpwidget"></div>
    		</li>
        </ul>
    </div><!-- end sidebar3 -->

    Switch to the WordPress Default theme.

    Activate Ottos PHP widget and put the code in a widget. Then see if it shows on the sidebar.

    Thread Starter Paul_NGC

    (@paul_ngc)

    When I switch to the default theme I just see the main. None of my sidebars show. Not even the standard sidebar is visible :S

    Hmm–the WordPress Default theme has one sidebar so you might consider upgrading to 2.9.1 to see if that resolves the Default theme issue.

    Looks like I’m not really getting to the bottom of this problem so if you continue this thread please paste all the code from the theme’s sidebar template file (probably sidebar.php) into a pastebin such as wordpress.pastebin.ca, and report the link back here. Maybe someone can spot your problem. Thanks.

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Can’t display custom field’ is closed to new replies.