• Resolved GoutWeed

    (@goutweed)


    I running a blog with a static front page and was wondering if it is possible to use the custom field to display the most recent post on the front page.
    Fx using <?php wp_get_archives('title_li=&type=postbypost&limit=1'); ?>

Viewing 10 replies - 1 through 10 (of 10 total)
  • Not sure why you would need a custom field. You can just use a second loop like this:

    <?php $my_query = new WP_Query('posts_per_page=1');
    if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
       // Code to display the post
    endwhile; endif;
    ?>

    You can get more info from the Codex:The Loop/Multiple Loops.

    Thread Starter GoutWeed

    (@goutweed)

    Thnx.
    I thought the custom field was a good way to go. But futher reading I have discarded that thought. I guess the solution is to put it inside the loop.
    I’m still not sure how to proceed.

    If you are using a free theme, post a link to your site and I will take a look.

    Thread Starter GoutWeed

    (@goutweed)

    Thnx you very much.
    The site is in Danish, but here’s the URL:
    https://www.ekstremisme.dk/

    Locate <div id="right"> in index.php and paste in the code shown below (don’t include <div id="right">):

    <div id="right">
         <div class="leftcol latest-post">
           <?php
             $my_query = new WP_Query('posts_per_page=1&caller_get_posts=1');
             while ($my_query->have_posts()) : $my_query->the_post();
                setup_postdata($post);
                $do_not_duplicate = $post->ID; ?>
                <div <?php if (function_exists('post_class')) { post_class(); } else { echo 'class="post"'; } ?> id="post-<?php the_ID(); ?>">
                <h2>The Latest Post</h2>
                <h3><a href="<?php the_permalink(); ?>" class="posttitle" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>
                <?php the_content(); ?>
                <?php if (function_exists('the_tags')) { ?>
                <?php the_tags('<p>Tags: ', ', ', '</p>'); ?>
                <?php } ?>
                <div class="metadata"><?php the_time('d M y'); ?> | <?php the_category(', ') ?> | <a href="<?php the_permalink() ?>">Read on</a> | <?php comments_popup_link('Comments (0)', 'Comment (1)', 'Comments (%)'); ?><?php edit_post_link('Edit',' | ',''); ?></div>
                </div>
             <?php endwhile;
             wp_reset_query(); ?>
           </div>

    Then, change this:

    <div class="leftcol">
            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    to this:

    <div class="leftcol">
            <?php if (have_posts()) : while (have_posts()) : the_post();
                 if ($post->ID == $do_not_duplicate) continue; update_post_caches($posts); ?>
    Thread Starter GoutWeed

    (@goutweed)

    Thnx you very much. I’ll give it a try.

    Thread Starter GoutWeed

    (@goutweed)

    Couldn’t get it to work.
    But I tried with the following

    <?php if ( is_front_page() ) { ?>
            <?php
    	query_posts('showposts=1');
    	if (have_posts()) : while (have_posts()) : the_post(); ?>
    		<div>
    			<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>
    
    			<?php the_excerpt(); ?>
    		</div>
    	<?php endwhile; else: endif; ?>
    	<?php wp_reset_query() ?>
    <?php } ?>

    I sort of works, now I have to figure out have to get rid of the few glitches and add a seperate box for the post.

    You might want to change showposts to posts_per_page, as showposts is deprecated. Also, if you ever plan to use sticky posts, you will want to add ‘caller_get_posts=1’ to ignore the sticky posts. Otherwise you will always get the same sticky post every time.

    Thread Starter GoutWeed

    (@goutweed)

    Thnx for the heads up.
    I have made the suggested adjustments.

    If I understand what you mean by ‘add a seperate box for the post’, I think that the box might be controlled by the div classes. Try enclosing your code in <div class="leftcol"> ... </div> tags instead of the plain tags:

    <div class="leftcol">
       <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>
    
       <?php the_excerpt(); ?>
    </div>
Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Custom Field: Post on page’ is closed to new replies.