WordPress的获取自定义的文章标题
我是新来的WordPress编程 我创建了一个名为“wp_locations” 我需要证明这个帖子 网页上的标题,我把下面的代码在我的主题指数WordPress的邮政仓库文件WordPress的获取自定义的文章标题
<?php $gallery_args = array(
'posts_per_page' => -1,
'orderby'=> 'date',
'order'=> 'DESC',
'post_type'=> 'wp_locations',
'post_status'=> 'publish',
'suppress_filters' => true
);
$posts_display_gallery = get_posts($gallery_args);
foreach($posts_display_gallery as $rows){
$post_title = $rows->post_title;
} ?>
但没有显示标题 请指导我
请尝试以下代码,并再次检查....
<?php
global $post;
$gallery_args = array(
'posts_per_page' => -1,
'orderby'=> 'date',
'order'=> 'DESC',
'post_type'=> 'wp_locations',
'post_status'=> 'publish',
'suppress_filters' => true
);
$posts_display_gallery = get_posts($gallery_args);
foreach ($posts_display_gallery as $post) : setup_postdata($post);
$post_title = $post->post_title; // Like this you will get post title.
echo $post_title; // For display
endforeach;
wp_reset_postdata();?>
希望这会对你有所帮助。
仅限代码答案是因为他们没有解释他们如何解决问题中的问题而不鼓励。请更新您的答案,以解释这是什么以及如何解决问题。请回顾[如何写出一个好的答案](https://stackoverflow.com/help/how-to-answer) – FluffyKitten
感谢评论@FluffyKitten。我已将您的评论更新为答案。谢谢。 –
谢谢大家 我忘了回声و我的问题已解决 –
请使用echo在你的代码
echo $post_title = $rows->post_title;
<?php $gallery_args = array(
'posts_per_page' => -1,
'orderby'=> 'date',
'order'=> 'DESC',
'post_type'=> 'wp_locations',
'post_status'=> 'publish',
'suppress_filters' => true
);
$posts_display_gallery = get_posts($gallery_args);
foreach($posts_display_gallery as $rows){
$post_title = $rows->post_title;
echo $post_title; // for display the title
} ?>
您可以用这段代码试试,应该工作
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$loop = new WP_Query($args);
if($loop->have_posts()){
while ($loop->have_posts()) : $loop->the_post();
global $product;
$product_id = $loop->post->ID;
$product_title = $loop->post->post_title;
endwhile;
}
您好,欢迎StackOverflow上。请参阅https://stackoverflow.com/help/how-to-ask,了解如何根据指南提出正确的问题并改进您的问题。 –
你有没有试过'echo $ post_title'? – purvik7373
缺少来自您的代码的'echo' – Arun