建议使PHP ID具体而不是最近的帖子

时间:2016-06-30 10:26:33

标签: php wordpress

我一直在处理php文件(在wordpress自定义主题上,不会在任何时候更新),以使主页page.php拉入三个帖子。它最初是由另一个开发人员设置的,它让它与另一个dbase一起工作,所以我试图让我插入的帖子适合现有的php。任何人都可以通过建议我可以更改代码以适应特定的帖子ID而不是recnet帖子来帮助我吗?或者如果你知道任何可以帮助我的教程,因为我找不到任何东西。

代码

     <?php
$args = array(
    'numberposts' => 1,
    'orderby' => 'date',
    'order' => 'desc',
    'post_type' => 'post',
    'post_status' => 'publish',
    // 'post_parent' => '90',
    'suppress_filters' => true
);

$recent_posts = wp_get_recent_posts($args, $output = ARRAY_A);
foreach ($recent_posts as $recent) {
?>

                        <div class="col-md-4" style="margin-bottom: 20px;">
                            <a href="<?php
    echo get_permalink($recent['ID']);
?><?php
    echo get_permalink($post->ID);
?>" title="<?php
    echo esc_attr($recent['post_title']);
?>" >

                                <div class="feature-item">

                                    <?php
    if (has_post_thumbnail($recent["ID"])) {
?>
                                       <?php
        $image = wp_get_attachment_image_src(get_post_thumbnail_id($recent["ID"]), 'single-post-thumbnail');
?>                    
                                        <img src='<?php
        echo $image[0];
?>' style='width: 100%; border-radius: 6px'>
                                    <?php
    }
?>

                                    <div class='feature-title'><?php
    echo $recent['post_title'];
?></div>
                                    <div class='feature-content'><?php
    echo substr($recent['post_content'], 0, 80);
?></div>              
                                </div>
                            </a>
                        </div>

                        <?php
}
?> 

我试过简单地改变args:

$args = array(
                    'numberposts' => 1, 
                    'post_type' => 'page',
                    'post_status' => 'publish',
                    'ID' => '268');

但是实现$ recent_posts命令会抛出ID函数。

任何帮助都非常感激。

1 个答案:

答案 0 :(得分:0)

这是get_post()的一个很好的用例:

$post_268 = get_post(268);
// Do something with the $post_268. Example:
echo $post_268->post_title;

当然,您还需要更改其余代码以考虑此新变量。