I'm trying to create a navigation based around post titles. The main navigation at the top is the Categories, in a sidebar will be the Post Titles from that relevant Category.
The code I'm using works up until a point (i got it from List wordpress posts by category which match page title).
But I have 2 problems.
The main issue is when you get to the single post it just lists that post in the sidebar and no others from the same category.
The second minor issue, is it is listing pages when I don't need it to.
<p>below lists the right posts for the right
cats but fails at the single post stage</p>
<?php
$test = get_the_title();
$args = array( 'cat_name' => $test );
$args = array_merge( $args , $wp_query->query );
get_posts( $args ); while (have_posts()) { the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<ul>
<li><?php the_title(); ?></li>
</ul>
<?php } ?>
Any help would be appreciated. Thanks
for you second issue you can add one argument 'post_type' => 'post'
it will only display posts.
$test = get_the_title();
$args = array( 'cat_name' => $test,'post_type' => 'post' );
$args = array_merge( $args , $wp_query->query );
Related
I have a custom post type and it have many categories. When I open a single post of this custom post type post, then at the bottom of this custom single post, I want to show 8 posts title, image etc from this single post category.
Add this code in single.php of your theme.
$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 8, 'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
<ul>
<li>
<?php the_title(); ?>
<?php the_content('Read the rest of this entry »'); ?>
<?php the_post_thumbnail( 'thumbnail' ); ?>
</li>
</ul>
<?php }
wp_reset_postdata(); ?>
I want to be able to list ALL posts (children and parent) when browsing a parent category.
Like this:
Parent (Show posts from Child 1 and Child 2)
Child 1 (Show only posts from Child 1)
Child 2 (Show only posts from Child 2)
With this code below (placed in category.php) I don't get all the posts when I'm in a parent category. I just get posts from ONE child category instead of several.
Any ideas how to solve this?
<?php get_header(); ?>
<div class="container">
<div class="row">
<?php get_template_part( 'include-cat-tag' ); ?>
<div class="col-xs-12 col-sm-9 col-md-9 list-page-middle">
<header class="clearfix">
<h1><?php single_cat_title( '', true ); ?></h1>
</header>
<?php
wp_reset_query();
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
$args = array(
'posts_per_page' => 100,
'category__in' => array($category_id),
'orderby' => 'meta_value title',
'order' => 'ASC',
'post_status' => 'publish',
'meta_key' => 'betyg',
'child_of' => $category_id
);
query_posts( $args );
if (have_posts()): while (have_posts()) : the_post();
get_template_part( 'include-list-post' );
?>
<?php endwhile; ?>
<?php else: ?>
<?php get_template_part( 'include-no-post' ); ?>
<?php endif; ?>
</div>
</div>
<?php
get_template_part( 'include-list' );
get_template_part( 'include-social' );
?>
</div>
</div>
<?php get_footer(); ?>
I think there is a few problems with your query.
"child_of" is not supported for query_posts/get_posts function. Instead, it is only a valid option for get_categories. I think you are confused between querying the posts and categories here. You can check parse_query or get_posts function to see all options available.
https://codex.wordpress.org/Plugin_API/Action_Reference/parse_query
https://developer.wordpress.org/reference/functions/get_posts/
"meta_key" option shown but "meta_value" missing.
It is also not recommended by WP to use query_posts directly but to use get_posts function or to hook to the pre_get_posts action.
To get the list of all posts of of child categories of a category, you can do something as follows:
<?php
$categories = get_categories(array("child_of" => $parent_category->term_id));
$posts_to_display = get_posts("category" => $parent_category->term_id);
foreach($categories as $category){
// query child posts of this category here using get_posts
// then merge them to the $posts_to_display array
}
//Do what ever you want with the array
Or if you still want to make use of query_posts and the Wordpress loop not using get_posts, use can hook to the pre_get_posts action:
<?php
function get_posts_for_parent_category( $query ) {
// make sure that you are in the category page not single post
// or custom post type page
if(!$query->is_category)
return;
$parent_category = $query->get_querried_object();
$categories_to_query = array($parent_category->term_id);
// Using get_categories to get all child categories_to_query
// Get their ids and add it to the $categories_to_query array
$query->set("category__in",$categories_to_query);
}
add_action( 'pre_get_posts', 'get_posts_for_parent_category' );
I am using wordpress and have set up a couple of Custom Post Types. I need to know how to exclude these from the main look so the Custom Post Types do not show up in the blog.
Just replace 'post_type' => 'home-post' with 'post_type' => 'post' or delete that line and it will only pull your regular posts
this code use for fetch post type data
<?php
$args = array('post_type' => 'post','posts_per_page' => -1);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile;?>
I need to show future posts in all posts list on page.
my code
$args = array('posts_per_page' => 10,
'category' => $category->cat_ID );
?>
<?php $posts = get_posts($args); ?>
what arguments I can to write for showing all posts from category with future posts?
And it my archive page code, what should to show posts list from category
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
<?php endif; ?>
this solution mast to show future and past posts too
You should specify the post_status to be publish and future in your $args, for example:
$args = array(
'posts_per_page' => 10,
'category' => $category->cat_ID,
'post_status' => array(
'publish',
'future'
)
);
To get the posts with these args, use (for example):
$posts = get_posts($args);
To display these posts, use (for example):
foreach( $posts as $p ) {
// display the post title
echo '<h2>' . apply_filters('the_title', $p->post_title) . '</h2>';
// display the post date
echo '<h4>' . date('F jS, Y', strtotime($p->post_date)) . '</h4>';
// display an excerpt of the post
echo wp_trim_excerpt($p->post_content);
}
An alternative is to use query_posts / WP_Query to get these posts. In this case, to display the posts, you would use a loop, similar to the one in your question. Such example can be seen here: http://codex.wordpress.org/Class_Reference/WP_Query#Standard_Loop
How do I get a random post in Wordpress?
I would like to display a button on a page that, when pressed, goes to a random post from the blog. I don't want a random post to be displayed on the page, I just want a link that leads to that post.
I tried searching for a code on Google and here at stackoverflow but no success.
Thanks...
UPDATE:
Here is my template code:
<?php /*Template Name: Random*/ ?>
<?php get_header(); ?>
<nav><?php wp_nav_menu(array('menu' => 'Main Nav Menu')); ?></nav>
<div id="main-content-archive">
<div class="grey-text">Random post</div>
<?php $query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );?>
<?php if (have_posts()) : while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
?>
<?php endwhile; ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
create a page template, and use the following code to get a random post:
//Create WordPress Query with 'orderby' set to 'rand' (Random)
$the_query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );
// output the random post
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();
then in a page, just use:
see a random post
I found this post which gave me desired results...
Here's a solution copy/pasted from the wpbeginner blog post. No copyright infringement intended.
Just add the following code to the functions.php file:
add_action('init','random_add_rewrite');
function random_add_rewrite() {
global $wp;
$wp->add_query_var('random');
add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
}
add_action('template_redirect','random_template');
function random_template() {
if (get_query_var('random') == 1) {
$posts = get_posts('post_type=post&orderby=rand&numberposts=1');
foreach($posts as $post) {
$link = get_permalink($post);
}
wp_redirect($link,307);
exit;
}
}
Use mydomain.com/random/ as your href for your button that leads to the random post.
Thanks everyone who contributed for your help...
Cheers!
I find it is more useful to have a URL that will redirect to a random post that you can use as link in sidebar or in menus. If it is a single WP site and even on wp.com it's really easy, for a blog at
http://mygroovywpsite.me/
All you need to do is append it with ?random
http://mygroovywpsite.me/?random
I found this did not work (nor the wp_beginner code above) on subsites in my multisite installation, either approach just loaded the home page. Maybe I had some funky cache issues. The way I do this on many sites is a few more steps w/o plugins.
First make a Page in your site called "Random" / with the slug "random" -- it does not need any content in it
Then create a page-random.php template
<?php
/*
Random Post Picker
Use on page to send viewer to random post optionally mod query
*/
// set arguments for WP_Query on published posts to get 1 at random
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'orderby' => 'rand'
);
// It's time! Go someplace random
$my_random_post = new WP_Query ( $args );
while ( $my_random_post->have_posts () ) {
$my_random_post->the_post ();
// redirect to the random post
wp_redirect ( get_permalink () );
exit;
}
?>
Then you get the re-direct for any link on your blog ...../random w/o any wrestling with .htaccess
I've done it this way because I've had to modify the query, sometimes for custom post type, sometimes to restrict to category, etc.
I only had one site that was a problem because the hosting suppressed the use of mySQL queries with ORDER BY RAND()
Another Simple solution to display Random Post
1.First a create a custom page template. Name it as random post or a name of your choice!
2.Open the page and remove the default wp loop and Paste the code below
3.To change the no of post change the number ‘1’ to your choice!
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile;
endif; ?>
source: http://www.yengkokpam.com/displays-random-posts-in-a-page/
Check This
<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>