Search
 
SCRIPT & CODE EXAMPLE
 

PHP

Add Recent Posts by Category Using PHP

// Output this shortcode on your template file or page editor

<?php echo do_shortcode('[categoryposts]'); ?>
[categoryposts] or [wpb_postsbycategory]


function wpb_postsbycategory() {
// the query
$the_query = new WP_Query( array( 
    'category_name' => 'travel', 
    'posts_per_page' => 5 
) ); 
   
// The Loop
if ( $the_query->have_posts() ) {
    $string .= '<ul class="postsbycategory widget_recent_entries">';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
            if ( has_post_thumbnail() ) {
            $string .= '<li>';
            $string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() .'</a></li>';
            } else { 
            // if no featured image is found
            $string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';
            }
            }
    } else {
    // no posts found
 $string .= '<li>No Posts Found</li>';
}
$string .= '</ul>';
   
return $string;
   
/* Restore original Post Data */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('categoryposts', 'wpb_postsbycategory');
Comment

PREVIOUS NEXT
Code Example
Php :: mamp change php version 
Php :: laravel creating_table_name 
Php :: Available excel column formatting 
Php :: fuzzy search in php with percentage 
Php :: laravel ignition dark mode 
Php :: caculator 
Php :: wp-config.php location 
Php :: how to convert array into json php 
Php :: php edit user profile 
Php :: Required parameter follows optional parameter (500 Internal Server Error) php 
Php :: php if form fails keep data 
Php :: HTTP 500 ERROR WITHOUT MESSAGE PHP 
Php :: page preview changes in wordpress in custom post type 
Php :: php unsupported file types 
Php :: Wordpress Scheduled Post to be viewable on front end 
Php :: Save custom input field value into cart item 
Php :: mkdir recursive php 
Php :: wp php blog info background image 
Php :: how to get file name in upload images in php 
Php :: laravel eloquent query with orderBy subquery 
Php :: change php variable value in javascript 
Php :: An expression was expected phpmyadmin 
Php :: OR criteria 
Php :: amazon ami 2 php ini 
Php :: teaching php in interactive mode 
Php :: File: C:xampphtdocsmarvellogistikapplicationlibrariesProfiler.php Line: 386 Function: sendDataInHeaders 
Php :: SQLSTATE[HY000]: General errorstring(58) 
Php :: 0 == "string" php 
Php :: How to clear previously echoed items in PHP 
Php :: HASHING in php double scripting 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =