Using page slug in array

I want to use the page slug as the taxonomy when calling a list of custom posts.

This works:

<?php $page_slug = basename(get_permalink()); ?>
<?php echo $page_slug; ?>

It shows “december-2017” on the page.

So does this:

<ul>
<?php
$args = array(
‘post_type’ => ‘nomination’,
‘post_status’ => ‘draft’,
‘tax_query’ => array(
‘relation’ => ‘AND’,
array(
‘taxonomy’ => ‘month_category’,
‘field’ => ‘slug’,
‘terms’ => ‘december-2017’,
)
)
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<?php the_terms( $post ->ID, ‘dealership_category’, ”, ”, ” ); ?><br>
<a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a><br>
Submitted: <?php echo get_the_date( ‘d/m/Y’ ); ?>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<li>Nothing found.</li>
<?php endif; ?>
</ul>

So why can’t I get this to work:

<ul>
<?php $page_slug = basename(get_permalink()); ?>

<?php
$args = array(
‘post_type’ => ‘nomination’,
‘post_status’ => ‘draft’,
‘tax_query’ => array(
‘relation’ => ‘AND’,
array(
‘taxonomy’ => ‘month_category’,
‘field’ => ‘slug’,
‘terms’ => ‘$page_slug’,
)
)
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<?php the_terms( $post ->ID, ‘dealership_category’, ”, ”, ” ); ?><br>
<a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a><br>
Submitted: <?php echo get_the_date( ‘d/m/Y’ ); ?>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<li>Test not working.3</li>
<?php endif; ?>
</ul>

It shows “Test not working.3” on the page. I’m not great at PHP so am confused.

Read more here:: Using page slug in array

Leave a Reply

Your email address will not be published. Required fields are marked *