What is the best way to show multiple pages in a Wordpress page.
I only want to show the text before the read more. The following code is displaying the whole content instead of only the read more.
$args=array(
'orderby' =>'parent',
'order' =>'asc',
'post_type' =>'page',
'post__in' => array(9,24,33),
);
$page_query = new WP_Query($args); ?>
<?php while ($page_query->have_posts()) : $page_query->the_post(); ?>
<article class="g320">
<?php the_content("Lees meer",true);?>
</article>
<?php endwhile; ?>
There is a global variable $more which enables / disables this feature for the_content() function. Read more in Codex
In your example, the solution would be like this:
$args=array(
'orderby' =>'parent',
'order' =>'asc',
'post_type' =>'page',
'post__in' => array(9,24,33),
);
$page_query = new WP_Query($args); ?>
<?php while ($page_query->have_posts()) : $page_query->the_post(); ?>
<article class="g320">
<?php global $more; $more = 0; ?>
<?php the_content("Lees meer",true);?>
</article>
<?php endwhile; ?>
Currently you are displaying full content using the_content.
But you can display excerpt with [...] at the end of each post.
So change the_content to the_excerpt and check if this fits your needs.
Read more here - http://codex.wordpress.org/Function_Reference/the_excerpt
As you can read in above link "By default, excerpt length is set to 55 words. To change excerpt length using excerpt_length filter, add the following code to functions.php file in your theme:
function custom_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
Related
I'm trying to use get_field to return a simple text field and it returns empty for some reason. A field itself is where it should be and there is text in it, so that part is all set. This PHP code is loaded via php snippet, post thumbnail for example, shows up perfectly. So everything works except for ACF field value.
<div class="your-class">
<?php
$args = array(
'post_type' => 'home_test',
'posts_per_page' => -1,
'orderby' => 'name',
'order' => 'ASC',
);
$the_query = new WP_Query($args);
$brand = get_posts($args);
foreach ($brand as $post) {
setup_postdata($post);
$thumbnail = get_the_post_thumbnail_url($post->ID, 'full');
$homelinkvalue = get_field("home_brand_link");
if (!$thumbnail)
continue;
?>
<div>
<p><?php echo $homelinkvalue; ?></p><img src="<?php echo $thumbnail; ?>">
</div>
<?php
}
wp_reset_postdata();
?>
</div>
I think the issue is that you are mixing together a custom post loop (your foreach and setup_postdata()) but then are using functions like get_field(), which make use of the global post object. In this case, get_field() tries to lookup the field value by checking against the global $post, but it has not been properly set. See warning here about setup_postdata($post):
You must pass a reference to the global $post variable, otherwise functions like the_title() don't work properly.
You can implement this in your code with a slight modification:
global $post;
foreach ($brand as $currPost) {
$post = $currPost;
setup_postdata($post);
// Rest of code as normal
}
Or, since get_field() can accept a specific post as an argument instead of automatically using the global one, you could change:
$homelinkvalue = get_field("home_brand_link");
to:
$homelinkvalue = get_field("home_brand_link",$post->ID);
Side note: Normally, the recommended way to iterate posts is with the special "WP loop" pattern, something like:
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!-- Do something -->
<?php endwhile; ?>
Using the above pattern automatically sets the global $post variable as it loops through, which allows devs to use functions like get_field() without having to worry about explicitly passing in a specific post; makes things a bit easier.
Try this one:
<div class="your-class">
<?php
$args = array(
'post_type' => 'home_test',
'posts_per_page' => -1,
'orderby' => 'name',
'order' => 'ASC',
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts) :
while($the_query->have_posts) : $the_query->the_post();
?>
<div>
<p><?php the_field( "home_brand_link" ); ?></p>
<img src="<?php the_post_thumbnail_url(); ?>">
</div>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
</div>
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 have the following loop that utilises Advanced custom fields (ACF) - Everything displays except the content. I have tried various solutions with no success.
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'sectors',
'orderby' => 'menu_order',
'order' => 'ASC'
));
global $post;
if($posts) {
?>
<?php
foreach($posts as $post) {
?>
<div class="content full sector">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
</div>
<?php
}
?>
<?php
}
wp_reset_postdata();
?>
Any help is greatly appreciated.
Thanks
When you use get_posts() to return post data, some post-related data is not available by default. One of these items is the_content(). This is resolved by calling an internal function setup_postdata(), with the $post array as its argument.
Solution: Add setup_postdata( $post ); within your foreach().
Read more in the Codex.
I am trying to figure out how to sort the search results by post type. I asked the question on wordpress stack exchange and someone tried answering but I don't think they understood what I was trying to achieve.
-- https://wordpress.stackexchange.com/questions/72914/search-results-sorted-by-post-types
For example, when a person searches a term they are taking to the page
with the results, all >posts found from all post types are shown but
up top there are the different post types name >links that will sort
the results and show only the respective post type's post. Right now I
have several loops on the search results page for each post type but when I test it, the >loops are all showing the same results even
though each loop has a query for a different post type.
My code for the search page - http://pastebin.com/L9zEw1cn
This is a little above the first loop <?php global $wp_query; $total_results = $wp_query->found_posts; ?> This is the first loop <?php if(have_posts()) : ?> <?php while(have_posts()) : the_post() ?> //My divs// <?php endwhile; endif; ?> <?php wp_reset_postdata(); ?>
Then the second loop which is the same for all the others except the post type name -- <?php $args = array( 'post_type' => 'videos', 's' => $s ); ?> <?php if(have_posts()) : ?> <?php while(have_posts()) : the_post() ?> //My divs <?php endwhile; endif; ?> <?php wp_reset_postdata(); ?>
How can I fix it so that each loops display the search results for only that post type?
Try using get_posts for the second loop.
$args = array(
'post_type'=> 'videos',
'numberposts' => -1,
's' => $s
);
$videos = get_posts( $args );
foreach( $videos as $post ) : setup_postdata($post);
// My divs
endforeach;
http://codex.wordpress.org/Template_Tags/get_posts
Edit: added 'numberposts' to the arguments.
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>