PHP inside Array

0

I'm creating a loop where I want to display only content that contains the slug category of the page. I'm not sure what variable to put inside the array $args to make the slug of the page itself the category to display.

I have a function that allows me to use <?php the_slug(); ?> , returning the slug from the page, but I'm not sure how to put this in loop .

I have the following code:

<?php
        $args = array('category_name'=>'<?php the_slug(); ?>', 'showposts'=>4);
        $my_post = get_posts( $args );
        if($my_post) : foreach ($my_post as $post) : setup_postdata( $post ); 
      ?>


        <div class="col-xs-6 col-sm-6 col-lg-3"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?><p><?php the_title(); ?></p></a></div>

      <?php
        endforeach;
        endif;
      ?>
    
asked by anonymous 19.08.2018 / 10:02

1 answer

1

Assuming you're using this :

Just use the get_the_slug function instead of the_slug

Switch:

$args = array('category_name'=>'<?php the_slug(); ?>', 'showposts'=>4);

by:

$args = array('category_name' => get_the_slug(), 'showposts'=>4);

Of course you have to see what function you are using, since this is not something that is native to Wordpress.

    
19.08.2018 / 17:43