PHP inside the Array

Asked

Viewed 31 times

0

I’m creating a loop where I want to display only content that contains the Slug page. I’m not sure which variable to put inside the array $args to make the Slug of the page itself be the category to be displayed.

I have a function that allows me to use <?php the_slug(); ?>, returning the Slug of the page, but I’m not sure how to put this in the 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;
      ?>
  • It wouldn’t just be 'category_name' => the_slug()?

1 answer

1


Supposing you are using this:

Just use the function only get_the_slug instead of the_slug

Change:

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

for:

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

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

Browser other questions tagged

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