<?php
$args = array( 'numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<h1>' .$recent["post_title"].' </h1> ';
}
?>
I added this code for getting just single latest post on desired page.
How can I add next and previous button of posts to it?
If you want to get previous post link, You can do it using following code:
<?php $prev_post = get_adjacent_post( false, '', true ); ?>
<?php if ( is_a( $prev_post, 'WP_Post' ) ) { ?>
<?php echo get_the_title( $prev_post->ID ); ?>
<?php } ?>
If you want to get next post link, Than:
<?php $next_post = get_adjacent_post( false, '', false ); ?>
<?php if ( is_a( $next_post, 'WP_Post' ) ) { ?>
<?php echo get_the_title( $next_post->ID ); ?>
<?php } ?>
You can check more information about this function here : get_adjacent_post()
$previous=$recent["ID"]-1;
echo '' . PREVIOUS.' ';
$next=$recent["ID"]+1;
echo '' . NEXT.' ';
I did this and it worked :)
Related
I have created a live search field that fetches the title of the product using AJAX.
It works correctly, however, I would like to fetch the product thumbnail and the ‘Add to the cart’ button.
**Edit: I have added the output for the thumbnail and add to cart button. It works, but it only displays in singular rows. How can I update my output in rows of 4 as below?
Front End Code
<input type="text" name="keyword" id="keyword" onkeyup="fetch()">
<div id="datafetch">Your numbers will show here</div>
<script>
function fetch(){
$.post('<?php echo admin_url('admin-ajax.php'); ?>',{'action':'my_action'},
function(response){
$('#datafetch').append(response);
console.log(result);
});
}
</script>
Code in Functions.php
<?php
}// LOTTERY start the ajax function
add_action('wp_ajax_data_fetch' , 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch','data_fetch');
function data_fetch(){
$the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'product' ) );
if( $the_query->have_posts() ) :
while( $the_query->have_posts() ): $the_query->the_post();
global $product;
$product = get_product( get_the_ID() ); //set the global product object
$myquery = esc_attr( $_POST['keyword'] );
$a = $myquery;
$search = get_the_title();
if( stripos("/{$search}/", $a) !== false) {?>
<h4><?php the_title();?></h4>
<h4><?php the_post_thumbnail();?></h4>
<p><?php echo $product->get_price_html(); ?></p>
<?php woocommerce_template_loop_add_to_cart(); //ouptput the woocommerce loop add to cart button ?>
<?php
}
endwhile;
wp_reset_postdata();
endif;
die();
}
You can use this get_the_post_thumbnail_url(get_the_ID()) to get thumbnail URL and add image.
You can use this https://yourdomain.com/?add-to-cart=<product_id> to add product add to cart URL.
https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/
Try this approach
//Ajax
add_action('wp_ajax_data_fetch' , 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch','data_fetch');
function data_fetch(){
$the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'product' ) );
if( $the_query->have_posts() ) :
while( $the_query->have_posts() ): $the_query->the_post();
$myquery = esc_attr( $_POST['keyword'] );
$a = $myquery;
$search = get_the_title();
if( stripos("/{$search}/", $a) !== false) {
//get image and add-to-cart buttom here
$query->the_post();
global $product;
wc_get_template_part('content', 'product');
//End get image and add-to-cart buttom here
}
endwhile;
wp_reset_postdata();
endif;
die();
}
I created a custom post type "stm_media_gallery"
And three category inside this custom post type.
I want to display category name associated with each post.
<?php $gallery_query = new WP_Query( array('post_type' =>
'stm_media_gallery', 'posts_per_page' => -1) );
if( $gallery_query->have_posts() ) :
while( $gallery_query->have_posts() ) : $gallery_query->the_post(); ?>
--Display post name and its category name
<?php endif; ?>
<?php endwhile; ?>
You just need to put following code inside loop :
<div>
<?php
foreach((get_the_category()) as $category){
echo $category->name."<br>";
echo category_description($category);
}
?>
</div>
Update in existing code
<?php $gallery_query = new WP_Query(
array('post_type' => 'stm_media_gallery',
'posts_per_page' => -1) );
if( $gallery_query->have_posts() ) :
while( $gallery_query->have_posts() ) : $gallery_query->the_post();
$gallery_category = get_the_category( get_the_ID() );
the_title( '<h3>', '</h3>' );
echo "<br>";
<?php foreach ( $gallery_category as $key => $value) { echo $value->category_nicename; } ?>
<?php endif; ?>
<?php endwhile; ?>
You can use the pre-made WordPress function the_category( $separator, $parents, $post_id ) to print the post categories as links.
Further information on the WordPress Codex: Function Reference: the_category
Edit: Print only the names:
$categories = get_the_category();
if ( ! empty( $categories ) ) {
echo esc_html( $categories->name );
}
Put this inside While Loop
global $post;
$postcat = get_the_category( $post->ID );
Only two categories need to be showed in the homepage. Can anyone help.
You can use WP_Query to get your posts list, and display it with the loop
Example :
$the_query = new WP_Query( array( 'category_name' => 'staff,news' ) );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
In your functions.php file paste the below code:
I am assuming that you want to show categories from two categories which are having ids 5 and 9.
function kiran_home_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '5,9');
}
}
add_action( 'pre_get_posts', 'kiran_home_category' );
Explanation:
kiran_home_category is just a custom name for the function. That can be any name. The way it works is you attach a function to the action hook pre_get_posts. So before getting the posts the function kiran_home_category will be called. And then inside the function I am changing the query here to only load categories with ID 5 and 9
In wordpress WP_query, category__in parameter used to select category with posts.
<?php
$query = new WP_Query( array( 'category__in' => array( 2, 6 ),'post_status'=>'publish','orderby'=>'menu_order','order'=>'Asc' ) );
if($query->have_posts()):
echo '<ul>';
while ( $query->have_posts() ) : the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
echo '</ul>';
endif;
?>
For more information about wordpress query click here , you can read more information.
<?php
$args = array( 'post_type' => 'post', 'posts_per_page' => -1,'category_name' => array('Latest News','News') );
$loop = new WP_Query( $args );
if($loop->have_posts()):
?><ul>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li> <span class="date"><?php echo get_the_date( 'd F Y');?></span>
<h3><?php echo get_the_title();?></h3>
<?php echo $description = get_the_content(); ?>
</li>
<?php endwhile;?>
</ul>
<?php endif;?>
<?php wp_reset_postdata(); ?>
Do the following, usually in page.php or single.php or if you want a custom page for a category, you can do, category-samplecat.php..
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => array('samplecat', 'anothercat'),
'paged' => $paged
);
$arr_posts = new WP_Query($args);
Then do the usual if, while statement..
if($arr_posts->have_posts() ) :
// Start the loop.
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();?>
<?php endwhile;
endif;
Hi I'm trying to figure out how to put a featured image in wordpress on my custom page.
here is my code:
<?php
$args = array( 'numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
if (has_post_thumbnail() ) {
foreach( $recent_posts as $recent ){
the_post_thumbnail();
echo '<li>' . $recent["post_title"].' </li> ';
}
}
else {
echo "no featured post";
}
?>
<?php
$args = array( 'numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
if( $recent_posts ) {
foreach( $recent_posts as $recent ){
if (has_post_thumbnail($recent['ID']) ) {
echo get_the_post_thumbnail($recent['ID']);
}
echo '<li>' . $recent["post_title"].' </li> ';
}
} else {
echo "no featured post";
}
?>
the_post_thumbnail requires you to be in a loop. If you want to retrieve with your own ID, use get_the_post_thumbnail
This is the actual code for the loop:
<div id="content" class="span9" role="main">
<?php if ( have_posts() ){
// Do we have any posts in the databse that match our query?
?>
<?php
get_template_part( 'inc/post-format/single', get_post_format() );
$enable_rtl = of_get_option('enable_rtl', false);
if(!of_get_option('disable_pagination')){
if($enable_rtl){
$next_post = get_adjacent_post( false, '', true );
$prev_post = get_adjacent_post( false, '', false );
}else{
$next_post = get_adjacent_post( false, '', false );
$prev_post = get_adjacent_post( false, '', true );
}
?>
<?php
}
// show related posts by tag
if(!of_get_option('disable_related_posts')){
get_template_part( 'inc/related-posts' );
}
endwhile; // OK, let's stop the post loop once we've displayed it
// If comments are open or we have at least one comment, load up the default comment template provided by Wordpress
if ( comments_open() )
comments_template( '', true );
}else{ // Well, if there are no posts to display and loop through, let's apologize to the reader (also your 404 error) ?>
<article class="post error">
<h1 class="404"><?php _e('Page not found', 'outbox'); ?></h1>
</article>
<?php } // OK, I think that takes care of both scenarios (having a post or not having a post to show) ?>
</div><!-- #content .site-content -->
This is the snippet which I want to add:
// Parent CPT
if ( 'cpt-parent' == wpse121567_get_cpt_hierarchy() ) {
// Normal loop here
if ( have_posts() ) : while ( have_posts() ) : the_post();
// Normal loop markup
endwhile; endif;
}
// Child CPT
else if ( 'cpt-child' == wpse121567_get_cpt_hierarchy() ) {
// Globalize post object
global $post;
// Output Parent CPT title and content
$parent = get_post( $post->post_parent );
echo '<h1>' . $parent->post_title . '</h1>';
echo '<div>' . apply_filters( 'the_content', $parent->post_content ) . '</div>';
// Fetch parent CPT comments
$parent_cpt_comments = get_comments( array(
'post_id' => $post->post_parent,
'status' => 'approve'
) );
// Loop through parent CPT comments
foreach ( $parent_cpt_comments as $comment ) {
// Output comment list markup here
}
}
// Grandchild CPT
else if ( 'cpt-grandchild' == wpse121567_get_cpt_hierarchy() ) {
// Comment Stats code goes here
}
This is how I added it inside my actual code:
<div id="content" class="span9" role="main">
<?php if ( 'cpt-parent' == wpse121567_get_cpt_hierarchy() ) {
// Normal loop here
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'inc/post-format/single', get_post_format() );
endwhile; endif;
}
// Child CPT
else if ( 'cpt-child' == wpse121567_get_cpt_hierarchy() ) {
// Globalize post object
global $post;
// Output Parent CPT title and content
$parent = get_post( $post->post_parent );
echo '<h1>' . $parent->post_title . '</h1>';
echo '<div>' . apply_filters( 'the_content', $parent->post_content ) . '</div>';
// Fetch parent CPT comments
$parent_cpt_comments = get_comments( array(
'post_id' => $post->post_parent,
'status' => 'approve'
) );
// Loop through parent CPT comments
foreach ( $parent_cpt_comments as $comment ) {
// Output comment list markup here
}
}
// Grandchild CPT
else if ( 'cpt-grandchild' == wpse121567_get_cpt_hierarchy() ) {
// Comment Stats code goes here
}
?>
<?php
$enable_rtl = of_get_option('enable_rtl', false);
if(!of_get_option('disable_pagination')){
if($enable_rtl){
$next_post = get_adjacent_post( false, '', true );
$prev_post = get_adjacent_post( false, '', false );
}else{
$next_post = get_adjacent_post( false, '', false );
$prev_post = get_adjacent_post( false, '', true );
}
?>
<?php
}
// show related posts by tag
if(!of_get_option('disable_related_posts')){
get_template_part( 'inc/related-posts' );
}
endwhile; // OK, let's stop the post loop once we've displayed it
// If comments are open or we have at least one comment, load up the default comment template provided by Wordpress
if ( comments_open() )
comments_template( '', true );
}else{ // Well, if there are no posts to display and loop through, let's apologize to the reader (also your 404 error) ?>
<article class="post error">
<h1 class="404"><?php _e('Page not found', 'outbox'); ?></h1>
</article>
<?php } // OK, I think that takes care of both scenarios (having a post or not having a post to show) ?>
</div><!-- #content .site-content -->
And I'm getting this error: P**arse error: syntax error, unexpected T_ENDWHILE** in this line: endwhile;
I'm sure is something wrong on how I added the code but it's very confusing for me because the actual code has a lot of php tags around...
Any suggestion? Thanks!
i have changed ,try below code
<div id="content" class="span9" role="main">
<?php if ( 'cpt-parent' == wpse121567_get_cpt_hierarchy() ) {
// Normal loop here
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'inc/post-format/single', get_post_format() );
endwhile; endif;
}
// Child CPT
else if ( 'cpt-child' == wpse121567_get_cpt_hierarchy() ) {
// Globalize post object
global $post;
// Output Parent CPT title and content
$parent = get_post( $post->post_parent );
echo '<h1>' . $parent->post_title . '</h1>';
echo '<div>' . apply_filters( 'the_content', $parent->post_content ) . '</div>';
// Fetch parent CPT comments
$parent_cpt_comments = get_comments( array(
'post_id' => $post->post_parent,
'status' => 'approve'
) );
// Loop through parent CPT comments
foreach ( $parent_cpt_comments as $comment ) {
// Output comment list markup here
}
}
// Grandchild CPT
else if ( 'cpt-grandchild' == wpse121567_get_cpt_hierarchy() ) {
// Comment Stats code goes here
?>
<?php
$enable_rtl = of_get_option('enable_rtl', false);
if(!of_get_option('disable_pagination')){
if($enable_rtl){
$next_post = get_adjacent_post( false, '', true );
$prev_post = get_adjacent_post( false, '', false );
}else{
$next_post = get_adjacent_post( false, '', false );
$prev_post = get_adjacent_post( false, '', true );
}
?>
<?php
}
// show related posts by tag
if(!of_get_option('disable_related_posts')){
get_template_part( 'inc/related-posts' );
}
// If comments are open or we have at least one comment, load up the default comment template provided by Wordpress
if ( comments_open() )
comments_template( '', true );
}else{ // Well, if there are no posts to display and loop through, let's apologize to the reader (also your 404 error) ?>
<article class="post error">
<h1 class="404"><?php _e('Page not found', 'outbox'); ?></h1>
</article>
<?php } // OK, I think that takes care of both scenarios (having a post or not having a post to show) ?>