WordPress.org

Forums

pre_get_posts // category (4 posts)

  1. kellyalan
    Member
    Posted 10 months ago #

    from our sidebar, using the category widget, I'd like to navigate to a page showing all posts in that particular category (10 posts per page)

    does anyone know where I could find code snippets to input in my functions file for this?

    I found this code but I want category, not archive - when I just changed that word, this code didn't work and I can't seem to find the right phrasing online

    add_action( 'pre_get_posts', 'custom_post_type_archive' );
    
    function custom_post_type_archive( $query ) {
    
    if( $query->is_main_query() && !is_admin() && is_post_type_archive( 'portfolio' ) ) {
    
    		$query->set( 'posts_per_page', '6' );
    		$query->set( 'orderby', 'title' );
                    $query->set( 'order', 'DESC' );
    	}
    
    }
  2. vtxyzzy
    Member
    Posted 10 months ago #

    I think the Categories widget does what you want without any coding changes. What is it doing differently than what you want?

  3. kellyalan
    Member
    Posted 10 months ago #

    as of now, it's coded as:

    $paginar=1;
      $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $args = array(
                                'paged' => $page,
                                'post_status' => 'publish',
                                'order_by' => 'date',
                            );
    query_posts($args);
    
      while (have_posts()) {
                   the_post();
                   $day = get_the_time('F j, Y');
                   $cate = get_the_category(get_the_id());
                   foreach($cate as $k){
    }

    output is: https://www.absolutept.com/category/electric-stimulation-2/

    it's pulling posts that are in categories that aren't in the Electric Stimulation category and it's not stopping once you're at the last post(select page 4 - goes to a blank page)

    We have talked with the theme creator but not much luck - seems like there should be a way to recode the child theme function.php and the category.php with pre_get_posts code instead of query_posts

  4. vtxyzzy
    Member
    Posted 10 months ago #

    First, make sure that category.php is the file being used.

    If it is, put the entire file in a pastebin and post a link to it here.

Reply

You must log in to post.

About this Topic