doulu5109 2016-06-30 10:26
浏览 38

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

I have been working with the php file (on a wordpress custom theme which won't be updated at any point) to make the home page.php pull in three posts. It was initially set up by another developer who had it working with another dbase so I am trying to make the posts I have inserted fit with the existing php. Could anyone help me out by suggesting what I can put to change the code to fit specific post ID rather than recnet posts? Or if you know of any tutorials which would help me as I cannot find anything.

Code

     <?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
}
?> 

I have tried simply changing the args to have:

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

but realise the $recent_posts commands throw out the ID function.

Any help massively appreciated.

  • 写回答

1条回答 默认 最新

  • dougao9864 2016-06-30 10:43
    关注

    This is a good use case for get_post():

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

    Of course, you're also going to need to change the rest of the code to account for this new variable.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。