WordPress的 - 添加类别index.php
问题描述:
我有一个类别设置为“一周的歌”。在我的网站的桌面版本上,它以侧边栏的形式出现在窗口小部件中。WordPress的 - 添加类别index.php
但是,我试图在移动网站上显示它。之前,我的主题制作人员让我将代码添加到index.php
以将搜索栏添加到我的移动网站。
到目前为止,我已经尝试通过使用“小部件短代码”插件添加类别。
我试着添加以下到我的index.php:
<?php
echo do_shortcode(widget id="rpwe_widget-6");
?>
但是,当我这样做,我得到一个500错误。
在index.php
上添加一个类别(1篇文章)的正确PHP代码是什么?
我知道这种短代码方法不是正确的方法,但我不确定还有什么要做。
答
您可以通过使用循环,给它一个参数,寻找获得的职位。在你的情况下,你需要找出你想要显示的类别的ID。为此,请转到您的类别概览并将鼠标悬停在您想要显示的类别上。
然后你会看到在浏览器的底部,像一个链接:
http://www.yourdomain.com/wp-admin/term.php?taxonomy=category&tag_ID=1&post_type=post...
the part with category&tag_id=X
X是该类别的ID,你想展示。然后在你的面前,page.php文件,home.php或index.php的,你可以用下面的代码查询数据库(所以是的,把在你的模板文件):
<?php
// Feed PHP with the information you want to show, in our case: a certain category = use of the id, number = amount of posts to show
$catquery = new WP_Query('cat=3&posts_per_page=1');
// Let WordPress run the loop for you
while($catquery->have_posts()) : $catquery->the_post();
?>
<!--Inside the loop, you can use the WP template tags to show the stuff you want, like author, exerpt of the post etc. -->
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<p><?php the_excerpt(); ?></p>
<?php endwhile; ?>
<?php // Don't forget to reset the query (clean the data after it is finished) ?>
<?php wp_reset_query(); ?>
为了帮助你解决问题:查找类别ID,在圆括号内输入数字它说cat = 3(3是一个示例类别)。然后将代码复制到你想要的地方的主题中,并看看你得到了什么:)
希望你的结果。
这一点查询会给你:
后的链接标题和摘录(后短预览)。 欲了解更多标签/信息展示,请访问WordPress Codex或只是做一个像这样的Google搜索:http://blog.teamtreehouse.com/wordpress-loop-beginners-guide
感谢您的帮助,user2521387!为了让我暂时得到我,我使用了Fencer04发布的更正的简码方法。我将在本周晚些时候测试此方法,因为这是我需要运行的少一个插件! – scr3w
此方法有效!我有一个后续问题,我的理解可能需要一个新的职位。当我使用简码/小部件方法时,在我的css中,我能够做到:'} .rpwe-title> a { color:#fff; font-family:“league_gothicregular”!important; font-size:23px; } .rpwe-title { padding-bottom:10px!important; padding-top:10px!important; } .rpwe_widget-6 { margin-bottom:8px; } }'在我的CSS中格式化。我将如何格式化此代码在我的CSS? – scr3w