WordPress.org

Forums

[resolved] Custom Page Template - display posts (3 posts)

  1. jwhazzard
    Member
    Posted 5 years ago #

    Hello All,

    I need help with a custom page template for displaying all posts in a certain category underneath the page content. I have the code 90% working but one little bug eludes me.

    Page is drawing the category from a custom field "showcategory" whose string value should match the post category to be displayed on the page.

    What I have:

    ...normal page template...
    <!-- article-content -->

    <?php if (is_search()) the_excerpt(); else the_content(__('Read the rest of this entry »', 'kubrick')); ?>

    <?php if (is_page() or is_single()) wp_link_pages(array('before' => '<p>Pages: ', 'after' => '</p>', 'next_or_number' => 'number')); ?>

    <!-- Display all posts of category set by post custom value key "showcategory" -->
    <!-- Segment 1: Get custom value and assign to $catstubs -->

    <?php $catstubs = get_post_custom_values('showcategory'); ?>
    <?php echo "Category: $catstubs[0]";
    $currentcat = $catstubs[0];
    if (is_string($currentcat)) {
    }
    else { echo "it's not a string"; }
    ?>

    <!-- Segment 2: display all post (title permalink and except) for $catstub category -->

    <?php $myposts = get_posts('category_name=$currentcat');
    foreach($myposts as $post) :
    setup_postdata($post); ?>

    <div class="art-post"><?php the_excerpt(); ?></div>

    <?php endforeach; ?>

    <!-- -->

    <!-- /article-content -->
    ..continue normal template...

    I'm getting a blank page when the category_name param is drawing from a variable.

    Any help would be greatly appreciated!

    Thanks!

  2. MichaelH
    Member
    Posted 5 years ago #

    <!-- Segment 2: display all post (title permalink and except) for $catstub category -->
    <?php
    $cat_id = get_cat_ID($currentcat);
    $args=array(
      'cat' => $cat_id,
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    <!-- -->
  3. jwhazzard
    Member
    Posted 5 years ago #

    Brilliant! Thank you.

    I'm still getting the hang of the WP_Query use and this helps tremendously.

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic