显示带自定义字段的wordpress页面
我想在wordpress页面(page.php)上显示两篇文章,主文章和侧边栏中的次要文章。次要文章从具有帖子ID的自定义字段中检索。我使用此代码:显示带自定义字段的wordpress页面
<?php
$other_article = get_post_custom('secondaryarticle');
if ($other_article > 0) {
$show_article = get_post($other_article);
$article_content = $show_article->post_content;
echo $article_content;
}?>
每篇主要文章都有一个名为secondaryarticle的自定义字段,值是帖子ID(例如14)。代码运行时,php中没有错误消息,但没有显示文章内容。有小费吗?
您需要注册一个新的侧边栏,并相应地勾住您的自定义字段,以便它显示在边栏中。
更多来自下面的抄本链接:
http://codex.wordpress.org/Function_Reference/register_sidebar
希望它能帮助。
这个codex页面讲述了如何在一个支持widget的主题中注册你的边栏,但我没有在这里使用widgets,我只是使用一个普通的include sidebar.php文件,我从index.php或page.php调用。所以我需要知道的是为什么我发布的代码不会从我的自定义字段中打印我选择的帖子ID的帖子内容。有任何想法吗? – Hansson
我是这样做的,我没有使用任何小部件,这是我的目标。此代码是硬编码在边栏:
<div class="sidebar-article">
<?php
$secondary_article = get_post_custom('sidebararticle');
if ($secondary_article > 0) {
$show_article = get_post($secondary_article);
$article_content = $show_article->post_content;
echo $article_content;
}
?>
</div>
然后你犯了一个自定义字段上的主帖/页的文章,并称之为“sidebararticle”,在值字段后ID。
你能告诉我什么是自定义字段secondaryarticle的价值?它是帖子ID还是帖子帖子? – Pranita
对不起,我迟到了,我想我会收到一封电子邮件,当我在这里收到答案时。次粒子字段将具有作为值的帖子ID,例如, 14. – Hansson