首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

初学者:如何将固定链接添加到WP_Query多个自定义帖子类型?

答:要将固定链接添加到WP_Query多个自定义帖子类型,你可以按照以下步骤进行操作:

  1. 创建自定义帖子类型:首先,你需要使用register_post_type函数创建自定义帖子类型。你可以在functions.php文件中添加以下代码:
代码语言:txt
复制
function create_custom_post_types() {
    register_post_type('custom_type1',
        array(
            'labels' => array(
                'name' => 'Custom Type 1',
                'singular_name' => 'Custom Type 1'
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'custom-type-1'),
        )
    );

    register_post_type('custom_type2',
        array(
            'labels' => array(
                'name' => 'Custom Type 2',
                'singular_name' => 'Custom Type 2'
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'custom-type-2'),
        )
    );
}

add_action('init', 'create_custom_post_types');

上述代码创建了两个自定义帖子类型:custom_type1和custom_type2。你可以根据需要添加更多的自定义帖子类型。

  1. 更新固定链接结构:在WordPress后台,导航到“设置”->“固定链接”。选择一个你喜欢的固定链接结构,例如“帖子名称”,然后点击“保存更改”。
  2. 添加固定链接到WP_Query:现在,你可以使用WP_Query来获取多个自定义帖子类型,并将固定链接添加到查询结果中。以下是一个示例代码:
代码语言:txt
复制
$args = array(
    'post_type' => array('custom_type1', 'custom_type2'),
    'posts_per_page' => 10
);

$query = new WP_Query($args);

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a><br>';
    }
} else {
    echo '没有找到帖子。';
}

wp_reset_postdata();

上述代码中,我们使用'post_type'参数指定了要查询的自定义帖子类型。你可以根据需要添加更多的自定义帖子类型。然后,我们使用get_permalink函数获取每个帖子的固定链接,并将其添加到输出中。

这样,你就可以将固定链接添加到WP_Query多个自定义帖子类型了。

腾讯云相关产品和产品介绍链接地址:

  • 腾讯云服务器(CVM):https://cloud.tencent.com/product/cvm
  • 腾讯云对象存储(COS):https://cloud.tencent.com/product/cos
  • 腾讯云数据库(TencentDB):https://cloud.tencent.com/product/cdb
  • 腾讯云人工智能(AI):https://cloud.tencent.com/product/ai
  • 腾讯云物联网(IoT):https://cloud.tencent.com/product/iot
  • 腾讯云区块链(Blockchain):https://cloud.tencent.com/product/baas
  • 腾讯云元宇宙(Metaverse):https://cloud.tencent.com/product/metaverse

请注意,以上链接仅供参考,具体产品选择应根据实际需求进行评估。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的结果

领券