Wordpress post query php自定义字段条件
情况如下: 在wordpress中,我试图重置一个post WP_查询,以便根据post中是否存在自定义字段重写post链接。我试图在自定义字段中为帖子添加一个新链接 我在这里所做的就是完全删除链接。非常感谢您的任何帮助,我对php非常熟悉 以下是我的WP_查询:Wordpress post query php自定义字段条件,wordpress,Wordpress,情况如下: 在wordpress中,我试图重置一个post WP_查询,以便根据post中是否存在自定义字段重写post链接。我试图在自定义字段中为帖子添加一个新链接 我在这里所做的就是完全删除链接。非常感谢您的任何帮助,我对php非常熟悉 以下是我的WP_查询: <?php $recentPosts = new WP_Query(); $recentPosts->query('showposts=3'); ?>
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=3');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php
$attribute = the_title_attribute();
$title = the_title();
$key = 'NewPostLink';
$newLink = get_post_meta( $post->ID, $key, TRUE );
if ($newLink != '') {
$theLink = get_permalink ($post->ID );
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail"><a href="'.$theLink.'">'.$image.'</a></div></div>';
echo '<h2><a href="'.$theLink.'" rel="bookmark" title="Permanent Link to '.$attribute.'">'.$title.'</a></h2>';
} else {
echo '<h2><a href="'.$theLink.'" rel="bookmark" title="Permanent Link to '.$attribute.'">'.$title.'</a></h2>';
}
} else {
$theLink = $newLink;
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail"><a href="'.$theLink.'">'.$image.'</a></div></div>';
echo '<h2><a href="'.$theLink.'" rel="bookmark" title="Permanent Link to '.$attribute.'">'.$title.'</a></h2>';
} else {
echo '<h2><a href="'.$theLink.'" rel="bookmark" title="Permanent Link to '.$attribute.'">'.$title.'</a></h2>';
}
}
?>
<small><?php the_time('F jS, Y') ?></small>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
我想这就是你需要的。很难说。我假设if语句的第一部分是在没有自定义post-meta的情况下运行的语句?我不知道。这就是问题所在。如果自定义post meta返回值,if语句将运行第一部分,否则将使用空字符串作为href运行第二部分。如果自定义值不存在或不是空字符串,则运行第一部分。将if语句更改为检查它是否为空更好,因为如果它不存在,它将捕获它,并返回false,或者如果它确实存在,但是声明但未定义的空字符串,则将捕获它 我只用一行注释标记了我编辑的内容
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=3');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php
$attribute = the_title_attribute();
$title = the_title();
$key = 'NewPostLink';
$newLink = get_post_meta( $post->ID, $key, TRUE );
/* EDITED */ if (empty($newLink)) {
$theLink = get_permalink ($post->ID );
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail"><a href="'.$theLink.'">'.$image.'</a></div></div>';
echo '<h2><a href="'.$theLink.'" rel="bookmark" title="Permanent Link to '.$attribute.'">'.$title.'</a></h2>';
} else {
echo '<h2><a href="'.$theLink.'" rel="bookmark" title="Permanent Link to '.$attribute.'">'.$title.'</a></h2>';
}
} else {
$theLink = $newLink;
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail"><a href="'.$theLink.'">'.$image.'</a></div></div>';
echo '<h2><a href="'.$theLink.'" rel="bookmark" title="Permanent Link to '.$attribute.'">'.$title.'</a></h2>';
} else {
echo '<h2><a href="'.$theLink.'" rel="bookmark" title="Permanent Link to '.$attribute.'">'.$title.'</a></h2>';
}
}
?>
<small><?php the_time('F jS, Y') ?></small>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>