ACF: Display values outside the main loop - wordpress

I am coding a theme that uses an archive on a page with a special template. Above the results of the archive (that also means outside of the loop) I want to display the values of some custom fields. (e.g. the value of the field “intro”. I tried to create an additional loop and have the values displayed, but there are no results. Does anybody see a mistake?
Thanks!
Raphael
Here is what I tried last:
<div class="newsearch_intro">
<?php global $post;
$args = array('numberposts'=>'1');
$custom_posts = get_posts($args);
foreach($custom_posts as $post) : setup_postdata($post);
the_field('intro');
endforeach;
?>
</div>

Try using the_field() / get_field() with the post ID as a second parameter:
<div class="newsearch_intro">
<?php
global $post;
the_field('intro', $post->ID);
?>
</div>

Related

How to list posts from categories with custom post type? (WP)

Hi I am having an issues with custom post type.
So what I am trying to do, list all posts from subcategories while calling a main category. Usualy this worked with no problem with normal post type from Wordpress, but since I tried to use custom post type it's not working...
My category structure is like this:
Category
Sub category
( Posts inside )
Sub category
( Posts inside )
Any help or tips are appreciated. Thanks
<?php
$categories = get_categories('title_li=&hide_empty=1&parent=1430');
foreach($categories as $category) {
echo "<div class='col-12' style='border-bottom: 0'><h1 class=''>".$category->name."</h1></div>";
$args = array('cat'=> $category->term_id);
if (have_posts() ) : while (have_posts() ) : the_post(); ?>
<!-- article -->
<article class="col-3">
<div class="image">
<span class="helper"></span><?php the_post_thumbnail('full');?>
</div>
<h1><?php the_title(); ?></h1>
<?php the_content();?>
</article>
<!-- /article -->
<?php endwhile; endif; }?>
</main>
There are a couple of problems going on here:
First, you're not declaring a loop, or calling get_posts.
Second, if you check out the documentation for WP_Query (which is the "backbone" behind get_posts, so the arguments are essentially the same), you'll see that if you do NOT pass in an argument for post type, the default is post.
So, since you've not shared with us the post type, you'll have to adjust the below as needed:
// .. your code above ....
$args = array(
'cat'=> $category->term_id,
// Include the post_type in the query arguments
'post_type' => 'custom-post-type' // Change this as needed
);
// Now we need to actually query for the posts...
$custom_posts = new WP_Query( $args );
// These are modified to use our custom loop...
if ($custom_posts->have_posts() ) : while ($custom_posts->have_posts() ) : $custom_posts->the_post(); ?>
// .. your code below ... the_title(), etc will work here...

Wordpress - Two loops on author.php?

Normally if I was doing more than one loop on a page, aside from the main wordpress loop, I'd just use wp_query, however when the author.php template is being used I can't see how I could use that, since I'd have to pass some args.
Pulling posts from categories or by date etc is all easy enough with wp_query, even getting posts by author ID can be done, but it needs to be generic, ie get posts from the current authors page.
Now, using the same loop as my category page I can easily generate posts on the author.php, but I need a second loop and I just can't figure out how to do it.
First loop would be pulling one random post and it's featured image, second would be getting the archive of that authors posts.
Any ideas?
Does this give you a list of the author's posts? If so, it should be easy to modify the arguments for WP_Query to get exactly what you want.
See here for all the options: http://codex.wordpress.org/Class_Reference/WP_Query
<?php
$new_loop = new WP_Query( array(
'post_type' => 'post',
'author' => get_the_author_meta('ID')
) );
?>
<?php if ( $new_loop->have_posts() ) : while ( $new_loop->have_posts() ) : $new_loop->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; else: ?>
Sorry, nothing was found.
<?php endif; ?>
<?php wp_reset_query(); ?>

Wordpress: Adding the category as text - not link

I want to add a ribbon on my posts on the front page, which shows the posts category.
I can add it as text like this (I use the Imbalance theme by WPShower):
<?php imbalance2_posted_in(); ?>
But how can I just write the Category Name, without markup so I can use it in classes and such?
Thank you in advance.
You can get the category of every post with get_the_category(). Below is shown how to get the category of the current post.
global $post;
$category = get_the_category( $post->ID ); //OR SOME OTHER ID, DEPENDING ON WHAT YOU WANT
$category_name = $category->name; //GETS THE ORIGINAL NAME, INCLUDING WHITESPACES
$category_slug = $category->slug; //GETS THE SLUG, WHICH WILL BE BETTER TO USE IN CLASSNAMES
EDIT
<?php
global $post;
$category = get_the_category( $post->ID );
?>
<div class="box <?php echo($category->slug); ?>"></div>
You can use post_class() to generate a few class names including one for each category.
If you want to do it manually, you can get information on the categories using get_the_category() and put the class names together yourself.

Get a specific page's excerpt within the loop Wordpress

I am trying to create a loop that loads a random image from any posts, whilst also retrieving the excerpt of a specific page. I have done the random post part, but cannot get it to retrieve the page excerpt... I think I may need to query the pages in their own loop but I'm not sure how to do this. I have installed the function to get page excerpt support etc. but I think I am doing something wrong within the loop, any help would be appreciated.
<div class="postimage">
<?php if (have_posts()) :
query_posts('showposts=1&orderby=rand');
while (have_posts()) : the_post(); ?>
<?php the_post_thumbnail('blog-post-image'); ?>
<div class="borderimage"></div>
<div class="tagline"><h1><?php the_excerpt('$page_id=8'); ?> </h1>
</div>
</div>
</div>
<?php endwhile; else : endif; ?>
query_posts replaces the global $wp_query, which you don't want to do since you want to keep that query for your page. Try this instead...
if (have_posts()){
while(have_posts()){
the_post(); //global $post now has the page in it
$args = array("posts_per_page"=>1,"orderby"=>"rand");
$random_posts = get_posts($args); //returns an array
$random_post = $random_posts[0];
//do your stuff...
//$post contains the original page
//$random_post contains the random post
}
}

WordPress Rewind Posts

I have the following code: http://pastebin.com/ng99augD
It basically spits out five posts for the five authors I have specified in the query posts in each chunk of code and then rewinds the posts prior to the next query. This is far from ideal and would like to make the code more efficient without having to repeat all the template code for the posts over and over. I also want them to be in a random order instead of coming out in the order they have been written as I don't want any particular author to be at the top. Thanks.
I'm not sure why the use of rewind_posts() is necessary. You are creating a new query using by your use of query_posts() each time. rewind_posts() rewinds the query for re-use.
If your performance isn't suffering, it may be okay to run 5 queries to get your five authors. To avoid rewriting the same template code each time you have at least two options. Create a php file with the template code in it, and include it each time you need it:
<?php include('author_posts.php');?>
Or, better yet, create a function in your functions.php or in a plugin file that accepts your query results and outputs the template.
<?php $posts = query_posts('posts_per_page=1&author=author1');
if(function_exists('my_authors')){my_authors($posts);}
?>
Then in your functions.php or plugin:
function my_authors($posts){
//template code here, using $posts->ID, $posts->post_content, etc
}
The third option, which cleans up your code and the number of queries would be to use a category to display posts assigned to it here. You are making 5 queries to display five posts. To use one query to display five posts and sort randomly like you want to, edit each of the five posts and assign them to a new category, lets say:'author sampler'. Then in your template file:
<?php $posts = get_posts('category=author sampler'&order_by=rand&order=asc');
foreach($posts as $post):?>
<?php setup_postdata($post);
//your posted template code follows:
?>
<article id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?><?php edit_post_link('Edit', ' (', ')'); ?></h2>
<p class="p-cat">In: <?php the_category('|') ?></p>
<p class="p-author">
<span class="name">
<?php the_author_posts_link(); ?></span> <span class="avatar"><?php echo get_avatar( $email, $size = '32' ); ?>
</span>
</p>
<?php //etc ?>
<?php endforeach;?>

Resources