1

My client wants to have the ability to call up a common category used in multiple custom post types. So if there is a common category of "budget", then a blog listing would come up with all the custom post types that have a category of "budget".

I read up on a few examples here of using args for different post types, and pieced them together on a custom template called "page-category-budgeting.php". This is in a child theme based on Twenty-twelve.

After testing and working through some debugging, I'm still without success. The latest error is "Parse error: syntax error, unexpected end of file in /home/acs10047/public_html/wp-content/themes/moneysmartfamily/page-category-budgeting.php on line 53"

But that is a blank line at the end of the code. I have nothing to go with to figure this out. Here is the template code. I must be missing something simple or overlooking how this should be set up. Could someone advise me as to what I'm missing? Thanks.

<?php
/**
 * Template Name: Category - Budgeting
 *
 *
 * The template for displaying all pages.
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages
 * and that other 'pages' on your WordPress site will use a
 * different template.
 *
 * @package WordPress
 * @subpackage Twenty_Twelve
 * @since Twenty Twelve 1.0
 */

get_header(); 

    $args = array('post_type' => array('post','media-appearance','members-archive','money-saving-tips','review'),
    'posts_per_page' => '20',
);

    $this_query = new WP_Query( $args ); ?>


<div id="primary" class="site-content">
    <div id="content" role="main">

    <?php while ( $this_query->have_posts()) : $this_query->the_post(); {?>

        <?php if( has_post_thumbnail() && ($cat == 'budget') ): ?>
    <div class="tip-excerpt">                                     
        <h1 class="entry-title">
            <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
        </h1>

    <div class="entry-content">
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail', array('class' => 'alignleft')); ?></a><?php the_excerpt(); ?>
    </div>
    </div>
        <?php endif; ?>        

                <?php wp_reset_query(); ?>
            <?php  // done the foreach statement ?>


    </div><!-- #content -->
</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
0

You are missing <?php endwhile; ?> before the end of content div.

get_header(); 

    $args = array('post_type' => array('post','media-appearance','members-archive','money-saving-tips','review'),
    'posts_per_page' => '20',
);

    $this_query = new WP_Query( $args ); ?>


<div id="primary" class="site-content">
    <div id="content" role="main">

    <?php while ( $this_query->have_posts()) : $this_query->the_post(); {?>

        <?php if( has_post_thumbnail() && ($cat == 'budget') ): ?>
    <div class="tip-excerpt">                                     
        <h1 class="entry-title">
            <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
        </h1>

    <div class="entry-content">
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail', array('class' => 'alignleft')); ?></a><?php the_excerpt(); ?>
    </div>
    </div>
        <?php endif; ?>        

                <?php wp_reset_query(); ?>
            <?php  // done the foreach statement ?>

     <?php endwhile; ?> //ADDED CODE
    </div><!-- #content -->
</div><!-- #primary -->

<?php get_sidebar(); ?>
  • Okay. Thanks for noticing that. In my debugging I had an "expected endwhile" and removed it and it kicked the next issue. In the codex it had an example without an endwhile so I guessed it was okay to not use that. Apparently not. I'll try that out. – David Borrink Sep 21 at 15:31
  • Nope, that's not working. By the way, I noted that you missed a ? before the end > of your code addition. I tried this and got "Parse error: syntax error, unexpected 'endwhile' (T_ENDWHILE) in /home/acs10047/public_html/wp-content/themes/moneysmartfamily/page-category-budgeting.php on line 47". What would cause that? – David Borrink Sep 21 at 16:47
0

This should work for you. I made some modifications to your code. you can message me if you need help with the layout of the elements.

    get_header(); 

    $args = array(
        'category_name'  => array(
            'budget'
        ), // category_name (string) – use category slug.
    );

    // This query should now only have the post_type in the args above that are in the category budget.
    $this_query = new WP_Query( $args ); ?> 


<div id="primary" class="site-content">
    <div id="content" role="main">

    <?php 
    if ( $this_query->have_posts() ) :

        while ( $this_query->have_posts() ) : $this_query->the_post(); 
            $post_id = get_the_ID(); // In case you need to use the post ID
            ?>

            <?php if( has_post_thumbnail() ): ?>
        <header class="entry-header">

            <h1 class="entry-title">
                <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
            </h1>
            <span class="post-meta">Author  <?php the_author_posts_link(); ?> | <?php the_time('F jS, Y'); ?>  | <?php the_category(', '); ?></span>

        </header><!-- .entry-header -->


              <div class="entry-summary">
            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail', array('class' => 'alignleft')); ?></a>

            <?php the_excerpt(); ?>
        </div><!-- .entry-summary -->


            <?php endif; ?>

        <?php
        endwhile;

    endif;
    ?>
    </div><!-- #content -->
</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
  • 1
    Thank you, Rudy. I see a couple significant things you did here by putting a category_name array in the args (besides tidying up the post type presentation - nice). I haven't done much with queries in my development and this project request is getting me some long overdue exposure to the concept. I'll give it a try. – David Borrink Sep 22 at 2:31
  • 1
    It seems to have worked! I'll have to verify with my client if it is indeed calling up from the different CPTs as he'll be able to tell better. I'll follow up with more info. – David Borrink Sep 22 at 2:36
  • If it worked can you please mark as the answer. Thanks. – Rudy Lister Sep 22 at 6:37
  • Well, it is showing the current blog roll. If you compare the test page (moneysmartfamily.com/?page_id=22149) with the normal blog page (moneysmartfamily.com/blog), you'll see the items are the same. If you look at one of their existing category pages (here is "budget": moneysmartfamily.com/?cat=214) that would seem to be the items we should be seeing in the new test page. – David Borrink Sep 22 at 18:32
  • Your args seemed sound. Anyway, I posted an updated template as an answer. I added more template to style the page more like the client's blog. Sorry, but the paste-in got wonky and I had to combine lines so that everything showed up. – David Borrink Sep 23 at 2:59
0

Another topic created here gave me the answer... very simple args structure now that I know.

 get_header(); 

    $args = array(
      'post_type' => 'any', //or use cpt slug as string, or array of strings if multiple
      'category_name' => 'budget', //use 'budget,premium' if you want posts with budget OR premium, use 'budget+premium' if you want budget AND premium. 
    );


    // This query should now only have the post_type in the args above that are in the category budget.
    $this_query = new WP_Query( $args ); ?> 


<div id="primary" class="site-content">
    <div id="content" role="main">

    <?php 
    if ( $this_query->have_posts() ) :



        while ( $this_query->have_posts() ) : $this_query->the_post(); 
            $post_id = get_the_ID(); // In case you need to use the post ID
            ?>

            <?php if( has_post_thumbnail() ): ?>
        <header class="entry-header">

            <h1 class="entry-title">
                <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
            </h1>
            <span class="post-meta">Author  <?php the_author_posts_link(); ?> | <?php the_time('F jS, Y'); ?>  | <?php the_category(', '); ?></span>

        </header><!-- .entry-header -->


              <div class="entry-summary">
            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail', array('class' => 'alignleft')); ?></a><?php the_excerpt(); ?>
        </div><!-- .entry-summary -->


            <?php endif; ?>

        <?php
        endwhile;
        wp_reset_postdata();
    endif;
    ?>
    </div><!-- #content -->
</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.