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.
Related
Hi I have created my own custom post type within Wordpress to contain projects that i can call via my theme files.
I am new to creating my own themes. I currently am using the following code in my single.php file to call in related articles based on the category of the blog post.
<?php
// Default arguments
$args = array(
'posts_per_page' => 3, // How many items to display
'post__not_in' => array( get_the_ID() ), // Exclude current post
'no_found_rows' => true, // We don't ned pagination so this speeds up the query
);
// Check for current post category and add tax_query to the query arguments
$cats = wp_get_post_terms( get_the_ID(), 'category' );
$cats_ids = array();
foreach( $cats as $wpex_related_cat ) {
$cats_ids[] = $wpex_related_cat->term_id;
}
if ( ! empty( $cats_ids ) ) {
$args['category__in'] = $cats_ids;
}
// Query posts
$wpex_query = new wp_query( $args );
// Loop through posts
foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<div class="col-md-4 related-post">
<?php the_post_thumbnail('large'); ?>
<?php the_title(); ?>
</div>
<?php
// End loop
endforeach;
// Reset post data
wp_reset_postdata(); ?>
In my new post type "projects" i would like to call in related projects. Which im assuming would be very similar code except i need to stop it looking for posts and instead look for my projects.
Here is my code for new post type:
// Projects
add_action( 'init', 'create_post_type' );
add_post_type_support( 'bw_projects', 'thumbnail' );
add_post_type_support( 'bw_projects', 'custom-fields' );
function create_post_type() {
register_post_type( 'bw_projects',
array(
'labels' => array(
'name' => __( 'Projects' ),
'singular_name' => __( 'Projects' )
),
'public' => true,
'has_archive' => true,
'taxonomies' => array( 'category','post_tag'),
)
);
}
What would i need to change in my first code snippet in order to look for bw_projects and not look for 'posts' anymore. I tried playing around and changing certain lines myself but i caused more issues and stopped the page loading. Is this even right i can use the same code, slightly altered or would i need something completely different?
Thanks in advance.
You can get any post type that you require using get_posts();
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'projects',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
Simply set the 'post_type' argument to that of you custom post type, to get only these posts. You can also set the number of post, and filter by category etc.
You can find more info in the codex.
Alternatively, if you wanted to keep something similar to your existing code you could try using 'pre_get_posts' to filter the query to just your projects. However you'd need to remember to add / remove this filter so it only operates on the queries that need it.
To display the posts you can use a simple foreach to churn them out. You#d obviously want to do some sort of styling to get the layout correct:
$args = array("posts_per_page" => 10, "orderby" => "comment_count");
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
echo "<h1>" . $post->post_title . "</h1><br>";
echo "<p>" . $post->post_content . "</p><br>";
}
Or a really concise way of doing all of the above would be something like:
$args = array("posts_per_page" => 5, "post_type" => "projects");
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
echo "<h1>" . $post->post_title . "</h1><br>";
echo "<p>" . $post->post_content . "</p><br>";
}
I basically want to create a category in the "Links" section of wordpress and add a number of links and titles for that category. Simple stuff.
I then want to be able to, in my template file, echo the links and title or anything about the link individually as i please. Preferably in a loop as I have some page building to do before and after the links.
I know 'category_before' and 'category_after' exist but they won't do what I need.
So I tried,
<?php $args = array(
'orderby' => 'name',
'order' => 'ASC',
'limit' => -1,
'category' => '3',
'hide_invisible' => 1,
'show_updated' => 0,
'echo' => 1,
'categorize' => 0,
'category_orderby' => 'name',
'category_order' => 'ASC',
'class' => 'linkcat',
'category_before' => '<tr><td>',
'category_after' => '</td></tr>' );
wp_list_bookmarks( $args );
?>
But that does a few things wrong. I don't need the category title or anything else besides the link text and destination really.
I am hoping to have a 'for' loop that will loop all links and I can just build my code section and links inside that, but let me know if there is a better way.
Thanks
EDIT: More Info
So I tried:
<?php
$taxonomy = 'link_category'; // Taken from the DB table
$tax_terms = get_terms( $taxonomy, array( 'hide_empty' => false ) );
?>
<ul>
<?php
foreach ($tax_terms as $tax_term) {
echo $tax_term->name;
}
?></ul>
Which is the closest I have got. This only returns the info about the category not what is in the category.
There is nothing in the "wp_term_taxonomy" table in the DB about the actually category I have made.
Thanks again
EDIT:
Here is the area I'm referring to:
I want to show these 2 links
You may want to try the get_bookmarks function.
$bookmarks = get_bookmarks( array(
'orderby' => 'name',
'order' => 'ASC',
'category_name' => 'category-name'
));
// Loop through each bookmark and print formatted output
foreach ( $bookmarks as $bookmark ) {
printf( '<a class="relatedlink" href="%s">%s</a><br />', $bookmark->link_url, $bookmark->link_name );
}
https://codex.wordpress.org/Function_Reference/get_bookmarks
For more control you can use the function get_terms :
<?php
//list terms in a given taxonomy
$taxonomy = 'category'; // Pass default category or any custom taxonomy name
$tax_terms = get_terms($taxonomy);
?>
<ul>
<?php
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
}
?>
</ul>
For info functions & its parameter:
https://developer.wordpress.org/reference/functions/get_terms/
Please, help me with the matter discussed below:
I have registered this shortcode:
[category_post title="world news" category="world news" link="#"]
to call different categories. But the problem is when I post and select a single category, the post is shown in all category. But, I don't want this. I want the post to show only in that category that has been mentioned in the short-code. Can anyone help me to solve this problem?
Please, have a look at the picture and also I have given u the function here:
/* Register shortcode for querying custom category post *********/
function category_post_shortcode($atts){
extract( shortcode_atts( array(
'title' => '',
'link' => '',
'category' => '',
), $atts, 'category_post' ) );
$q = new WP_Query(
array( 'category' => $category, 'posts_per_page' => '4', 'post_type' => 'post')
);
$list = '<div class="latest_from_category"><h2 class="latest_frm_cat_title">'.$title.'</h2> more';
while($q->have_posts()) : $q->the_post();
//get the ID of your post in the loop
$id = get_the_ID();
$post_excerpt = get_post_meta($id, 'post_excerpt', true);
$post_thumbnail= get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
$list .= '
<div class="single_cate_post floatleft">
'.$post_thumbnail.'
<h3>'.get_the_title().'</h3>
<p>'.$post_excerpt.'</p>
বিস্তারিত
</div>
';
endwhile;
$list.= '</div>';
wp_reset_query();
return $list;
}
add_shortcode('category_post', 'category_post_shortcode')
The category parameter for WP_Query expects the category ID - to query by name, use category_name:
$q = new WP_Query(
array( 'category_name' => $category, 'posts_per_page' => '4', 'post_type' => 'post')
);
http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
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
I have a category query, and in my category query I want to get product (only one) by queried category id (or name or whatsoever)
I start query:
<?wpsc_start_category_query(array('category_group'=> get_option('wpsc_default_category'))); ?>
and then try to use get_posts() function to get product:
$args = array(
'post_type' => 'wpsc-product',
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'wpsc_product_category',
'field' => 'id',
'terms' => $aka
)));
$cat1_posts = get_posts($args);
where $aka is:
$aka = '[wpsc_category_id]';
but when I echo $cat1_posts[0]->ID; it only shows my last product ID for every category. what is the problem? echoing only [wpsc_category_id] works perfect.
I tried EVERYTHING for the last few days. I will buy you cookies for help
I've got to idea, that I need foreach or anything like this
You can use the get_terms() function. So something like this (untested)
<?php
//for each category, show latest post
$cat_args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_terms( 'wpsc_product_category');
foreach($categories as $category) {
$args=array(
'showposts' => 1,
'post_type' => 'wpsc-product',
'wpsc_product_category' => array($category->slug)
);
$posts=get_posts($args);
if ($posts) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
foreach($posts as $post) {
setup_postdata($post); ?>
<p><?php the_title(); ?></p>
<?php
} // foreach($posts
} // if ($posts
} // foreach($categories
?>