Below, you’ll find a detailed guide on how to add the Posts for Page Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Posts for Page Plugin shortcode not to show or not to work correctly.
Before starting, here is an overview of the Posts for Page Plugin and the shortcodes it provides:
"Posts for Page is a WordPress plugin designed to effortlessly display your blog posts on any page. With its user-friendly interface, it simplifies content management. Customize your website with ease."
- [posts-for-page]
Posts for Page [posts-for-page] Shortcode
The Posts-for-Page shortcode is a versatile tool that allows you to customize the display of posts on your WordPress pages. You can define the length of posts, add a ‘Read More’ text, and customize navigation with ‘Next’ and ‘Previous’ text. It also supports sorting by categories, tags, authors, and more. You can choose to show full posts or excerpts, hide images, and even strip HTML from the content. The shortcode ensures that your posts are displayed exactly how you want them.
Shortcode: [posts-for-page]
Parameters
Here is a list of all possible posts-for-page shortcode parameters and attributes:
length
– Defines the length of the post excerptread_more
– Custom text for the ‘read more’ linkprev_text
– Custom text for the previous page linknext_text
– Custom text for the next page linkcat_slug
– Specifies a category using its slugcat
– Specifies a category using its IDtag_slug
– Specifies a tag using its slugorder_by
– Determines the order of the postspost_id
– Specifies a specific post using its IDauthor
– Specifies posts from a certain authornum
– Number of posts to showshow_full_posts
– Determines if full posts are displayedhide_images
– Option to hide images in the postsuse_wp_excerpt
– Option to use the WordPress excerpt functionstrip_html
– Option to remove HTML from the post contenthide_post_content
– Option to hide the post contentshow_meta
– Option to show the post meta datahide_post_title
– Option to hide the post titlehide_read_more
– Option to hide the ‘read more’ linkcreate_para_tags
– Option to create paragraph tags in the post contentorder
– Specifies the order direction of the postsforce_excerpt_image
– Forces an image in the excerptforce_image_height
– Forces a specific image heightforce_image_width
– Forces a specific image width
Examples and Usage
Basic example – Display posts from a specific category by referencing the category slug.
[posts-for-page cat_slug="news" /]
Advanced examples
Display posts from a specific category, limit the number of posts displayed, and customize the “Read More” text.
[posts-for-page cat_slug="news" num="5" read_more="Continue Reading..." /]
Display posts from a specific author, hide the post content, and customize the previous and next text.
[posts-for-page author="John" hide_post_content="true" prev_text="Previous Posts" next_text="Next Posts" /]
Display a specific post by ID, show the full post content, and hide the post title.
[posts-for-page post_id="123" show_full_posts="true" hide_post_title="true" /]
These examples demonstrate the flexibility of the shortcode and how it can be used to customize the display of posts on your WordPress site.
PHP Function Code
In case you have difficulties debugging what causing issues with [posts-for-page]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode("posts-for-page", "sc_posts_for_page");
Shortcode PHP function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | function sc_posts_for_page( $atts , $content = null){ if ( $atts [ 'length' ] > 0 ){ } else { $atts [ 'length' ] = 50; } //echo $atts['read_more']; if ( $atts [ 'read_more' ] != "" ){ $_opts [ 'readMoreText' ] = $atts [ 'read_more' ]; } else { $_opts [ 'readMoreText' ] = "Read More »" ; } if ( $atts [ 'prev_text' ] != "" ){ $_opts [ 'prevText' ] = $atts [ 'prev_text' ]; } else { $_opts [ 'prevText' ] = "« Newer Entries" ; } if ( $atts [ 'next_text' ] != "" ){ $_opts [ 'nextText' ] = $atts [ 'next_text' ]; } else { $_opts [ 'nextText' ] = "Older Entries » " ; } // set values from the shortcode $_opts [ 'cat_slug' ] = $atts [ 'cat_slug' ]; $_opts [ 'cat' ] = $atts [ 'cat' ]; $_opts [ 'tag_slug' ] = $atts [ 'tag_slug' ]; $_opts [ 'order_by' ] = $atts [ 'order_by' ]; $_opts [ 'post_id' ] = $atts [ 'post_id' ]; $_opts [ 'author' ] = $atts [ 'author' ]; $_opts [ 'num' ] = $atts [ 'num' ]; $_opts [ 'showFullPost' ] = $atts [ 'show_full_posts' ]; global $paged ; if ( empty ( $paged )) { $_opts [ 'cur_page' ] = 1; } else { $_opts [ 'cur_page' ] = $paged ; } $_opts [ 'hide_images' ] = $atts [ 'hide_images' ]; $_opts [ 'use_wp_excerpt' ] = $atts [ 'use_wp_excerpt' ]; $_opts [ 'strip_html' ] = $atts [ 'strip_html' ]; $_opts [ 'hide_post_content' ] = $atts [ 'hide_post_content' ]; $_opts [ 'show_meta' ] = $atts [ 'show_meta' ]; $_opts [ 'hide_post_title' ] = $atts [ 'hide_post_title' ]; $_opts [ 'hide_read_more' ] = $atts [ 'hide_read_more' ]; $_opts [ 'create_para_tags' ] = $atts [ 'create_para_tags' ]; $_opts [ 'order' ] = $atts [ 'order' ]; $_opts [ 'force_excerpt_image' ] = $atts [ 'force_excerpt_image' ]; $_opts [ 'force_image_height' ] = $atts [ 'force_image_height' ]; $_opts [ 'force_image_width' ] = $atts [ 'force_image_width' ]; //get the id of the current article that is calling the shortcode $parent_id = get_the_ID(); $output = "" ; $i = 0; $children = pfp_get_posts( $_opts ); if ( is_array ( $children )) { if ( count ( $children ) <= 0){ $out = "<strong>Items for Page:</strong> There are no posts that match the selection criteria." ; return $out ; } foreach ( $children as $child ) { $title = $child ->post_title; $link = get_permalink( $child ->ID); $content ; $date ; //$thumb = get_the_post_thumbnail($child->ID,array(60, 60, true)); $imageSrc = "" ; $image_html = "" ; $excerpt_images = "" ; if ( $_opts [ 'create_para_tags' ] == "true" ) { //$content = wpautop($child->post_content); $content = wpautop(apply_filters( 'the_content' , $child ->post_content)); //echo "wpautop"; } else { //if($_opts['showFullPost'] == "true") //{ //$content = apply_filters('the_content', $child->post_content); //} //else //{ //$content = $child->post_content; //} $content = apply_filters( 'the_content' , $child ->post_content); //echo "no wpautop"; } if ( $_opts [ 'showFullPost' ] != "true" ) { if ( $_opts [ 'hide_images' ] != 'true' ) { $args = array ( 'numberposts' => 1, 'order' => 'ASC' , 'post_mime_type' => 'image' , 'post_parent' => $child ->ID , 'post_status' => null, 'post_type' => 'attachment' ); //get the first image and resize it $attachments = get_children( $args ); if ( $attachments ) { foreach ( $attachments as $attachment ) { $image_attributes = wp_get_attachment_image_src( $attachment ->ID, 'thumbnail' ) ? wp_get_attachment_image_src( $attachment ->ID, 'thumbnail' ) : wp_get_attachment_image_src( $attachment ->ID, 'full' ); $imageSrc = '<img src="' .wp_get_attachment_thumb_url( $attachment ->ID ). '" />' ; $imageSrc = pfp_resize_image( $imageSrc , $_opts [ 'force_image_height' ], $_opts [ 'force_image_width' ]); } // if attachment found need to remove the image from the post content **TODO - this is odd behaviour but works $content = remove_images( $content ); } else // maybe an image in the post content itself - so resize if necessary { $match = preg_match( '/<img[^>]+./' , $content , $excerpt_images ); // got array of any images found now get just the src value so we can use it when forcing the image size if (! empty ( $excerpt_images [0])) { $imageSrc = pfp_resize_image( $excerpt_images [0], $_opts [ 'force_image_height' ], $_opts [ 'force_image_width' ]); $content = remove_images( $content ); } } } if ( $_opts [ 'use_wp_excerpt' ] == 'true' ) { // use wp excerpt function //$post = get_post($post_id); //if($_opts['force_excerpt_image'] == 'true') //{ // //echo "imageSrc: " . $child->ID . $imageSrc . "<br>"; // // if one not already added as thumbnail get it from post content itself // if(empty($imageSrc)) // { // $match = preg_match('/<img[^>]+./', $content, $excerpt_images); // // got array of any images found now get just the src value so we can use it when forcing the image size // if(!empty($excerpt_images[0])) // { // $image_html = pfp_resize_image($excerpt_images[0], $_opts['force_image_height'], $_opts['force_image_width']); // } // } //} setup_postdata( $child ); $content = robins_get_the_excerpt( $child ->ID); if (! empty ( $image_html )) { $content = $image_html . $content ; } } else { // remove all images from the post if ( $_opts [ 'hide_images' ] == 'true' ) { $content = remove_images( $content ); } else { $content = ( $content ); } if ( $_opts [ 'strip_html' ] == 'true' ) { $content = strip_tags ( $content ); } //split excerpt into array for processing $words = explode ( ' ' , $content ); //chop off the excerpt based on the atts->lenth $words = array_slice ( $words , 0, $atts [ 'length' ]); //merge the array of words for the excerpt back into sentances $content = implode( ' ' , $words ) . "..." ; } } // output post $output .= "<div class='pfpItem entry-content'>" ; if ( $_opts [ 'hide_post_title' ] != 'true' ) { $output .= "<h2 class='entry-title'><a href='$link'>$title</a></h2>" ; } if ( $_opts [ 'show_meta' ] == 'true' ) { $userid = $child ->post_author; $userdata = get_userdata( $userid ); $output .= "<div class='entry-meta'>" ; $output .= "Posted on " . mysql2date( 'F j, Y' , $child ->post_date) . " by " . $userdata ->user_nicename; $output .= "</div>" ; } // do not get any content if it is to be hidden (i.e. show titles only) if ( $_opts [ 'hide_post_content' ] != 'true' ) { if ( ( $_opts [ 'hide_images' ] != 'true' ) && ( $_opts [ 'showFullPost' ] != "true" )) { $output .= $imageSrc ; } $output .= $content ; if ( ( $_opts [ 'hide_read_more' ] != 'true' ) && ( $_opts [ 'showFullPost' ] != "true" )) { $output .= "<a href='$link' class='pfpReadMore'>" . $_opts [ 'readMoreText' ] . "</a>" ; } } $output .= "<div class='clear'></div>" ; $output .= "<hr>" ; $output .= "</div>" ; } } global $wp_query ; $page_links_total = $wp_query ->max_num_pages; if ( $_opts [ 'cur_page' ] > 1) { // show prev $output .= "<span class='pfpNav'>" . get_previous_posts_link( $_opts [ 'prevText' ]) . "</span>" ; } if ( $_opts [ 'cur_page' ] < $page_links_total ) { // show next $output .= "<span class='pfpNav'>" . get_next_posts_link( $_opts [ 'nextText' ]) . "</span>" ; } wp_reset_query(); return $output ; } |
Code file location:
posts-for-page/posts-for-page/posts-for-page.php
Conclusion
Now that you’ve learned how to embed the Posts for Page Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply