Link each category to last post

You can filter category_link and replace the URL here. I have used the newest post in the following example, because first could also mean the oldest and that sounds … strange. 🙂 add_filter( ‘category_link’, ‘wpse_96677_cat_link_to_first_post’, 10, 2 ); function wpse_96677_cat_link_to_first_post( $url, $term_id ) { $term = get_term( $term_id, ‘category’ ); // sub-terms only if ( … Read more

Query the Loop without breaking it

That’s a really inefficient way to do it. Just use the $count variable you declared at the top, without any query_posts(): $count = 0; while ( have_posts() ) : the_post(); if ( $count < 1 ) { // first post } elseif ( $count <= 4 ) { // next 4 posts } else { … Read more

Pagination on child category returns 404

There are a lot of questions here regarding pagination, it is definitely one of the least understood aspects of how WordPress works internally. To understand why you get 404s, we’ll start by looking at the Action Reference in Codex to see the process WordPress follows for each request. The process begins by loading up the … Read more

how to show posts of category random by session

You don’t need complicated filters to modify the SQL query, just use the order parameter of WP_Query ( avoid query_posts, your code above will trigger multiple unused queries, making the page very slow ) $q = new WP_Query( [ ‘order’ => ‘rand’ ] ); You can pass an integer seed too: $q = new WP_Query( … Read more

Assign different category colours to different categories in the main menu

Got it to work, finally! Nothing like a good sleep to restart your brain 🙂 Updated code that works: $item_output = $args->before; if( $item->object == ‘category’ ) { $category = get_category( $item->object_id ); $category_color = get_field( ‘category_color’, $category); $item_output .= ‘<a style=”color:’ . $category_color . ‘;”‘. $attributes .’>’; } else { $item_output .= ‘<a’. $attributes … Read more