How do I get id of a category using the return of the category name of the single_cat_title() Wordpress function;

Asked

Viewed 1,060 times

0

Friends I have a menu with the following links from Categories Products and News I want you to click on any of the links and my page Category.php return only the posts of the category to which it was clicked.

I have on my page single_cat_title(); which returns the category name, I would like to know how to get the ID of this category to use in the $args array

$args = array('post_type'=>'post', 'Category'=>CODIGO_CATEGORIA, 'showposts'=>3);

<div class="container">
    <?php
        $args = array('post_type'=>'post', 'category'=>single_cat_title(), 'showposts'=>3);
        $my_posts = get_posts( $args );
        if($my_posts) : foreach($my_posts as $post) : setup_postdata( $post );
     ?>

        <div class="row">
            <div class="col-md-3 col-lg-3">
                <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(false, array('class'=>'img-responsive img-post-blog')); ?></a>
            </div>
            <div class="col-md-9 col-lg-9">
            <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <?php the_excerpt(); ?>
            </div>
        </div>

     <?php
        endforeach;
        endif;
    ?>
</div>

1 answer

1

I managed to solve the problem.

$page_object = get_queried_object();
$categoriaID = get_cat_ID($page_object->cat_name);
$args = array('post_type' => 'post', 'category' => $categoriaID, 'showposts' => 3);

I returned with an answer because this may help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.