• Hello my fellow WPers

    I’m making my own WP theme, and on some pages I want to only show one category pr. page, so that the users that aren’t as interested in my political posts can view only posts concerning football, for instance.

    So what I have to do is of course spesifcy ‘category_name’ => and then make sure the right category is passed in, but how to I obtain the name of the current category I am on? I tried to use single_cat_title, but that did not work.

    And just so it is meantioned, I would very much like not to make spesfic category-3,4,5 etc. pages and spesifcy the category for each page, if that’s possible.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter LBA1403

    (@lba1403)

    Currently I have this on top of my category.php:

    $cat_name = get_the_category( $id );
    “$args = array( ‘posts_per_page’ => 6, ‘post_type’ => ‘larsarholmnet_posts’, ‘category-name’ => ‘$cat_name’ );”

    How would it be possible for me to alter this so that it only shows one category pr. page based on the category name?

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it without a query in the category.php and with this in your theme’s functions.php:

    function my_post_queries( $query ) {
      // not an admin page and is the main query
      if (!is_admin() && $query->is_main_query()){
        if(is_category()){
          $query->set('posts_per_page', 6);
          $query->set('post_type', array('larsarholmnet_posts'));
        }
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    btw:
    consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost.

    Thread Starter LBA1403

    (@lba1403)

    Thank you for your response!

    I am a bit new to both WP and php, so I’ll have to ask the stupid question, when you say “without a query in category.php” do you mean to remove the lines I’ve posted in the first post and then copy+paste the code-snippet into my functions.php?

    I guess it wasn’t, because when I did that it didn’t make it any better, it shows no posts..

    But I made a child theme, seemed like a very good idea, thanks for the suggestion!

    Moderator keesiemeijer

    (@keesiemeijer)

    Can you post the code of catetegory.php – https://codex.www.ads-software.com/Forum_Welcome#Posting_Code

    Thread Starter LBA1403

    (@lba1403)

    I most certainly can do!

    <?php get_header();?>
    
    <section class="featured-post fifteen columns row">
    <h1><?php single_cat_title(); ?></h1>
    <ul>
    <?php
    	$query = new WP_Query( $args );
    	while ($query->have_posts()) : $query->the_post();
    	?>
    
        <div class="cat-thumb">
        <a href="<?php the_permalink(); ?>">
       <h2><?php the_title();?></h2>
        <?php the_post_thumbnail('cat-thumb'); ?>
       <p><?php the_excerpt(); ?></p>
         </a></div>
    
    <?php endwhile;?>
        </ul>
    <div id="push"></div>
    </section>
    
    <?php get_sidebar();?>
    
    <?php get_footer();?>

    Maybe I’m missing something but WP already does what you want by default. The only place posts from different categories would be mixed is a date archive page. Instead of trying to filter those pages, just add the categories to your navigation so people can select what they want to see, without having to filter on a date archive page.

    Thread Starter LBA1403

    (@lba1403)

    @webbrewers Well, if WP already did it, I would not be asking. I haveset it up with categories in the navigation, but the posts are not sorting them, only giving me all the posts in all the categories pages..

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘One category pr. page’ is closed to new replies.