Sunday, January 1, 2023
HomeWordPress Developmentphp - Tips on how to type wordpress posts by deciding on...

php – Tips on how to type wordpress posts by deciding on a 12 months from a drop down?


I answered my very own query. I am only a newbie, so the code is trash; I do know, do not decide.

What it’s important to do is create a brand new question. First, go to your themes class.php file.

Folks want to pick out the 12 months out of your ul/li menu. For me it’s:

<ul class="dropdown-menu">
<li class="nav-item"><a href="?orderbyyear=2021" class="nav-link dropdown-item" sort="button" function="tab">2021</a></li></ul>

‘?orderbyyear=2021’ Would not exist; I made it up. It does not do something in WordPress. We’re simply checking the URL for ‘2021’. Subsequent, add these under:

<?php
                                $cat_ID = get_query_var('cat'); // Get the present class, should you do not it'll show all (your selection)
                                $args = array(
                                    'cat' => $cat_ID, // decide, you add different stuff right here. Reminiscent of post_per_page, cat_id, and many others.
                                    'date_query' => array(
                                        array(
                                            '12 months'  => 2021,
                                        ),
                                    ),
                                );
                                $myYearQuery = new WP_Query($args); ?>

Subsequent, test your URL for the 12 months you want (it’s good to create a brand new if-else for every year you need). We have to test the URL for ‘2021’ utilizing if-else. I am certain there’s a less complicated and smarter approach that I do not know. Perhaps it may be created within the features.php and use get.

                                <?php
                                $url = $_SERVER["REQUEST_URI"];
                                $isityear = strpos($url, '2021');
                                if ($isityear!==false) {
                                if ( $myYearQuery->have_posts() ) : whereas ( $myYearQuery->have_posts() ) : $myYearQuery->the_post(); ?>

Do your weblog’s php/html/css -here- and finish it.

<?php endwhile; endif; ?>

Repeat this for all of your years. On the very finish, you additionally want so as to add your regular code. As a result of if individuals do not choose a 12 months, it’s good to show put up usually.

<?PHP else if (have_posts()) : whereas (have_posts()) : the_post(); ?>

Do not forget to finish your regular whereas/if as effectively.
Effectively. That is how I solved it. Hope it helps somebody. I am certain individuals who know will do it simply.

You can too add pagination should you want. I’ve did it like this:

<?php the_posts_pagination( array(
    'mid_size' => 1,
    'prev_text' => __( '<', 'textdomain' ),
    'next_text' => __( '>', 'textdomain' ),
) ); ?>

So what we acquired is

area/class/ > Regular Posts
area/class/?orderbyyear=2022 > Posts from 2022
area/class/?orderbyyear=2021 > Posts from 2021
area/class/?orderbyyear=2020 > Posts from 2020…

And so forth.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments