Output recent posts and excerpt in Wordpress, not of current page - php

I am trying to output recent posts plus the excerpt onto my homepage using the following code:
<?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.$recent["post_title"].'" >' . $recent["post_title"].'</a>' . $recent["post_excerpt"] . ' </li> ';
}
?>
This seems to output the title and the permalink just fine, however it does not output the excerpt.
Hope someone can help

put the array in your desired custom post like this in your functions.php
$args = array(
'supports' => array('title','editor','author','excerpt') // by writing these lines an custom field has been added to CMS
);
For retrieving at front end
echo $post->post_excerpt; // this will return you the excerpt of the current post

try this one
<?php
$args = array( 'post_type'=>'post',
'orderby'=>'post_date',
'post_status'=>'publish',
'order' => 'DESC',
'showposts' => '3' );
$recent_posts = get_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent->ID) . '" title="Look '.$recent->post_title.'" >' . $recent->post_title.'</a>' . $recent->post_excerpt . ' </li> ';
}
?>
Make sure your post_excerpt is not empty
If you want to add the post_excerpt then use wp_update_post
$my_post = array();
$my_post['ID'] = 37;// it is important
$my_post['post_excerpt'] = 'This is the updated post excerpt.';
wp_update_post( $my_post );
As per your request in comments i am showing you the demo to update the post by copying the post_title in the post_excerpt so here you go
<?php
$args = array( 'post_type'=>'post',
'orderby'=>'post_date',
'post_status'=>'publish',
'order' => 'DESC',
'showposts' => '3' );
$recent_posts = get_posts( $args );
foreach( $recent_posts as $recent ){ // this foreach to add the excerpt
$my_post = array();
$my_post['ID'] = $recent->ID;// it is important
$my_post['post_excerpt'] = $recent->post_content;
wp_update_post( $my_post );
}
foreach( $recent_posts as $recent ){ // this foreach to show the excerpt
echo '<li><a href="' . get_permalink($recent->ID) . '" title="Look '.$recent->post_title.'" >' . $recent->post_title.'</a>' . $recent->post_excerpt . ' </li> ';
}
?>
wp_update_post
Also see wp_insert_post

Related

How do I sort categories Wordpress blogs by recent post date?

Hey everyone I am attempting to order my categories by most recent post date. This means when I go to add a post and mark it under that category I want the category to show at the top from most recent to old. Also if you have a way to hide a certain category id I would love to know how. I'm new with PHP and would love some help, thanks everyone.
CODE:
<?php
$cat_args = array(
'orderby' => 'date',
'post_type' => 'products',
'order' => 'ASC',
'child_of' => 0,
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
echo '<dl>';
echo '<h3 class="category-name">' . $category->name.'</h3>';
$post_args = array(
'numberposts' => -1,
'category' => $category->term_id
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<dd><a class="article" target="_blank" href="<?php the_field('article_link') ?>"><?php the_title(); ?></a><span class="news-source"> - <?php the_field('news_source') ?></span><p class="important"><?php the_field('important') ?></p></dd>
<?php
}
//echo '<dd class="view-all"> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>View all posts in ' . $category->name.'</a></dd>';
echo '</dl>';
}
?>
First get recent posts and from that get categories after getting categories run loop through categori it.
$args = array( 'numberposts' => '-1' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<dl>';
echo '<h3 class="category-name">' . get_the_category_list( ', ', '', $recent["ID"] ).'</h3>';
$post_args = array(
'numberposts' => -1,
'category' => $recent["ID"]
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<dd><a class="article" target="_blank" href="<?php the_field('article_link') ?>"><?php the_title(); ?></a><span class="news-source"> - <?php the_field('news_source') ?></span><p class="important"><?php the_field('important') ?></p></dd>
<?php
}
//echo '<dd class="view-all"> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>View all posts in ' . $category->name.'</a></dd>';
echo '</dl>';
}
wp_reset_query();

How to load post titles for each category while loading all categories for a custom post type

I created a custom post type for an FAQ section on my page. I want each category to load as a section title, with the post titles loading below each category.
I am able to load the categories using the following code:
<ul class="sort-by-category">
<?
$args = array(
'orderby' => 'name',
'parent' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '<li><a class="browse-categories" href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a></li>';
}
?>
</ul>
<?
// Our variables
$page = (isset($_GET['pageNumber'])) ? $_GET['pageNumber'] : 0;
// grab slug from url
$category_id = get_query_var('cat');
// define args
$args = array(
'posts_per_page' => '36',
'paged' => $page,
'post_type' => 'faqs',
'cat' => $category_id
);
// place args in query
$category_posts = new WP_Query($args);
// our loop
if($category_posts->have_posts()) {
while($category_posts->have_posts()) {
$category_posts->the_post();
get_template_part( 'partials/content', 'browse' );
}
}
wp_reset_query(); ?>
Please help! I'm not great with php

Display custom posts with shortcode

I've been searching this forum for help for 2 days now but I can't find a solution for what I'm trying to achieve.
I have a custom post type called "product" and inside each post in that post type there are two custom fields called produktnamn(a textfield with product name) and produktbild(a product image). that's it.
Now I need a shortcode to display a particular post in that post type by entering it's slug (which is the same as it's name).
like this:
[product name="myproduct"]
That will render the name and product image inside some HTML tags.
Please help!
My code so far:
//Product image
function cpt_content_func($atts){
extract( shortcode_atts(['name' => null], $atts ) );
$args = [
'name' => $slug,
'post_type' => 'product',
'numberposts' => 1
];
$posts = get_posts( $args );
$post = $post[0];
$title = $post->title;
$id = $post->ID;
get_post_meta('produktbild', $id);
$content = $post[0]->post_title;
return '<h3>'.$content.'</h3>';
}
add_shortcode('product','cpt_content_func');
Solved it. Here's the code!
function display_custom_post_type( $atts ){
// Handle the attributes
$atts = shortcode_atts(
array(
'cat' => ' '
),
$atts, 'products_by_cat' );
// Set default values for the post
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'post_status' => 'publish',
'category' => $atts['cat']
);
// Get posts
$posts_array = get_posts( $args );
// Check if posts exist
if( !empty($posts_array) ){
$string = '<div class="row">';
foreach( $posts_array as $post ){
$string .= '<div class="col-sm-3 text-center">';
$string .= '<div class="product-container">';
$string .= '<a href="' . get_field( "order_link", $post->ID ) . '">';
$string .= '<img class="product-img" src="' . get_field( "produktbild", $post->ID ) . '">';
$string .= '</a>';
$string .= '</div>';
$string .= '<p class="product-btn">' . get_field( "produktnamn", $post->ID ) . '</p>';
$string .= '</div>';
}
$string .= '</div>';
} else {
// Didn't find any posts.
}
// Return HTML
return $string;
I agree with James--I'd just use woocommerce.
But you first step has to be learning how to build a shortcode: http://code.tutsplus.com/tutorials/wordpress-shortcodes-the-right-way--wp-17165
Once you've attempted creating your own shortcodes, if things still aren't working, that's the time to ask for help.

WordPress: Get all tags from custom post type

I'm trying to get all the tags that are within my custom post type "resource".
The problem is that I'm outside of the loop and struggling to find a way to get the functionality to work with the custom post type.
I have the category setup also as "resource_category"
My current code is:
$tax = 'post_tag';
$terms = get_terms( $tax );
$count = count( $terms );
if ( $count > 0 ): ?>
<div class="post-tags">
<?php
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, $tax );
echo '' . $term->name . ' ';
} ?>
</div>
<?php endif;
Can anyone help?
you are asking for the tags right, because the answer from 2015 is listing categories not tags
$args = array(
'type' => get_post_type(),
'orderby' => 'name',
'order' => 'ASC'
);
$tags = get_tags($args);
foreach($tags as $tag) {
var_dump($tag->name);
}
Tags are also WordPress taxonomy. So, you can get all the tags as like you get all terms
Read More
$tags = get_terms([
'taxonomy' => 'YOUR_CUSTOM_TAXONOMY',
'hide_empty' => false
]);
var_dump($tags);
You can also copy custom post taxonomy from Tags page URL.
http://localhost/wp-admin/edit-tags.php?taxonomy=YOUR_CUSTOM_POST_TAG_TAXONOMY_NAME&post_type=custom-post-type
$args = array(
'type' => 'resource',
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
}

Wordpress: Get recent posts, exclude current post

I am trying to get recent posts, only in the category of the current post, while excluding the current post, but I can't get it to work:
$curr_cat = get_the_category();
$args = array( 'numberposts' => '10', 'post_status' => 'publish', 'category' => $curr_cat['0']->cat_ID, 'exclude' => $post->ID );
$recent_posts = wp_get_recent_posts( $args );
It's just showing the current post over and over again.
John, you can try something like code below, I don't know your case but it works for me in one of my project
$args = array ('category__in' => $curr_cat['0']->cat_ID, 'posts_per_page' => 10, 'post__not_in' => array( $post->ID ) );
Your code is fine, be sure that you are echoing your posts like:
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
}
If you are doing like above, be sure that $post is set and you are in a category archive or single post file.

Categories