How to display last published post? - php

I have a task to modify the code in one website so that it shows the latest post on the top of the page. The rest of the page is blog posts and this part of the code is fine. I'm just starting to learn PHP and have no clue what I need to do.
Here is the website so you can have a better understanding of what I need - https://yourbestbrace.staging.wpengine.com/
The post that is currently on top is not the last one published.
Also, the piece of the code that needs to be changed is placed in the header.php. Here it is ( full code screenshot since it wouldn't display the code properly, image part is missing - https://prnt.sc/oy6r8q):
$the_query = new WP_Query( array(
'posts_per_page' => 1,
'meta_key' => 'show_as_first',
'meta_value' => 1
));
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h1 class="new-home-title">Products Reviews of the Best Braces & Supports for You</h1>
<div class="header-post">
<div class="h-post-detail">
<p class="sub-title-home">Featured Review</p>
<h2> <?php the_title(); ?></h2>
<p><?php echo wp_trim_words( get_the_excerpt(), 22 ); ?></p>
Read More
</div>
<?php endif;

Related

Display (Wordpress) Child Pages with Title, Thumbnail and Excerpt

Apologies in advance for a question, which to an expert, I've no doubt is relatively obvious. I've looked at WordPress Codex for the appropriate information (the_post_thumbnail, the_excerpt, etc) but am not well versed enough in .php yet to implement it properly. Still learning!
I am trying to display, within the of a standard (WP) page, the child-pages, including their Title, Thumbnail and Excerpt. I can get everything to work bar the THUMBNAIL and EXCERPT with the following:
<div class="child-pages">
<?php
$pageChild = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'menu_order', 'sort_order' => 'ASC' ) );
foreach( $pageChild as $page ) {
?>
<!-- loop: child page -->
<div class="child">
<header class="entry-header">
<?php echo '<h3>'.$page->post_title.'</h3>'; ?>
</header><!-- .entry-header -->
<img src="<?php echo the_post_thumbnail_url( $page->ID ); ?>">
<?php echo $page->the_excerpt; ?>
</div>
<?php } ?>
</div>
So far, I can see the links/titles of the correct child-pages, and in the correct order, but not the Thumbnail or Excerpt. Obviously, I am not calling the Thumbnail or the Excerpt properly. Could someone please correct me?
I have also tried these lines, as supported by the twenty-sixteen theme:
<?php twentysixteen_post_thumbnail(); ?>
<?php the_excerpt(); ?>
Any help would be much appreciated!
The thumbnail doesn't show because the function the_post_thumbnail_url(); shows the thumbnail of the current post, the parameter is to specify the size of the image, not the post.
$post->the_excerpt is not necessarily filled. If you look at the add/edit post screen you'll notice two textfields, one for editing the content of the post and one for the excerpt. The excerpt is optional, so the function the_excerpt() shows the content of that field, but when it is empty it will show the first X characters of $post->the_content.
Regarding your second attempt: the functions the_excerpt(), get_permalink() and twentysixteen_post_thumbnail() don't work because you don't set up the post properly.
The easiest way would be to add setup_postdata() to your code:
$pageChild = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'menu_order', 'sort_order' => 'ASC' ) );
foreach( $pageChild as $page ) {
setup_postdata( $page );
// now you can use all the fancy functions like `the_excerpt()`
// add your output here
}
Or you can do it the recommended way, using a WP_Query object:
<?php
$args = array(
'post_parent' => $post->ID,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

WordPress broken pagination

Pagination on my custom post page was working fine but after adding few posts it got broken - the older post link is not working any more.
Please suggest how can I fix it? I have tried disabling plugins, changing permalinks and almost anything i could find easy on the WordPress Codex.
Here's my query with pagination:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('post_type' => 'press' , 'posts_per_page' => 50 , 'paged' => $paged);
query_posts( $args );
/*Setting up our custom query (In here we are setting it to show 12 posts per page and eliminate all sticky posts) */
//query_posts ( $args );//query_posts($query_string . '&caller_get_posts=6&posts_per_page=12');
?>
<ul class="griditemleft clear">
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<?php if (has_post_thumbnail() ) : ?>
<li>
<?php the_post_thumbnail('category-thumbnail'); ?>
<h2 class="press-title"><?php the_title(); ?></h2>
</li>
<?php endif; ?>
<?php endwhile; ?>
</ul>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
Its not entirely clear on what you are trying to achieve, but I think you want to look closely at http://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination
Specifying hard-coded offsets in queries can and will break pagination since offset is used by WordPress internally to calculate and handle pagination.
To get around this limitation, you will need to write some additional code to manually handle pagination; you need to detect whether a loop has additional pages and then dynamically calculate the appropriate offset for the current page.
The code for controlling custom pagination will all occur within your functions.php file and not within the template page.php You can set an initial offset, as well as redefine the number of posts per page. There are specific samples displayed on the codex link above.
You will be adding the action before the query is run, via
add_action('pre_get_posts', 'myprefix_query_offset', 1 );
and you will have to account for the customization via
add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );

Wordpress ACF Checkbox pull

So I did my due diligence looking on here for an answer since this has come up a few times in the Treehouse forum, but found nothing related. I also tried to find a related topic on the acf site but that didn't give me the right info either. Hopefully you guys can help me out.
At the end of a tutorial on Treehouse, the instructions explain to add a few custom fields using ACF. He explains how to pull all of those in the code except on one crucial one.
We are supposed to create a checkbox field with the Field Label as Display on Homepage Slider and Field Name (or slug) as display_on_homepage. The idea is that we check this on each custom post entry that we want to display on the homepage slider, if you hadn't already guessed that.
Here's the code for the slider as it stands now.
<?php get_header('header.php'); ?>
</div>
<div id="featured" class="clearfix flexslider">
<ul class="slides">
<?php
$args = array(
'post_type' => 'work'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li style="background-color:<?php the_field( 'background_color' ); ?>;">
<div class="container">
<div class="grid_7">
<img src="<?php the_field( 'homepage_slider_image' ); ?>" alt="<?php the_title(); ?> Project Screenshot">
</div>
<div id="featured-info" class="grid_5 omega">
<h6>Featured Work</h6>
<h3><?php the_title(); ?></h3>
<p><?php the_field( 'description' ); ?></p>
<p><a class="btn blue" style="background-color: <?php the_field( 'button_color' ); ?>" href="<?php the_permalink(); ?>">View Project →</a></p>
</div>
</div>
</li>
<?php endwhile; endif; ?>
</ul>
</div>
I'm sure I need to pull some conditions in the args or even establish a different rule in my acf plugin, but I'm at a loss as to where to start with that can of worms. Thanks in advance for any help or advice. I'll be sure to forward this answer to the forum if I can get any assistance.
You'll want to add in an argument to your query for your custom field (meta field): http://codex.wordpress.org/Class_Reference/WP_Query
$args = array(
'post_type' => 'work',
'meta_key' =>'display_on_homepage',
'meta_value'=>'true'
);
The first answer is almost there, just missing a couple of commas :)
$args = array(
'post_type' => 'work',
'meta_key' =>'display_on_homepage',
'meta_value'=>'true'
);

How to add posts in wordpress pages?

I want to show some posts in the homepage of my wordpress site..How will I do it??or is there any plugin that can help me do it?or is there shortcodes that could pull out and display those post in my homepage?
If you want to do it the easy way, you can use a plugin like Display Posts Shortcode.
Or, if you want to do it manually, you can use get_posts().
Here's an example you could use:
<?php
if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("cat=$cat&showposts=5");
if ($posts) {
foreach ($posts as $post):
setup_postdata($post); ?>
<?php the_title(); ?></h2>
<?php endforeach;
}
}
?>
I hope this helps!
The implemetation varies greatly by theme.
Check if your wordpress theme has a file called index.php.
If you have this file for your current theme, this is the file responsible for displaying your home page. And this is where you will have to put the code snippets to display posts.
Presuming that you know a bit of html and PHP you will have to decide the suitable place within index.php to add the code suggested above by Amal Murali.
If you want to show a specific category post on Homepage you can use category slug or category name. Like the below to show and use the wp_pagenavi() plugin to show pagination and present it.
<?php
$paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'CATEGORY NAME ',
'posts_per_page' => 5,
'paged' => $paged,
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail();
endif;
?>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<div class="entry-content">
<?php the_excerpt(); ?>
Read More
</div>
</article>
<?php
endwhile;
wp_pagenavi(
array(
'query' => $arr_posts,
)
);
endif;
?>

Page navigation won't show up for the Homepage

I use PHP Code for post to place PHP into a template. Everything works fine, but the page navigation won't show up.
Here's a simple loop from my site. It was designed in local host, so I can't give you the address.
Here is my code:
<?php $lastest= new WP_Query ( array
(
'post_type' => post,
'posts_per_page' => 10,
'ignore_sticky_posts' => 1
)
);
?>
<div id="content-wrap">
<?php while($lastest->have_posts()) : $lastest->the_post(); ?>
<div id="lastest-new-wrap">
<div id="thumbnail-wrap">
<div id="thumbnail-size"><?php the_post_thumbnail(); ?></div>
</div>
<h1 id="title"><?php the_title(); ?></h1>
<div id="post-desc"><?php the_excerpt(); ?></div>
</div>
<?php endwhile; ?>
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
</div>
<?php wp_reset_postdata(); ?>
Thx you for reply !
But the text link don't show . I mean Older post and New post
I'm using this php code to call it :
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
Note : First time , i use "Your latest posts" at Homepage . It show good but Paged is not right !
Second time , i use a Static Page for Homepage . Then the text link don't show up ! Do use know why ? I use PHP Code for Post to insert PHP Code in Page . Then I Design one unique Page named "HomePage" . Design this Page with
"WPBakery Visual Composer"
If u know how to fix it plz help me ! Thx so much
You are using custom query, so you need to add $paged parameter like this:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
<?php $lastest= new WP_Query ( array
(
'post_type' => post,
'posts_per_page' => 10,
'ignore_sticky_posts' => 1,
'paged' => $paged
)
);
?>

Categories