WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom Fields and Custom Templates (10 posts)

  1. stml
    Member
    Posted 6 years ago #

    I'm using a custom field to put a short blurb in the sidebar for some of my posts and pages. This works fine with the following index.php file:

    <?php get_header(); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="post" id="post-<?php the_ID(); ?>">
    	<h2 class="storytitle"><?php the_title(); ?></h2>
    	<div class="storycontent">
    		<?php the_content(__('(Read more...)')); ?>
    	</div>
    </div>
    <?php endwhile; else: ?>
    <?php _e('Sorry, no posts matched your criteria.'); ?>
    <?php endif; ?>
    <?php get_footer(); ?>

    However, I have a static front page and use the following template and a page called 'News' to display some recent short posts (it's mostly a static site):

    <?php /* Template Name: News */ ?>
    <?php get_header();?>
    	<h2 class="storytitle">News</h2>
    	<div class="storycontent">
    	<?php query_posts('showposts=10'); global $more; $more = 0; while (have_posts()) : the_post(); ?>
    	<h3><?php the_time('j/m/y'); ?>: <a>" alt="Read this article..."><?php the_title(); ?></a></h3>
    		<?php the_content(__('(Read more...)')); ?>
    	<?php endwhile; ?>
    	</div>
    <?php get_footer(); ?>

    The special field is called in the sidebar:

    get_post_meta($post->ID, sidebar, true)

    This works fine with the first index.php code, but doesn't return anything for the news.php file.

    It looks like something to do with the Loop, but I can't figure out the code to use, as I thought $post->ID would work outside the loop anyway.

    All help gratefully received.

  2. Chris_K
    Member
    Posted 6 years ago #

    In your second example, is the actual sidebar loaded before or after the content?

    If after the content, I'd expect that it's looking for custom field on the last post listed, right? That would be the current post->ID.

  3. stml
    Member
    Posted 6 years ago #

    Thanks - that makes a lot of sense. The sidebar is loaded after (which is necessary the way the theme is laid out...).

    Any way you can see to get around that? Maybe set $post to another variable before starting the query_posts loop, although that might screw with my sidebar...

  4. stml
    Member
    Posted 6 years ago #

    Just tried adding custom fields to the posts that it's loading - no effect (still nothing in the sidebar).

    Might still be on the right track though - I still think it's something about the loop...

  5. syncbox
    Member
    Posted 6 years ago #

    Hmmm... I'd use the c2c get custom field values plugin for this. You'd still do your custom field entries just as you currently are, but in the sidebar, rather than look for the post ID and such, you simply call that key's entries. You can easily set it up to show in the sidebar ONLY if specific pages or categories, if desired. Let's assume you are using a custom field named "news" and only want it to show when viewing home and news categories for purposes of this example:

    if (is_category('home') || is_category('news')) {
    echo '<ul>';
    echo c2c_get_custom('news','','','','');
    echo '/ul';
    }
    else {//do not show anything }
    ?>

    I think you could also write it this way:

    if (is_category('home') || is_category('news')) {
    echo "<ul>".c2c_get_custom('news','','','','')."/ul";
    }
    else {//do not show anything }
    ?>

    This doesn't have to be in the loop to work.

    You can find the plugin at here and read about how to use it here.

    BTW, the empty parameters are explained in the plugin's examples, but to put it quickly, they are
    ($field, $before='', $after='', $none='', $between='', $before_last=', ', $limit=1, $unique=false, $order='DESC', $include_static=true, $show_pass_post=false)

    * $field : This is the name of the custom field you wish to display

    * $before : The text/HTML to display before all field value(s)

    * $after : The text/HTML to display after all field value(s)

    * $none : The text/HTML to display in place of the field value should no field value exists; if defined as " and no field value exists, then nothing (including no $before and $after) gets displayed

    * $between : The text/HTML to display between multiple occurrences of the custom field; if defined as ", then only the first instance will be used

    * $before_last : The text to display between the next-to-last and last items listed when multiple occurrences of the custom field; $between MUST be set to something other than " for this to take effect

    * $limit : The number of recent custom field values you want returned; default is 1

    * $unique : Boolean value (either 'true' or 'false') to indicate if only the latest instance of $field should be considered, and subsequent occurances of $field would be skipped in the search; default is 'false'

    * $order : The order the results should be returned, either ascending/increasing ('ASC') or descending/decreasing ('DESC'); default is 'DESC'

    * $include_static : Boolean ('true' or 'false') to indicate if static posts (i.e. "pages) should be included when retrieving recent custom values; default is 'true'

    * $show_pass_post : Boolean value (either 'true' or 'false') to indicate if passworded posts should be considered in the search; default is 'false'

    HTH
    syncbox

  6. stml
    Member
    Posted 6 years ago #

    Hmm, thanks for that, but I don't see how this plugin would solve the issue - it extends the possibilities for the custom field but the problem is that the custom field is not being called at all.

  7. stml
    Member
    Posted 6 years ago #

    OK - can confirm that this method is passing the ID of the last post displayed by query_posts to the sidebar, not the ID of the originating page.

    Anyone know how I can get around this?

  8. stml
    Member
    Posted 6 years ago #

    OK, have now updated my news.php to this:

    <?php
    /*
    Template Name: News
    */
    ?>
    <?php
    get_header();
    $newsid = $post->ID;
    ?>
    	<h2 class="storytitle">News</h2>
    	<div class="storycontent">
    	<?php query_posts('showposts=10'); global $more; $more = 0; while (have_posts()) : the_post(); ?>
    	<h3><?php the_time('j/m/y'); ?>: <a href="<?php the_permalink(); ?>" alt="Read this article..."><?php the_title(); ?></a></h3>
    		<?php the_content(__('(Read more...)')); ?>
    	<?php endwhile; ?>
    	</div>
    	<?php $post->ID = $newsid; echo $post->ID; ?>
    <?php get_footer(); ?>

    As you can see, I'm currently echoing the post ID at the end to test it - it shows the correct ID.

    I also echo it in the sidebar, to double-check - and it's still the correct ID.

    However, the get_post_meta($post->ID, sidebar, true) still is not working - nothing happens! If I place it at the bottom of news.php, it prints the correct field content, but refuses to do so in the sidebar - even though it's being passed the correct ID!

  9. stml
    Member
    Posted 6 years ago #

    It wasn't a variable/scoping problem - the problem was in my sidebar:

    <div id="sidebar">
    <h1><span class="smname">Stephenie Meyer</span>&nbsp;</h1>
    <div id="sidebar_content">
    <?php
    if ( is_page() && comments_open() ) { comments_template(); }
    else { echo ''.get_post_meta($post->ID, sidebar, true).'
    '; }
    ?>
    </div>
    </div>

    Because I wasn't using a loop, the if query couldn't understand comments_open() - by adding is_page() as well, I forced it to ignore this on query_posts pages.

  10. stml
    Member
    Posted 6 years ago #

    Note that I still had to reset $post_ID in order to use it in the sidebar after running query_posts:

    <?php
    /*
    Template Name: News
    */
    ?>
    <?php
    get_header();
    $newsid = $post->ID;
    ?>
    	<h2 class="storytitle">News</h2>
    	<div class="storycontent">
    	<?php query_posts('showposts=10'); global $more; $more = 0; while (have_posts()) : the_post(); ?>
    	<h3><?php the_time('j/m/y'); ?>: <a>" alt="Read this article..."><?php the_title(); ?></a></h3>
    		<?php the_content(__('(Read more...)')); ?>
    	<?php endwhile; ?>
    	</div>
    	<?php $post->ID = $newsid; ?>
    <?php get_footer(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic