• I feel like I’ve exhausted all searches on this subject & still not found one definitive answer! I have a client who needs to display a list of archives by month & with count within a menu. However, because they have also used posts to categorize client case study pages, they only wish to display a list of posts in the menu from one category & by month & with count;

    January 2012 (7)
    December 2011 (2)
    etc..

    I have tried pretty much every suggestion so far on this forum on this subject but with no luck s yet. This article did manage to get the list itself to appear correct but when the links were clicked, archives from all categories appeared again! I imagine that’s down to WP not being able to differentiate between archive categories when also dividing into months.

    I was wondering whether if the archive.php page within my theme could be made to filter only 1 category’s results?

    Anyways, any advise or suggestions would be most welcome!

    Many thanks in advance.

Viewing 15 replies - 16 through 30 (of 41 total)
  • Thread Starter deeve

    (@deeve)

    ..I did run across this article tonight & wondered if it may have some influence on the predicament:

    Display WordPress Archive Lists By Specific Year or Month

    Ah! This makes sense! See, it’s taking the 12 that we set at the beginning and counting it down until it gets to 0 so there are twelve ‘test 1’s! Let’s move the $m declaration (on line 2) down below line 5 (putting is below the echo $y;). This way, each time it hits a new year, it’ll reset the $m variable to 12 again. That’ll mean that after each year, our test code will output 12 ‘test 1’s. Which is fine… for now.

    Then, we have to tweak this query to your db… not having access to your db, I’m gonna have to spin something up on a site of mine to look at it. I feel like we’re really close, so it’s probably something small and stupid. I’ll try something tomorrow morning and let you know!

    If you get a chance, post a pastebin of the template your working with so I can see if anything else in there is interfering.

    Thread Starter deeve

    (@deeve)

    If I use the following:

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    I get the following output*:

    20121211109876543212011121110987655144232120101211109918766154332211220091211111101039876543212008121110987654331212007121110101991881765432221112006121110987654321112005121111110102991881765432111200412111098827654321200312111098765432120021211109876543212001121110987654321

    *year/month #12 through 1, all in a single line.

    Thread Starter deeve

    (@deeve)

    Here’s another development – I thought I’d try a list & moved the variables to this order:

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    returns this output:

    122012
    112012
    102012
    92012
    82012
    72012
    62012
    52012
    42012
    32012
    22012
    12012
    122011
    112011
    102011
    92011
    82011
    72011
    62011
    52011
    51
    42011
    42
    32011
    22011
    12011
    122010
    112010
    102010
    92010
    91
    82010
    72010
    62010
    61
    52010
    42010
    32010
    32
    22010
    12010
    12
    122009
    112009
    111
    102009
    103
    92009
    82009
    72009
    62009
    52009
    42009
    32009
    22009
    12009
    etc..

    & I believe the anomalies contained are the ‘month & #’ of posts but as yet, without the year itself.

    FYI if you post code of more than 10 lines it will eventually be erased by a moderator per the forum rules. Use pastebin for blocks of code longer than 10 lines.

    Let’s start examing the results we should be expecting. How many posts are in this category for the year 2012?

    The code that outputs this:

    returns this output:

    122012
    112012
    102012
    92012
    82012
    72012
    62012
    52012
    42012
    32012
    22012
    12012
    122011
    112011
    102011
    92011
    82011
    72011
    62011
    52011
    51
    42011
    42
    32011
    22011
    12011
    122010
    112010
    102010
    92010
    91
    82010
    72010
    62010
    61
    52010
    42010
    32010
    32
    22010
    12010
    12
    122009
    112009
    111
    102009
    103
    92009
    82009
    72009
    62009
    52009
    42009
    32009
    22009
    12009
    etc..

    seems to be it! So! Then move all the echoes within the WHILE HAVE POSTS loop, under the line $cat_q->the_post(); but above the line $p++;.

    This way, only the year/months with posts will output anything and you wont get a ton of empty year/months.

    Exciting! Results!

    Thread Starter deeve

    (@deeve)

    ..oops – sorry! I wasn’t aware of the 10 line code rule. This is just over but thought you’d like to know I tried the echo lines where you suggested but the output looked wrong in the sense the number of posts per month were incorrect. I then tried it here:

    while ( $cat_q->have_posts() ) {
    				$cat_q->the_post();
    				$p++;
    			}
    			echo '<li>';
    			echo $m;
    			echo $y;
    			echo '(';
    			echo $p;
    			echo ')';
    			echo '</li>';
    			}

    & bingo! The output is correct ??
    Just need to solve the permalink & monthnum as Month name & we’ll be there ??

    current output:

    52011(1)
    42011(2)
    92010(1)
    62010(1)
    32010(2)
    12010(2)
    112009(1)
    102009(3)
    etc.

    Thread Starter deeve

    (@deeve)

    ..just tried:

    echo '<li><a href="';
    			the_permalink();
    			echo '">';
    			echo $m;
    			echo $y;
    			echo '(';
    			echo $p;
    			echo ')';
    			echo '</a></li>';

    but that only gives me the link of one post whereas I’m after something like a ‘get_archives’ by Month permalink of ‘/2011/05/’ where I can see however many posts there are in that Month returned.

    Okay, now that weve got a month, year, and post count, it’s time to move to the next step. Let’s build out those links. You’re not going to want to use the_permalink() for reasons you have already discovered. Instead you’re going to point them to a new page. So the link will be like href="PAGE-SLUG?month=$m&year=$y&cat=YOUR-CATEGORY-ID-#"

    I suggest Adding a new template (call it something like archive-cat-month.php) to your site’s theme directory AND adding a new Page in your WP Called Category Archive (or whatever you want), and assign it the template you just created. This template is going to be where your links will eventually point to.

    In there, you’ll create a few variables like this:

    $year = $_GET['year'];
    $month = $_GET['month'];
    $cat = $_GET['cat'];

    These are going to collect information from your browser’s address bar and create a query to list the relevant posts.

    Once you have all that set up:

    In your new template, under the three variables you just created you are going to want to start a new query to pull up the posts for that category, month, and year. Good thing is, you’ve already written that query once! So, from the code we’ve already written (the stuff that spits out post counts) copy everything from $args = array( ... to wp_reset_query();. This is your query! The only stuff you need to change if the stuff inside the WHILE HAVE POSTS (don’t erase $cat_q->the_post();). Change that to whatever html code you want each post to be listed like. HERE is where you’ll use things like the_permalink() and whatnot.

    Make sense?

    Thread Starter deeve

    (@deeve)

    Brilliant! I understood your logic & am on the case – I already have the links working a treat ??

    In the meantime, do you have any suggestion on how to change $m to Month name? I’ve done some looking around & found the following but not sure if they’d work or where they’d go?

    $monthNum = 5;
    $monthName = date("F", mktime(0, 0, 0, $monthNum, 10));

    or this even:

    for($i=0;$i<6;$i++) {
    echo date(“F”, mktime(0, 0, 0, $i+11, 10));
    }

    Thanks.

    Those may work but I’m not in a place right now I can test them.

    I’d recommend using the php date( 'F' ) function (https://php.net/manual/en/function.date.php) in a switch() statement (https://php.net/manual/en/control-structures.switch.php). Leave $m as a number ( since you need to send it to your new template via a variable) but maybe create a new variable called $m_title and define it in the switch statement.

    There are lots of ways to do that but that’s just off the top of my head and you may be able to figure something out that would use less lines of code.

    Let me know if you need anything else, hope this helped you learn a little about WordPress functionality. If resolved, go ahead and mark the thread as such. Have a nice weekend!

    Thread Starter deeve

    (@deeve)

    For some reason I can’t seem to get the output to go to my new template & it defaults to index.php! I’ve tried declaring it within single.php but that doesn’t seem to make sense as I believe the correct Heirachy for Archive pages would be either category.php or possibly date.php?

    Also, the links currently include the current page within the url so differs depending on what page I’m on which is illogical unless on the blog page:

    https://mysite/blog/2956?month=5&year=2011&cat=7
    can also be:
    https://mysite/blog/2956/2956?month=5&year=2011&cat=7
    if I’m then on that page.

    Any ideas?

    Thread Starter deeve

    (@deeve)

    I’ve uploaded my latest tmpl-archive-cat-month.php code to pastebin so you can see where I’m at. I thought in order to test I’d also make this my index.php as that’s where my outputs is presently going by default. I know it’s not working yet as I’m getting php errors on lines 35, 60 & 62 which I believe is due to the nested functions & am unsure yet as to how to escape them correctly – any suggestions welcomed ??

    https://pastebin.com/vX9hjy44

    Thread Starter deeve

    (@deeve)

    ..also, here’s the code for the menu list which seems to be working well – I still have to include the Month conversion code:

    https://pastebin.com/RTtmBM5U

Viewing 15 replies - 16 through 30 (of 41 total)
  • The topic ‘List of posts from specific category by month & with count?’ is closed to new replies.