A
A
Alexey Mikhailov2020-07-21 16:57:12
WordPress
Alexey Mikhailov, 2020-07-21 16:57:12

How to handle categories in WordPress?

Good afternoon!

1. There is a WordPress site in which the output of news of a certain category is implemented not as it should be through a separate url of the form www.goodsite.com/category/category-name, but on the general page www.mysite.com/news (that is, all news in full and news of any category are displayed on the same page (with the same url).This was done using buttons with the names of the categories, on which jQuery is hung.If you are interested, below is the code that implements this, but the point is not in it.

function() {
  $(".blog .nav-item").removeClass("this-item"), $(this).addClass("this-item");
  var s = $(this).attr("data-news");
  $(".block-news").removeClass("block-news_show"), $(".block-news" + s).addClass("block-news_show")
}


2. Each separate news has a link to the list of news of the category to which this news belongs. Since we remember that category news is displayed by jQuery on the www.mysite.com/news page itself (news.php file), this link is implemented in a special way:

<?=
    ot_get_option('footer_link6')      // это ссылка на mysite.com/news/
     . '?cats='      
     . $number         // это айдишник категории
?>


As a result, a link of the form mysite.com/news/?cats=3 is generated, where 3 is the id of category

3. In the news.php file, this link is processed as follows:

<?php if(isset($_GET['cats'])) { ?>
                <?php if( ($_GET['cats'] == 3) ) { ?>


If the check succeeds, the JS code is run,
function a() {
                        $('.nav-item3').click();
                    }
                    setTimeout(a, 0.3);

which, if I'm not mistaken, clicks on the button, on which, as we remember from point 1, another JS code is hung, which leaves only the news of the desired (selected) category on the mysite.com/news/ page.

More complete version:
<script>
        $(document).ready(function() {
            $('.header a').css('color', '#171717');
            $('.header .this-nav a').css('color', 'white');
            $('.header').css('background', 'white');
            $('.header-nav_list .current_page_item').addClass('this-nav');
            $('.bread').find('span').addClass('mobile-hidden');
            $('.bread .current').removeClass('mobile-hidden');
      
      var button_lang = localStorage.getItem('switch_lang');
      
      if(button_lang == 'en') {
        $('.mc_signup_submit input').attr('value','Subscribe');
      } else {
        $('.mc_signup_submit input').attr('value','Подписка');
      }

            <?php if(isset($_GET['cats'])) { ?>
                <?php if( ($_GET['cats'] == 3) ) { ?>
                    function a() {
                        $('.nav-item3').click();
                    }
                    setTimeout(a, 0.3);


Questions:
1. Is it possible to replace a link like mysite.com/news/?cats=3 with a link like mysite.com/news/category-name. Well, that is, you can replace it, but how to configure its reception if you don’t use GET anymore? Split url by slashes? Maybe you can set the link mysite.com/news/?cats=3 "pseudonym" (is there such a thing as a "link alias" at all?)

2 philosophical question. How harmful is such a non-standard implementation of displaying category records? Maybe it's better to file separate pages for each category, as originally conceived in WP?

Original question:

There is a site on WP, it has a News section. The news itself, located in the News, belongs to one of three categories. In the body of each individual news, there is a link to a list of all news of the same category as this news. Link like: mysite.com/news/?cats=3. When this link is accepted, then 3 is pulled out by GET, and further processed.

I changed mysite.com/news/?cats=3 links to mysite.com/news/category-name. How can I now configure the acceptance of these links, given that I cannot use GET now?

Or was it necessary not to touch the link and somehow adjust .htaccess so that the link simply displays beautifully, and the logic remains the same?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Plawkevich, 2020-07-22
@DPhil

It's hard to answer without code,
but in general I would output like this

//получаем все категорию новость
<?php $categories = get_categories([
     'parent' => ID
]);
//проходимся по массиву и вытаскиваем значения
foreach($categories as $category):?>
<div><a "href=<?php echo $category->term_id;?>"><?php echo category->name;?></a><div>
<?php endforeach;?>
// ну и далее выводим уже наши посты в нужной вам обертке и и тд
<?php 
$post = get_posts([
'category' => ID
]);
foreach($posts as $post):?>
//разметка 
<div> <?php the_title?>/div>
//и тд и тп
<?php endforeach;?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question