首页 文章

Wordpress帖子查询php自定义字段有条件

提问于
浏览
0

情况如下:

在wordpress中,我正在尝试重置帖子WP_Query,以便我可以根据帖子中是否存在自定义字段来重写帖子链接 . 我正试图在自定义字段中为帖子提供一个新链接 .

我在这里设法做的就是彻底杀死链接 . 非常感谢任何和所有的帮助,我对PHP非常环保 .

这是我的WP_Query:

<?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; ?>

1 回答

  • 0

    我想这就是你需要的 . 这很难说 . 我认为 if 语句的第一部分是在没有自定义帖子元素的情况下运行的内容?我无法解决问题所在 . 如果为自定义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; ?>
    

相关问题