根据分类在5个最新帖子之后更改下拉列表中的项目

时间:2017-06-05 作者:Zeki

目前,我正在寻找如何做到这一点的建议。

要获取当前分类,请执行以下操作:

$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
要获取最新的5个项目:

$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 个回复
SO网友:CodeMascot

仔细查看blow代码-

$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
        }
    }
}
以及-

$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
当您在taxonomy.php 页码或archive.php 页在里面single.php 获取分类术语的页面您需要使用以下代码-

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
}
希望这有帮助。

结束

相关推荐

Display taxonomy posts

这似乎非常基本,但在任何地方都找不到明确的答案。我所要寻找的就是在分类法中显示帖子的循环。假设我有一个名为custom_category 在这其中,我有几个类别,比如美国和加拿大,然后爱达荷州和华盛顿是我们的孩子。所以url看起来像http://example.com/custom_category/us/idaho/. 我所要做的就是将正确的PHP添加到taxonomy.php 页面显示每个类别中的帖子。因此,只有当你在爱达荷州的网页上时,你才能看到贴在上面的帖子,而当你在华盛顿的网页上时,你才能看到贴