如何显示自定义字段,而不是the_title
问题描述:
我有一个WordPress网站,我想显示当前类别(当前文章除外)中的所有帖子的列表。如何显示自定义字段,而不是the_title
我需要使用名为“test”而不是帖子标题的特定custom-field
的值作为锚文本。
如何编辑下面的代码以完成该操作并排除当前帖子?
<?php
global $post;
$categories = get_the_category();
foreach ($categories as $category) :?>
<ul>
<?php
$posts = get_posts('numberposts=3&category='. $category->term_id);
foreach($posts as $post) : ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a> </li>
<?php endforeach; ?>
<?php endforeach; ?>
</ul>
答
这可能会实现:
<?php
global $post;
$categories = get_the_category();
$currentID = get_the_ID();
foreach ($categories as $category) :?>
<ul>
<?php
$posts = get_posts('numberposts=3&category='. $category->term_id);
foreach($posts as $post) :
if ($currentID != $post->ID) {
?>
$test_value = get_post_meta($post->ID, 'test', true);
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php if (! empty($test_value)) {echo $test_value; } else { echo 'Nothing'; } ?>
</a>
</li><?php
} ?>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
“'是编辑的主要部分 –
GingerGirl
对不起,忽略了那部分。更新。 – danjah
该代码有PHP语法错误,并且它不会加载:( – GingerGirl