wordpress如何获取当前文章的id,要求写一个函数在function里面调用

2025-04-01 09:04:03
推荐回答(1个)
回答1:

    //The args
    $args = array(
        'cat'   => 61 //这是分类ID,也可以用array给一组ID
    );
 
    // The Result
    $naruco= new WP_Query( $args );
 
    if ( $naruco-> have_posts() ) {
 
        // The Loop
        while ( $naruco-> have_posts() ) : $naruco->the_post();
            echo '
  • ';
                $post_ID = get_the_ID(); //这就是文章的ID了。
                $post_content = get_the_content();  //文章内容,至于怎么截取一定长度的字数,百度一下到处都是啦。
                echo '
  • ';
            endwhile;
     
        }else {
            echo 'no posts in current category!';
        }
    ?>

     

    说的全在注释里了。