【问题标题】:Wordpress post query by author作者的Wordpress帖子查询
【发布时间】:2013-07-05 02:18:22
【问题描述】:

我想要页面中作者的帖子列表。我使用的作者帖子在每个页面中都是动态的。我使用了下面的代码。

<ul>
<?php
global $post;
$args = array( 'posts_per_page' => 3, 'post_type'=> 'post', 'author_name' => 'ppm' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>

    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

<?php endforeach; ?>
</ul>

请查看 author_name 数组,我想要 ppm 用户的帖子显示。现在我要做动态。我想从页面的自定义字段中获取作者姓名。喜欢

<?php $author_name = get_post_meta($post->ID, 'author_name', true); ?>
<ul>
<?php
global $post;
$args = array( 'posts_per_page' => 3, 'post_type'=> 'post', 'author_name' => '$author_name' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>

    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

<?php endforeach; ?>
</ul>

但是,代码不起作用。如何从页面的自定义字段中获取作者姓名?任何人都可以帮忙吗?

抱歉英语不好。

【问题讨论】:

  • 你无法在循环之外获得$post-&gt;ID
  • 我在循环里面试过了,还是不行。
  • 您的帖子使用自定义元添加作者姓名?并且您可以使用自定义元用户滚动显示帖子?
  • 我有一个作者简介页面。我多次创建该页面。我只希望数组中的作者姓名将从页面的自定义字段中下拉。有可能吗?

标签: wordpress


【解决方案1】:

这对我有用。感谢 Sarim Khan 提供的解决方案。

<ul>
<?php
global $post;
$author_name = get_post_meta($post->ID, 'author_name', true);
$args = array( 'posts_per_page' => 3, 'post_type'=> 'post', 'author_name' => $author_name );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>

    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

<?php endforeach; ?>
</ul>

【讨论】:

    【解决方案2】:
    <?php while (have_posts() ) : the_post(); ?>
    <?php $author_name = get_post_meta($post->ID, 'author_name', true); ?>
    <?php endwhile; ?>
    
    
    <?php
            $args = array( 'posts_per_page' => 3, 'post_type'=> 'post','meta_key'=>'author_name','meta_value'=>$author_name);
            query_posts($args);  
    ?>
    
    <ul>
    <?php while (have_posts() ) : the_post(); ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>
    

    【讨论】:

      猜你喜欢
      • 2023-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多