Forum Replies Created

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter shochatd

    (@shochatd)

    The key is the use of ‘global $more;’ just before entering the second loop (the one that pulls posts from the category specified by the custom field named ‘category’).

    <?php
    /*
    Template Name: Category
    */
    
    /* This example is for a child theme of Twenty Twelve:
    *  You'll need to adapt it the HTML structure of your own theme.
    */
    
    get_header(); ?>
    
    	<div id="primary" class="content-area">
    		<div id="content" class="site-content" role="main">
            <?php
            /* The loop: the_post retrieves the content
             * of the new Page you created to list the posts,
             * e.g., an intro describing the posts shown listed on this Page..
             */
            if ( have_posts() ) :
                while ( have_posts() ) : the_post();
    
                  // Display content of page
                  get_template_part( 'content', get_post_format() );
                  wp_reset_postdata();
    
                endwhile;
            endif;
    
        $category = get_post_meta( $posts[0]->ID, 'category', true );
        $cat = get_cat_ID( $category );
    
        //$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
        if ( get_query_var('paged') ) {
          $paged = get_query_var('paged');
        } else if ( get_query_var('page') ) {
          $paged = get_query_var('page');
        } else {
          $paged = 1;
        }
    
        $post_per_page = 4; // -1 shows all posts
        $do_not_show_stickies = 0; // 0 to show stickies
        $args=array (
          'category__in' => array( $cat ),
          'orderby' => 'date',
          'order' => 'DESC',
          'paged' => $paged,
          'posts_per_page' => $post_per_page,
          'ignore_sticky_posts' => $do_not_show_stickies
        );
    
        $temp = $wp_query;
        $wp_query = null;
        $wp_query = new WP_Query( $args );
            ?>
            <?php if ( $wp_query->have_posts() ) : ?>
    
                     <div class="catsec">
    			<?php global $more; /* The loop */ ?>
    			<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
    				<?php $more = 0; // Display content of posts ?>
    				<?php get_template_part( 'content', get_post_format() ); ?>
    			<?php endwhile; ?>
    
    			<?php twentytwelve_content_nav( 'nav-below' ); ?>
    
    		<?php else : ?>
    			<?php ; /* get_template_part( 'content', 'none' ); */ ?>
    		<?php endif; ?>
    
                     </div><!-- .catsec -->
    
    		</div><!-- #content -->
    
        <?php
        $wp_query = null;
        $wp_query = $temp;
        ?>
    
        </div><!-- #primary -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
    Thread Starter shochatd

    (@shochatd)

    I just found the solution here:
    http://stackoverflow.com/questions/13768900/wordpress-pagination-not-working
    The crucial fix was to change this:

    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

    to this:

    if ( get_query_var('paged') ) {
          $paged = get_query_var('paged');
        } else if ( get_query_var('page') ) {
          $paged = get_query_var('page');
        } else {
          $paged = 1;
        }

    The original, simpler version works as long as this is not the home page, but for the home page, only the version that checks ‘page’ works.

    Thread Starter shochatd

    (@shochatd)

    I figured out what I had done wrong. I needed to use:
    global $more;
    outside the loop and
    $more = 0;
    inside the loop (this according to the documentation of the_content()). The problem is that I had inadvertently done this to the first loop, the one that displays the static content, but the place where it was needed was in the second loop, the one that displays the posts (the part I showed above). I assume that what is going on is that my customized page template is executed in a context where $more has somehow been set to 1, but this is not the case when using category.php, so it doesn’t have to be set to 0 there. If anyone looks at my code above, you may spot an additional error I had made. I needed to save $wp_query, and use it (instead of $list_of_posts) and finally restore the saved value, in order for paging to work. Minor final question: How to I post code to this forum without having all the indentation removed as above?

    Thread Starter shochatd

    (@shochatd)

    Ok, here is what really happened. The one page in question had received a pingback “comment” as a result of it being referenced via a link in a post (on the same site). The confusing comment approval mentioned above had nothing to do with the form being submitted and in fact the presence of the form in this one page was irrelevant. Apparently, disabling comments via Screen Options does not prevent pingback “comments”.

    Thread Starter shochatd

    (@shochatd)

    I think I may have some clues. I looked at comments.php in twentytwelve and there is code to output ‘One thought on’ if the function have_comments() returns a true value. That reminded me of something odd: When I tested the form, I (as administrator) was asked to approve a “comment”, even though I have disabled actual comments site-wide. So somehow, I think the act of submitting is being treated as making a comment, requiring approval. Perhaps an anti-spam measure? This may be causing the enclosing page to think it has a comment, and then that code in comments.php to kick in. Perhaps I have to take this up with WP Ninjas.

    Thread Starter shochatd

    (@shochatd)

    Thank you esmi and SWD. I’m sure that will give me what I want, but I have some studying to do (this will be my first time getting into the template code).

    Thread Starter shochatd

    (@shochatd)

    Thank you. That certainly covers exactly what I want to do. But is sounds like the static text would have to be hard-coded in the template file, unlike normal pages. I guess what I’m saying is that what would be ideal would be a true hybrid between a page template and a category template.

    Thread Starter shochatd

    (@shochatd)

    Thank you. I take it that in both cases, I need to delete the menu item in question and then create a replacement of a different type? So for example, to do what you are suggesting, I can’t change the existing menu item from a category to a link menu item?

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