我如何从 cubeportofolio 获取永久链接,以及从帖子获取标题/缩略图?使用 wordpress 的 post_type 语法。 这是我的代码:
<?php
$posts = get_posts(array(
'posts_per_page' => 1,
'post_type' => 'cubeportfolio' ));
if( $posts ): ?>
<?php foreach( $posts as $post ): setup_postdata( $post ) ?>
<li>
<a href="<?php the_permalink(); ?>">
到这里代码就OK了。 所以我得到了很好的永久链接,但我的标题是错误的,我什至没有尝试获取缩略图。
<?php
$args = array(
'post_type'=> 'post',
'order' => 'date'
);
echo get_the_title($recent)."<br/>";
wp_reset_postdata();
?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
我已经阅读了文档中的一些内容,但我仍然无法理解。 我想要一个 ul,这样人们就可以点击帖子,但问题是,我正在用插件制作我的博客。那是错误的标题和缩略图,所以我想要 cubeportfolio 的永久链接(这是一个 post_type)。我想要帖子的标题(这是一个 post_type)
孔码:
$posts = get_posts(array(
'post_type' => 'cubeportfolio',
'order' => 'date' ));
if( $posts ): ?>
<?php foreach( $posts as $post ): setup_postdata( $post ) ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php wp_reset_postdata(); ?>
<?php
$args = array(
'post_type'=> 'post',
'order' => 'date'
);
echo get_the_title($recent)."<br/>";
wp_reset_postdata();
?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
如有任何帮助,我们将不胜感激
最佳答案
我很乐意帮助你,但我需要了解你正在尝试做什么。从上面的代码中,您将引入 1 个“cubeportfolio”,然后执行另一个查询以引入“posts”,然后输出它们的标题。如果您尝试创建指向“cubeportfolio”的链接的无序列表,则不需要第二个查询。
<?php $args = array(
'post_type' => 'cubeportfolio',
'posts_per_page' => 1,
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ) { ?>
<ul>
<?php while ( $the_query->have_posts() ) { $the_query->the_post();
<li>
<a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a>
</li>
<?php } wp_reset_postdata(); ?>
</ul>
<?php endif; ?>
这只会拉入“cubeportfolio”项目中的 1 个,因为查询设置为 1 post_per_page。希望这对您有所帮助,如果没有,请帮助我理解您的计划,我可以更新代码来帮助您。
关于php - 如何让 2 post_type 相互配合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39756196/