<!--excerpt--> <?php the_excerpt(); ?> <!--content--> <?php the_content( 'Read more ...' ); ?> <!--content--> <?php if ( is_category() || is_archive() ) { the_excerpt(); } else { the_content(); } ?>
functions.php
// Changing excerpt length function new_excerpt_length($length) { return 100; } add_filter('excerpt_length', 'new_excerpt_length'); // Changing excerpt more function new_excerpt_more($more) { return '...'; } add_filter('excerpt_more', 'new_excerpt_more');
content + กำหนดจำนวนคำ
<!--content + ... --> <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 400, '...');?>
This will cut the string at 400 characters and close it with ...
. Just add a “read more”-link to the end by pointing to the permalink with get_permalink()
.
<a href="<?php the_permalink() ?>">Read more </a>
Of course you could also build the read more
in the first line. Than just replace '...'
with '<a href="' . get_permalink() . '">[Read more]</a>'
รวมกันเป็นดังนี้
<!--content + ... -->
<?php $content = get_the_content(); echo mb_strimwidth($content, 0, 400, '<a href="' . get_permalink() . '">[Read more]</a>'
);?>
(url นี้มีหลายวิธี)
https://stackoverflow.com/questions/3147898/how-to-set-character-limit-on-the-content-and-the-excerpt-in-wordpress
ดึงบทความเฉพาะ post
<?php $post_id = 26; $queried_post = get_post($post_id); $title = $queried_post->post_title; echo $title; echo $queried_post->post_content; ?> หรือ <?php $post_id = 26; $queried_post = get_post($post_id); ?> <h2><?php echo $queried_post->post_title; ?></h2> <?php echo $queried_post->post_content; ?>
ดึงหลายบทความ ระบุโพสต์
<?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(']]>', ']]>', $content);
echo $content;
?>
For a range of all the returned fields that you can use, check the WordPress site here.
Find out if we are in a particular WordPress post
Lets say you want to apply some custom tweak when a particular post is being viewed. You will need to programmatically determine when you are in this specific post (example: Post ID 2). The following snippet of code will be helpful for this:
if (is_single("2"))
{
//Do your custom tweak for post whose ID is 2
}
You can do the same thing for pages too (5 is the page ID in this example):
if (is_page("5"))
{
//Do your custom tweak for post whose ID is 2
}
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 } ?>
Using a Plugin to List all Posts Alphabetically
You can also use the WP Alphabetic Listing WordPress plugin to list all your posts.
Getting the URL of a post/page
The post/page object that you retrieve using the get_post() function doesn’t actually have the URL of the post. It does have a guid file which contains the URL but that one is not reliable. Use the following function to get the URL value of the post/page:
$post_id = '26'; $post_url = get_permalink($post_id);