php - 按 ID 获取自定义帖子类型的帖子内容
问题描述
我正在尝试按 ID 获取自定义帖子类型的特定帖子内容。
我尝试了以下解决方案和更多解决方案。充其量我似乎正在检索标题。但没有内容。
1:
<?php
$post_id = 15002;
$queried_post = get_post($post_id);
$title = $queried_post->post_title;
echo $queried_post->post_content;
?>
2:
<?php
$my_postid = 15002;
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>
3:
<?php
$ID = 15002;
$args = array('p' => $ID, 'post_type' => 'ct_template');
$loop = new WP_Query($args);
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php global $post; ?>
<?php the_content () ?>
<?php endwhile; ?>
解决方案
如果您正在使用Toolset
并希望content
通过页面 ID 获取,请在您的function.php
add_filter( 'wpv_filter_content_template_output', 'get_content_template_id', 99, 4 );
function get_content_template_id( $content, $template_selected, $id, $kind ) {
global $current_archive_template_id;
$current_archive_template_id = $template_selected; // $template_selected = current Content Template ID
return $content;
}
然后使用下面的代码来获取title
和content
$content_template_title = get_the_title($current_archive_template_id);
$content_template_content = get_the_content($current_archive_template_id);
推荐阅读
- kubernetes - kubernetes:如何从多个文件创建和使用 configmap
- node.js - 为什么 LoopBack 会缩小 JSON 配置文件的内容?
- elasticsearch - 如何在聚合中获取特定的 _source 字段
- python - .obj 到 python 中带有法线的原始顶点转换器
- python - Python:如何将标记列表添加到数据框的新列
- java - 无法创建 JVM Android Studio
- c# - vs 2017 .net核心类库项目中的引用管理器为空
- bash - 打印字符串中的字符
- asp.net-mvc - 获取操作方法的输入值。Mvc 核心和 AJAX
- regex - 有没有办法用 TextFSM 一次记录多行?