0

For now, I am looking for suggestions on how can I do it.

To get the current taxonomy:

$queried_object = get_queried_object();
$term_id = $queried_object->term_id;

To get the latest 5 items:

$query_args = array(
    "posts_per_page" => "5"
);

$listedPosts = new WP_Query($query_args);

// the loop

if ( $listedPosts->have_posts() ) {
    while ( $listedPosts->have_posts() ) {
        $listedPosts->the_post();
        if ( (int) $listedPosts->current_post => 5 ) {
          // change the drop down field item to "Visible" from Invisible
        } else {
          // stay as is
        }
    } 
}
  • 1
    you only get 5 posts; as 'current_post' starts with 0 zero, it will only ever go to maximal 4... – Michael Jun 6 '17 at 1:18
0

Have a closer look to the blow code-

$query_args = array(
    'posts_per_page'    => '5',
    'orderby'           => 'post_date',
    'order'             => 'DESC',
);

$listedPosts = new WP_Query($query_args);

// the loop
if ( $listedPosts->have_posts() ) {
    while ( $listedPosts->have_posts() ) {
        $listedPosts->the_post();
        if ( (int) $listedPosts->current_post == 5 ) { // You need to use '==' or '===' for comparison.
            // change the drop down field item to "Visible" from Invisible
        } else {
            // stay as is
        }
    }
}

And-

$queried_object = get_queried_object();
$term_id = $queried_object->term_id;

This above code will give you the term ID when you are at a taxonomy.php page or archive.php page. In single.php page for getting taxonomy terms you need to use the below code-

For inside a loop-

//Do something if a specific array value exists within a post
$term_list = wp_get_post_terms(get_the_ID(), 'taxonomy _name', array("fields" => "all"));
foreach($term_list as $term_single) {
    echo $term_single->slug; //do something here
}

For outside of the loop-

//Do something if a specific array value exists within a post
$term_list = wp_get_post_terms($post->ID, 'product_features', array("fields" => "all"));
foreach($term_list as $term_single) {
    echo $term_single->slug; //do something here
}

Hope that helps.

  • thanks! Any suggestion on how to change the drop down item? – Zeki Jun 6 '17 at 0:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.