How to display custom post wordpress while showing default posts? - php

I am trying to display new custom post and the default post as well.
How to display both posts(default,my_custom_post)
<div id="primary" class="site-content">
<div id="content" role="main">
<?php wp_list_pages('title_li=');
wp_nav_menu();?>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentytwelve_content_nav( 'nav-below' );
?><?php else : ?>
<?php wp_reset_query();
query_posts('post_type=my_custom_post'); // my custom post
?>
but only single post is displaying yet.

This is how you display custom posts in wordpress.
<?php
$type = 'my_custom_post';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>

Related

Wordpress PHP loop custom post type and display on homepage

I'm currently working on a WordPress site and I've created a custom post type called 'events' using the CPT UI plugin.
I want to display the events on my home page, so I've tried to create a loop in my homepage template in the theme files. I've been using this as a guide https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-post-types-in-wordpress/
but for the life of me, I can't get the PHP that is used in that link to work for me.
<?PHP
$args = array( 'post_type' => 'events', 'posts_per_page' => 4 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
When I try to save that I get this error
syntax error, unexpected 'else' (T_ELSE)
I've been searching for an answer for this for a while and I can't find anything.
I'm pretty new to PHP, so sorry if I'm being incredibly stupid. Any help would be appreciated :)
You have not end while loop , place this code also <?php endwhile; ?>
<?php
$args = array( 'post_type' => 'events', 'posts_per_page' => 4 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Wordpress template part show only selected posts from post type

I have this template part:
<?php
while (have_posts()) : the_post();
get_template_part('template-parts/content', 'page');
do_action('flash_before_comment_template');
// If comments are open or we have at least one comment, load up the comment template.
if (comments_open() || get_comments_number()) :
comments_template();
endif;
do_action('flash_after_comment_template');
endwhile; // End of the loop.
?>
And I have a Post Type named food. I want to show only thoose posts (post with title and whole body) which contains this Post type. How should I modify the template to make it work?
Custom post type get the query
<?php
$custom_args = array(
'post_type' => 'food',
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page'=> -1
);
$get_news_query = new WP_Query( $custom_args ); ?>
<?php if ( $get_news_query->have_posts() ) : ?>
<?php while ( $get_news_query->have_posts() ) : $get_news_query->the_post(); ?>
<h2> <?php the_title(); ?> </h2>
<p><?php the_content(); ?></p>
<?php endwhile; ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;
wp_reset_query();
?>

How show list title post by tag

I want to create custom page to show list title post by tag.
Example:
This title post by tag "handphone"
Title post
Title post
Title post
...etc
Any have code for this problem?
Create a template for your page
write the postcode below into the template
<?php
$original_query = $wp_query;
$wp_query = null;
$args=array('posts_per_page'=>5, 'tag' => $brand_name);
$wp_query = new WP_Query( $args );
if ( have_posts() ) :
while (have_posts()) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
?>
after that assigne the template from admin end to your page and lets see it's wwork or not .
Create a php file in your theme directory. You can give any name. And use code something like this. Replate tag_name by your desire tag. And then create a page. Set the template. You will see the list.
<?php
/**
* Template Name: Title by Tag
*
*/
get_header();
$args=array('posts_per_page'=>5, 'tag' => 'tag_name');
$wp_query = new WP_Query( $args );
if ( have_posts() ) :
while (have_posts()) : the_post();
?>
<li>
<?php the_title(); ?>
</li>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Create page and add below code to your page
<?php
/**
* Template Name: Get title by tag
*
*/
get_header();
$tagname = 'handphone';
$wp_query = new WP_Query(array(
'post_status' => 'publish',
'post_type' => 'your-posttype', // or 'any'
'tag_slug__in' => $tagname,
'posts_per_page' => -1
));
if ( have_posts() ) :
while (have_posts()) : the_post();
the_title();
endwhile;
endif;
wp_reset_postdata();
?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

get custompost with category

I have issue with getting all posts with a category("case"), if I remove the 'cat' => $cat_id line i get all posts in posttype "Feed"
How do I get out only posts with category 'case'?
$cat_id = get_cat_ID('case');
var_dump($cat_id); //responce is 40
$args = array(
'post_type' => 'Feed',
'posts_per_page' => -1,
'cat' => $cat_id,
);
while ( $loop->have_posts() ) : $loop->the_post();
var_dump($loop);
endwhile;
wp_reset_query();
<ul>
<?php
global $post;
$args = array( 'category' => 'your category name id''offset'=> 1, );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li class="testimonial"><?php the_content(); ?></li><br/>
<?php endforeach; ?>
</ul>
<?php } ?>
for more information check https://codex.wordpress.org/Template_Tags/get_posts
Please try this code :
change your category id or post id
<?php if (is_category('3') ) { ?>
<?php query_posts('p=17'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;
} elseif (is_category('13') ) { ?>
<?php query_posts('p=11'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;
} else { ?>
//whatever goes in here
<?php } ?>

Pagination on custom Wordpress template

I have problem to change content on page when for ex. url change from
/?page_id=55&paged=1 to /?page_id=55&paged=2...
So I have problem to display next/previous page content, it just show first 4 post on page and when I click next nothing hapend with content just url change.
Here is my blog template:
<?php
/*
Template Name: Blog
*/
?>
<?php get_header(); ?>
<div class="row main-part">
<?php
$args = array(
'category_name' => 'blog',
'paged' => $paged,
'posts_per_page' => 4
);
$args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$custom_query = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
?>
<?php
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
?>
<div class="col-xs-12 col-md-3">
<h1><?php the_title(); ?></h1>
<p>
<?php the_excerpt(); ?>
</p>
<p><a class="btn btn-primary" href="<?php the_permalink() ?>" role="button">View details ยป</a></p>
</div>
<?php endwhile;endif; ?>
<?php
wp_reset_postdata();
wp_reset_query();
// Custom query loop pagination
previous_posts_link( 'Older Posts' );
next_posts_link( 'Newer Posts', $custom_query->max_num_pages );
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>
</div>
<?php get_footer(); ?>

Categories