• May 25, 2020

    Ex1

    <?php
    $post_id = 26;
    $queried_post = get_post($post_id);
    $title = $queried_post->post_title;
    echo $title;
    echo $queried_post->post_content;
    ?>

    Ex2 ปรับ font

    <?php
    $post_id = 26;
    $queried_post = get_post($post_id);
    ?>
    <h2><?php echo $queried_post->post_title; ?></h2>
    <?php echo $queried_post->post_content; ?>

    Ex3 ใช้ Array

    <?php $thePostIdArray = array("28","74", "82", "92"); ?>
    <?php $limit = 4 ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); $counter++; ?>
    <?php if ( $counter < $limit + 1 ): ?>
    <div class="post" id="post-<?php the_ID(); ?>">
    <?php $post_id = $thePostIdArray[$counter-1]; ?>
    <?php $queried_post = get_post($post_id); ?>
    <h2><?php echo $queried_post->post_title; ?></h2>
    </div>
    <?php endif; ?>
    <?php endwhile; ?>
    <?php endif; ?>

    How to Display the Post Content Like WordPress

    When you retrieve the post content from the database you get the unfiltered content. If you want to achieve the same output like WordPress does in its’ posts or pages then you need to apply filter to the content. You can use the following code:

    <?php
    $post_id = 26;
    $queried_post = get_post($post_id);
    $content = $queried_post->post_content;
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);
    echo $content;
    ?>

    ถ้าเป็นPost/Page ที่ xxx ให้ทำอะไรบางอย่าง

    ใส่หน้า single.php ถ้า หน้าบทความที่ 16107 จะแสดง title ของหน้านั้นเพิ่ม หรือ อาจเขียนให้มัน echo อะไรก็ได้

    post

    <?php
    if (is_single("16107"))
    {
    echo $title;
    }
    ?>

    page

    <?php
    if (is_page("16107"))
    {
    echo $title;
    }
    ?>

    Query X Number of Recent Posts

    You can use the “wp_get_recent_posts” function to retrieve X number of recent posts and then display them however you want to. Here is an example:

    <?php
    //Query 5 recent published post in descending order
    $args = array( 'numberposts' => '5', 'order' => 'DESC','post_status' => 'publish' );
    $recent_posts = wp_get_recent_posts( $args );
    //Now lets do something with these posts
    foreach( $recent_posts as $recent )
    {
        echo 'Post ID: '.$recent["ID"];
        echo 'Post URL: '.get_permalink($recent["ID"]);
        echo 'Post Title: '.$recent["post_title"];
        //Do whatever else you please with this WordPress post
    }
    ?>

    Getting the URL of a post/page

    $post_id = '26';
    $post_url = get_permalink($post_id);
    ref https://www.tipsandtricks-hq.com/query-or-show-a-specific-post-in-wordpress-php-code-example-44


เวอไนน์ไอคอร์ส

ประหยัดเวลากว่า 100 เท่า!






เวอไนน์เว็บไซต์⚡️
สร้างเว็บไซต์ ดูแลเว็บไซต์

Categories


Uncategorized