single page wordpress

There’s nothing stopping you using AJAX for a singlepage website that loads in other pages dynamically rather than going to a whole new page. I would advise you build the site without the AJAX and then add AJAX on top so that everything degrades gracefully should an error occur or if javascript is turned off. … Read more

Custom SQL query to get List of posts with featured image url

Worked on similar problem recently. Here is the SQL query to get the post with Featured Image. global $wpdb; $perpage = 10; $page = 1; // Get the current page FROM $wp_query $counter = $perpage * $page; $uploadDir = wp_upload_dir(); $uploadDir = $uploadDir[‘baseurl’]; $sql = ” SELECT post.ID, post.post_title, post.post_date, post.category_name, post.category_slug, post.category_id, CONCAT( ‘”.$uploadDir.”‘,”https://wordpress.stackexchange.com/”, … Read more

Pull Two Posts Into Custom Post Type `single-cpt.php`

Try this: <?php $current_id = get_the_ID(); $next_post = get_next_post(); $next_id = $next_post->ID; $cpt = get_post_type(); $cpt_array = array($current_id, $next_id); $args = array( ‘post_type’ => $cpt, ‘post__in’ => $cpt_array, ‘order_by’ => ‘post_date’, ‘order’ => ‘ASC’, ); $the_query = new WP_Query($args); if($the_query->have_posts()): while($the_query->have_posts() ): $the_query->the_post(); echo ‘<h2>’.the_title().'</h2>’; endwhile; endif; wp_reset_postdata(); ?> Tested locally and seems to work … Read more

Listing a post’s categories and subcategories

Update 1: Thanks to @birgire for suggesting a better way: wp_list_categories( [ ‘include’ => wp_list_pluck( get_the_category(), ‘term_id’ ) ] ); Try this in your single.php template: $current_cats = get_the_category(); $current_cats_ids = []; foreach ($current_cats as $cat) { $current_cats_ids[] = $cat->term_id; } wp_list_categories([ ‘include’ => $current_cats_ids, ]);

Pre get posts for single post

First of all, $query object is passed by reference, you don’t need to return $query in pre_get_posts. Second, is_post_type_archive( ‘events’ ) will work just fine, you don’t need to use query->query_vars[]. Corrected code will look like this: function my_pre_get_posts( $query ) { if( ! is_admin() && is_main_query() && is_post_type_archive( ‘events’ ) ) { $query->set(‘orderby’, ‘meta_value_num’); … Read more

Posts in multiple Categories different single.php

Instead of making it category specific you could use post formats and use different content-templates. In single.php you can write <?php get_template_part( ‘content’, get_post_format() ); ?> Then create different post formats add_theme_support( ‘post-formats’, array( ‘withpictures’, ‘withcomments’ ) ); Then create different post templates content-withpictures.php, content-withcomments.php When creating content the chosen post format will determine the … Read more

How do I create a link that will always show the latest post?

If I understand well you want to show the last post (one post) from one of the 3 post types you have, using a dynamic url like http://example.com/latest. First of all lets add a filter to ‘do_parse_request’ filter: add_filter( ‘do_parse_request’, function( $bool, WP $wp ) { $home_path = trim( parse_url( home_url(), PHP_URL_PATH ), “https://wordpress.stackexchange.com/” ); … Read more

A special single page templates for posts under a category and all its child category

You can do that with single_template filter. First you will need to check if a post belongs to a top level category. So here is the function. // custom single template for specific category function wpse_custom_category_single_template( $single_template ) { global $post; // get all categories of current post $categories = get_the_category( $post->ID ); $top_categories = … Read more

Related Posts by Multiple Tags?

I had the same idea and wrote a small little plugin to help me do this. function get_pew_related_data($args, $post_id, $related_id) { global $post, $wpdb; $post_id = intval( $post_id ); if( !$post_id && $post->ID ) { $post_id = $post->ID; } if( !$post_id ) { return false; } $defaults = array( ‘taxonomy’ => ‘topics’, ‘post_type’ => array(‘post’), … Read more