标签: 随机调用

  • wordpress按分类ID调用最新、推荐、随机内容

    在WordPress中,可以通过自定义查询(WP_Query)来按分类ID调用最新、推荐(自定义字段或标签)、随机内容。以下是一些示例代码,帮助你实现这些功能。

    1. 按分类ID调用最新内容

    以下代码可以调用指定分类ID下的最新文章:

    <?php
    // 设置分类ID和文章数量
    $category_id = 1; // 替换为你的分类ID
    $posts_per_page = 5; // 显示的文章数量
    
    $args = array(
        'post_type' => 'post', // 文章类型
        'posts_per_page' => $posts_per_page, // 每页显示的文章数量
        'orderby' => 'date', // 按日期排序
        'order' => 'DESC', // 降序排列(最新文章在前)
        'cat' => $category_id // 指定分类ID
    );
    
    $query = new WP_Query($args);
    
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            ?>
            <div class="post-item">
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <p><?php the_excerpt(); ?></p>
            </div>
            <?php
        }
    } else {
        echo '<p>没有找到文章。</p>';
    }
    
    wp_reset_postdata(); // 重置查询
    ?>

    2. 按分类ID调用推荐内容

    推荐内容可以通过自定义字段或标签实现。假设你使用自定义字段is_recommended来标记推荐文章(值为true或1):

    <?php
    $category_id = 1; // 替换为你的分类ID
    $posts_per_page = 5; // 显示的文章数量
    
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => $posts_per_page,
        'orderby' => 'date', // 按日期排序
        'order' => 'DESC', // 降序排列
        'cat' => $category_id,
        'meta_query' => array( // 自定义字段查询
            array(
                'key' => 'is_recommended', // 自定义字段名称
                'value' => 'true', // 自定义字段值
                'compare' => '='
            )
        )
    );
    
    $query = new WP_Query($args);
    
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            ?>
            <div class="post-item">
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <p><?php the_excerpt(); ?></p>
            </div>
            <?php
        }
    } else {
        echo '<p>没有找到推荐文章。</p>';
    }
    
    wp_reset_postdata(); // 重置查询
    ?>

    调用随机内容

    以下代码可以调用指定分类ID下的随机文章:

    <?php
    $category_id = 1; // 替换为你的分类ID
    $posts_per_page = 5; // 显示的文章数量
    
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => $posts_per_page,
        'orderby' => 'rand', // 随机排序
        'cat' => $category_id
    );
    
    $query = new WP_Query($args);
    
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            ?>
            <div class="post-item">
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <p><?php the_excerpt(); ?></p>
            </div>
            <?php
        }
    } else {
        echo '<p>没有找到文章。</p>';
    }
    
    wp_reset_postdata(); // 重置查询
    ?>
  • wodpress调用当前文章同分类下相同tag的10篇文章

    要在WordPress中随机调用当前文章同分类下的相同标签的10篇文章,你可以使用以下代码。这段代码将会获取当前文章的分类和标签,然后查询同分类下具有相同标签的10篇随机文章。

    <?php
    // 获取当前文章的ID
    $current_post_id = get_the_ID();
    
    // 获取当前文章的分类
    $current_post_categories = wp_get_post_categories($current_post_id);
    
    // 获取当前文章的标签
    $current_post_tags = wp_get_post_tags($current_post_id);
    
    // 如果有分类和标签
    if (!empty($current_post_categories) && !empty($current_post_tags)) {
        // 随机化标签数组
        shuffle($current_post_tags);
    
        // 取第一个标签作为查询的基础
        $tag_id = $current_post_tags[0]->term_id;
    
        // 构建查询参数
        $args = array(
            'posts_per_page' => 10,
            'category' => $current_post_categories[0], // 使用第一个分类
            'tag_id' => $tag_id,
            'post__not_in' => array($current_post_id), // 排除当前文章
            'orderby' => 'rand', // 随机排序
        );
    
        // 查询文章
        $related_posts = new WP_Query($args);
    
        // 如果有找到相关文章
        if ($related_posts->have_posts()) {
            echo '<ul>';
            while ($related_posts->have_posts()) {
                $related_posts->the_post();
                echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
            }
            echo '</ul>';
        } else {
            echo '没有找到相关文章。';
        }
    
        // 重置文章数据
        wp_reset_postdata();
    } else {
        echo '当前文章没有分类或标签。';
    }
    ?>

    这段代码首先获取当前文章的ID,然后获取其分类和标签。接着,它构建一个查询参数数组,其中包含了限制文章数量、指定分类、指定标签ID、排除当前文章以及随机排序的设置。使用`WP_Query`执行这个查询,最后循环输出查询到的文章标题和链接。

    请注意,这段代码假设当前文章至少有一个分类和一个标签。如果当前文章没有分类或标签,代码将输出相应的信息。此外,代码中使用了`shuffle`函数来随机化标签数组,并使用数组的第一个元素作为查询的基础,这是因为我们只需要一个标签来找到相关文章。如果你想要基于所有标签进行查询,你需要修改查询参数以包含所有标签。