• Forgive me, folks, I probably have NO idea what I’m talking about…

    So I’m using the Coraline theme and creating a child theme to make various modifications to it.

    I need to create several category templates that display all the posts in that category BY TITLE ONLY with no other information. (It’s a recipe blog, I just want the titles of the recipes on the DESSERTS page, for example. You click on the title of the recipe, it takes you to the single post page of the recipe.)

    There is a wealth of information about how to do this, however, all the info seems to be for previous versions where the loop was included in each template. Now the loop only resides in loop.php. So none of the information I’m finding applies to my version. This is the code for the category.php file in Coraline:

    <div id="content-container">
    <div id="content" role="main">
    
    <h1 class="page-title"><?php
    printf( __( 'Category Archives: %s', 'coraline' ), '<span>' . single_cat_title( '', false ) . '</span>' );
    ?></h1>
    <?php
    $category_description = category_description();
    if ( ! empty( $category_description ) )
    echo '<div class="archive-meta">' . $category_description . '</div>';
    
    get_template_part( 'loop', 'category' );
    ?>
    
    </div><!-- #content -->
    </div><!-- #content-container -->

    I know that, in order to show the title of the post only, I need to remove <?php the_content() ?> the_permalink the_excerpt, etc. However…these things do not exist in the template.

    I know that I need to add <?php query_posts('posts_per_page=1000'); ?> in order to ensure that I have every post in the category showing on one page, but since there’s no loop, I’m not sure where to add that.

    So I guess my basic question is…how do you create a category-slug template in the NEW wordpress if I need to modify the loop to list only post titles, and to list every post title within that category? Do I have to modify loop.php? And if I do, can I have a child loop.php that will override the parent loop.php in the Coraline folder?

    This quickly got far too advanced for me…if I was working on an older version of WP it’d be a piece of cake using the examples I’ve found…but none of the examples apply because the loop code is no longer inside each template.

Viewing 15 replies - 1 through 15 (of 21 total)
  • You can duplicate the category.php file for each of the categories that you need to display the content differently for. Name each file as category-[slug].php and copy the loop from loop.php and hack them to your liking.

    Thread Starter TheBenStarr

    (@thebenstarr)

    But won’t this code mess everything up by also calling the loop from loop.php?

    get_template_part( 'loop', 'category' );

    Also, the loop has become FAR more advanced than the simple “poetry” loop that is described in all the books and 99.999% of examples in this forum and around the internet. There’s no a <?php the_content() ?> for me to delete anywhere in loop.php to ensure that only the title of the post is displayed.

    Hi Ben,
    Open loop.php and save this as loop-titles.php, open loop-titles.php and make any changes in the new file!

    Or copy and save this as loop-titles.php, just post title in the loop

    get_template_part({slug},{name}); How this works is {slug} is the template file, in this case loop.php, {name} looks for a hyphon ‘-‘ and variant of the loop, in this case it is {category}, WordPress will look for slug-name.php, if it finds it it will include it, if not it will fall through to slug.php

    This looks for a file loop-category.php if it is not present then loads loop.php

    get_template_part( 'loop', 'category' );

    In your template files change it to:

    get_template_part( 'loop', 'titles' );

    HTH

    David

    Thread Starter TheBenStarr

    (@thebenstarr)

    David…thank you, I THINK I comprehend this. Though all that nasty code in loop.php is too complex for me to understand. Can I just replace it with simple loop code from previous versions of WP? My loop.php has all sorts of exceptions for displaying different types of content. I really just need a simple query string to display content from one category, titles only…does this look right?

    <?php
    
    query_posts( '$query_string.'&posts_per_page=1000&category_name = desserts' );
    
    if ( have_posts() ) while ( have_posts() ) : the_post();
    
    echo '<li>';
    
    the_title();
    
    echo '</li>';
    
    endwhile;
    
    wp_reset_query(); ?>
    Thread Starter TheBenStarr

    (@thebenstarr)

    David, please disregard the previous message. My brain is farting. You sent me code, I’m working on it…

    Hi Ben,
    I have just edited the post above and included a pastebin, this gives the previous and next page links

    You would not want 1000 links on a page, so look to add them in a div and style the div for say 50 on a page!

    This example use a page.php as a template page then use the page slug to match the category slug.

    global $post;
    $slug = $post->post_name;
    $args = array(
       'posts_per_page' => 30,
       'category_name' => $slug,
       'paged' => $paged
    );
    query_posts( $args );

    HTH

    David

    Thread Starter TheBenStarr

    (@thebenstarr)

    David, I pulled the code you gave me and saved it as loop-titles.php and uploaded it to the Coraline theme folder.

    Then, I created category-desserts.php from the category.php file and it looks like this:

    [code moderated – please use the pastebin, if the code exceeds 10 lines]

    Then I created a page using the new Desserts template. Unfortunately, no posts load. ?? However, you just made some edits, so I’m going to try what you just posted…

    You are SO amazing to be helping me out like this…

    Hi Ben!

    Have a look at this as a solution, it uses page.php far better, and it also uses the page and category slugs to find the recepies, so just a single template needed!

    This is a page template that give the page title and content, and the list of news stories underneath, so in your case you can title the page, Cup Cakes {cup-cakes} write a few lines about Cup Cakes, with the Cup Cake {cup-cakes} category post lists below.

    HTH

    David

    Thread Starter TheBenStarr

    (@thebenstarr)

    David, I can’t thank you enough for your help. I used this new template you sent…I saved it as category-desserts.php and I changed the heading so it registers as the template: desserts

    I uploaded it to my Coraline-child folder.

    I created a new page called “desserts” and selected the Desserts template and published it. When I attempt to preview it, I just get a spinning cursor…nothing is rendered at all.

    Any suggestions? I’m about to pull a second mortgage so I can afford an expert to come to my house and figure this out. Ugh…

    Thread Starter TheBenStarr

    (@thebenstarr)

    Was I supposed to change some of the information in the page template other than the title? Like this section?

    ‘category_name’ => $slug,
    ‘paged’ => $paged

    Hold fire for a bit, I have started so will finish with a tested solution, note the page names!

    File Save As page-category.php
    File Save As: loop-title.php

    David

    Thread Starter TheBenStarr

    (@thebenstarr)

    David, first off…you are amazing to help me like this, and I’m going to send you the biggest box of cookies and cupcakes you’ve ever seen.

    Second…we’re VERY close. This actually DID render a list of post titles, but it’s ALL the titles of ALL my posts.

    I saved the page as page-desserts.php
    I created a new page called DESSERTS, selected the Category Page template (I did not change the name of this, maybe that’s the problem?)

    The page displays all titles from ALL posts in my blog, regardless of category.

    Any tweaks I can make so that it only shows titles from the DESSERTS category?

    Thread Starter TheBenStarr

    (@thebenstarr)

    David, in my first attempt I did save the first file as page-category.php and got the full list of every post in every category, so in my second attempt I named the first file page-desserts.php thinking that would make the distinction so the loop only called posts from category desserts. Same result each time, though.

    Thread Starter TheBenStarr

    (@thebenstarr)

    David, I also tried this…in the page-category.php file:

    * A custom page template Page Name = Category Name .

    I replaced Category Name with Desserts

    Same result…a full list of all posts titles, regardless of category.

    I feel like this is something I’m neglecting to do with your code, because your code looks rock solid.

    Ok.
    Save the two pastebins as I named them, don’t change anything the page-category.php, requires the template part to be called content-title.php nothing else.

    Then create a page with the same slug as your category, page slug can be viewed [Screen Options] > Slug[x], then make sure the page slug = category slug

    Screenshot:

    That is it it will work as it “says on the tin”!

    Screenshot:

    HTH

    David

Viewing 15 replies - 1 through 15 (of 21 total)
  • The topic ‘Modifying the Loop for category templates’ is closed to new replies.