首页 视频课程 主题开发课程第05章、文章输出 WordPress其它条件查询文章操作方法

WordPress其它条件查询文章操作方法

2023-06-11 / 257阅

以下是WordPress其它条件查询文章操作的示例代码,返回markdown格式。

根据分类查询文章

$args = array(
    'category_name' => 'news',
);
$query = new WP_Query( $args );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '- [' . get_the_title() . '](' . get_the_permalink() . ')' . "n";
    }
} 

上面的代码将查询一个名为“news”的分类,并将查询结果输出为标题和链接的markdown列表。

根据标签查询文章

$args = array(
    'tag' => 'wordpress',
);
$query = new WP_Query( $args );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '- [' . get_the_title() . '](' . get_the_permalink() . ')' . "n";
    }
} 

上面的代码将查询一个名为“wordpress”的标签,并将查询结果输出为标题和链接的markdown列表。

根据作者查询文章

$args = array(
    'author_name' => 'johndoe',
);
$query = new WP_Query( $args );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '- [' . get_the_title() . '](' . get_the_permalink() . ')' . "n";
    }
} 

上面的代码将查询一个名为“johndoe”的作者,并将查询结果输出为标题和链接的markdown列表。

根据发布日期查询文章

$args = array(
    'year' => '2021',
    'monthnum' => '5',
);
$query = new WP_Query( $args );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '- [' . get_the_title() . '](' . get_the_permalink() . ')' . "n";
    }
} 

上面的代码将查询2021年5月发布的文章,并将查询结果输出为标题和链接的markdown列表。

根据标题查询文章

$args = array(
    's' => 'wordpress',
);
$query = new WP_Query( $args );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '- [' . get_the_title() . '](' . get_the_permalink() . ')' . "n";
    }
} 

上面的代码将查询标题中包含“wordpress”的文章,并将查询结果输出为标题和链接的markdown列表。

阅读文章或者观看视频过程中有任何问题,请下方留言或者联系我Q248758228

大家谈论
    我的见解
    目录