如何在任何页面上的任意位置打印特定的 WordPress 类别?

发布于 2024-09-07 17:50:38 字数 268 浏览 1 评论 0原文

有人可以告诉我 PHP 会是什么样子,以便从 Wordpress 获取类别,然后将其打印在我想要的地方吗?

我猜我必须构建一个 PHP 函数。类似于:

function get_a_category() {

$category = get_the_category(); <-----(不确定如何获取特定类别)

echo $category;

不知道我对 PHP 一无所知,

有人可以帮助我吗?

Can someone tell me what the PHP would look like in order to get a category from Wordpress and then print it where ever I want?

I'm guessing I have to build a PHP function. Something like:

function get_a_category() {

$category = get_the_category(); <-----( not sure how to ge ta specific category )

echo $category;

}

I have no idea I know nothing about PHP

can someone help me please ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与风相奔跑 2024-09-14 17:50:38

您不必编写自己的函数;您必须使用 The Loop (WordPress Codex) 和新查询 (函数参考/查询帖子 « WordPress Codex) 以保持原始 WP 循环不变。在页面模板中运行此新查询以获取“mycategory”中的第一篇文章。将 showposts 更改为您想要的帖子数量。

<?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
<?php endwhile; ?>

You don't have to write your own function; you do have to work with the The Loop (WordPress Codex) and a new query (Function Reference/query posts « WordPress Codex) to keep the original WP loop in place. Run this new query in a page template to get the first post from "mycategory". Change showposts to the number of posts you want.

<?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
<?php endwhile; ?>
最美不过初阳 2024-09-14 17:50:38

试试这个

function get_a_category() {
    $category = get_the_category();
    foreach ($category AS $key => $value) {
        $category_name[] = $value->cat_name;
    }
    $categories = implode(', ',$category_name);
    echo $categories;
}

Try this

function get_a_category() {
    $category = get_the_category();
    foreach ($category AS $key => $value) {
        $category_name[] = $value->cat_name;
    }
    $categories = implode(', ',$category_name);
    echo $categories;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文