Trying to loop posts from a certain category by getting the category name and ID - Wordpress

Viewed 10

I am trying to create a page where I will list posts from only a certain category. I have gathered the information about the category, e.g, category name and ID but for some reason, I am not able to loop the posts.

Can you tell me what I am doing wrong?

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
                
                if (strpos($url,'stage') !== false){
                    the_content(
                        sprintf(
                            wp_kses(
                                /* translators: %s: Name of current post. Only visible to screen readers */
                                __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'martitheme' ),
                                array(
                                    'span' => array(
                                        'class' => array(),
                                    ),
                                )
                            ),
                            wp_kses_post( get_the_title() )
                        )
                    );
                }
                else{
                
                $category = single_term_title("", false);
                $current_category_array = get_queried_object();
                $ID = $current_category_array->term_id;
    
 
                echo $category;
                echo $ID;
            
 
 
                $args = array(
                    'post_type' => 'etapi',
                    'category_name'  => $category,
                    'cat' => 46
                );
                $loop = new WP_Query($args);
                
                while($loop->have_posts()): $loop->the_post();
                $title = the_title();
                $link = get_permalink( get_page_by_title( $title ) );
                ?>
                <div class="folderContainer">
                <a href="<?php echo $link; ?>"> <img src="http://localhost/marti/wp-content/uploads/2022/09/Folder-icon.png" alt="" class="folder hvr-float-shadow"> </a>
                </div>
                <?php
                
 
                
 
                endwhile;
                wp_reset_query();
                }
0 Answers
Related