WordPress Advanced Custom Fields Relationship Single Page - php

I have a custom post type for "Clubs" and custom post type for "Events by Clubs". How can I get the name of the club on my single event page (not the while loop)?
I have searched and searched, but all I can find is to get it in a loop.
Below is the code for the single event page.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<section class="entry-content">
<ul>
<li>Club Name: { get Club Name from Club post type relationship } </li>
<li>Other detail: <?php the_field('blah_blah'); ?>
<li>.....other details.....</li>
</ul>
<?php the_content(); ?>
</section>
<?php endwhile; endif; ?>

You need to set up the postdata for the $club post object in order to access it:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$club = get_field('club_field_name');
if($club):
$post = $club;
setup_postdata($post);
$club_name = get_the_title();
wp_reset_postdata();
endif; ?>
<h1><?php the_title(); ?></h1>
<section class="entry-content">
<ul>
<li>Club Name: <?php echo $club_name; ?> </li>
<li>Other detail: <?php the_field('blah_blah'); ?>
<li>.....other details.....</li>
</ul>
<?php the_content(); ?>
</section>
<?php endwhile; endif; ?>
A couple of points:
You must remember to call the wp_reset_postdata(); function after you call setup_postdata($post); as otherwise other bits of the page won't work (as you're overriding the main page loop)
If you want to link to the club page you can move my piece of code to within the <li> tag and then use the_permalink(); as usual to output a link to the page (I've done it outside of your code to keep things clear).
Give me a shout if you're still stuck.

Related

Manually show specific posts on homepage

To loop and show all WordPress posts we use:
<?php
if( have_posts() ) :
while( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endwhile;
endif;
?>
And to show only the first recent post we use:
<?php
if( have_posts() ) :
if( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endif;
endif;
?>
My question is, can I manually select what posts I want to show on my homepage without doing a loop? For example let's say I want to only show post1, post3, and post6 on my homepage, can I do that?
Without using while loop because the post contain only one post.
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=>40'); ?>
<?php if (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endif;?>
<?php get_footer(); ?>
Try this code
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=40'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;?>
<?php get_footer(); ?>
The one I picked had an ID of 40. So, I set p=40. On the page I chose to use this on, I selected the "Query Single Post" post template. Clicked save and refreshed my page.
You need to use pre_get_posts filter.
Code:
<?php
add_action('pre_get_posts', function($query){
if ( !$query->is_home() or !$query->is_main_query() )
return false;
# And here you can use any parameters from [WP_Query Class](https://codex.wordpress.org/Class_Reference/WP_Query)
# For example
$query->set('post__in', [10, 15, 16]);
});

Wordpress Post content layout

I am building a custom theme from scratch and come across a slight problem that I need some help with.
So I have on my front-page a list of the 3 latest blog posts showing the 'title', 'Excerpt' & a 'more...' link which both the title and more link take you to single.php.
I am generating the post content in a file named 'content-post.php' which is the following:
<div class="clearfix">
<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>
<header class="title">
<h3><?php the_title(); ?></h3>
</header>
<ul class="info">
<li><?php the_category(', '); ?> | <?php the_time('F Y'); ?></li>
<!--<li>Written By: <a href="<?php bloginfo('siteurl') ?>/about/"><?php the_author(); ?></li>-->
</ul>
<div class="excerpt">
<p><?php if(is_single()): ?>
<?php the_content(); ?>
<?php comments_template(); ?>
<?php else: ?>
<?php the_excerpt(); ?>
<a class="post-link" href="<?php the_permalink(); ?>">More...</a></p>
<?php endif; ?>
</div>
</div>
This builds the posts out to front-page.php just fine. I am having the problem when you go into the blog page which is using the same post content and the layout is the same. Is there a way I can specify how it shows on the front-page and how it appears on the blog page?
The post is being displayed on front-page.php like this:
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', 'post' ); ?>
<?php endwhile; endif; ?>
And on the blog page like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'post' ); ?>
<?php endwhile; else: ?>
<p>There are no posts to display or pages here</p>
<?php endif; ?>
If I understand your question, try this:
<?php if (is_page(page id)){your query}
Good luck! ;)
PS.. or on your way:
<?php if (is_page(page id)): ?>
your query
<?php endif; ?>

Display posts that belong to certain category - Wordpress

I'm having trouble getting this to work. Can someone provide a quick snippet for category template that displays posts that belong to a category called 'Product A'. I've been using the trial and error method for the past 3 hours with no luck.
Thank you!
Here's what I've been playing around with -
<?php
/*
Template Name: yadayada
*/
?>
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php query_posts('cat=32&showposts=5'); ?>
<div class="post">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="post-description">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
</div>
You can used the WP_Query class.
One way I've done it before is by first creating a category name of Product-A and making the slug 'product-a' all lower case.
Then instantiate a new instance of the class. Pass in the parameter of 'category_name=product-a' You do no pass in the category name with this parameter, but rather the slug name. once you do that you should be able to use the WP_Query as follows:
<?php $my_query = new WP_Query( 'category_name=product-a' ); ?>
<?php if ($my_query->have_posts() ) : ?>
<?php while ( $my_query->have_posts()) : $my_query->the_post() ?>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="product-excerpt"><?php the_content(); ?> </div>
</article>
<?php endwhile; ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
pretty much everything is the same as the regular loop but instead of just
<?php if(have_post()) : while(have_post()) : the_post() ?>
You would used object notation to refer to this particular query.
<?php if($my_query->have_post()) : while($my_query->have_post()) : $my_query->the_post() ?>
hope it helps.
First get your Product A category id; (if you use, your cat id in your custom query it 's gonna work perfectly instead of category name.)
<?php
query_posts('cat=1');
while (have_posts()) : the_post();
the_content();
endwhile;
?>

Is my loop done wrong - WordPress

Last time I checked I knew how to write a loop to display all my posts.....How ever when I wrote this:
if (have_posts()){
while (have_posts()): the_post();
?>
<div clas="span6">
<h3><?php echo the_title(); ?></h3>
<p><?php echo the_excerpt(); ?></p>
</div>
<?php
endwhile;
}
I ran into an issue:
If I have the following posts:
Post1, Post2, Post3
Posts 1 - 2 will show up in a list, its only until I write a new post (Post 4) and publish it that post 3 will show up in that list.
So whats wrong with my loop?
never had this issue before.
Note: WordPress 3.5 is being used.
I have checked WordPress Docs to make sure I am doing things right and as far as I know I am.
I see a couple things that could be causing your problems. First, you have a set of { } that are not needed. Your open to the loop should be:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
Instead of:
<?php if (have_posts()){ while (have_posts()): the_post(); ?>
See the curly brackets in your loop?
Then as McNab said, just fix your div class by adding the "s" and remove the echo from content pullers.
You also need to to include an endif; after your endwhile;.
So your full loop should loop like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="span6">
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(); ?></p>
</div>
<?php endwhile; endif; ?>
I think this should correct your problem.
You can use it like below. This is mainly used in wordpress.
if (have_posts()) : // your code if (have_posts()){
while (have_posts()): the_post();
?>
<div clas="span6">
<h3><?php echo the_title(); ?></h3>
<p><?php echo the_excerpt(); ?></p>
</div>
<?php
endwhile;
endif; //your code here }
Hope it may work.

Wordpress - Multiple Post Queries

I am using the following code to query posts for categories:
<?php query_posts("cat=8"); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<article>
<h4><?php the_title(); ?> </h4>
<p><?php the_content(); ?></p>
</article>
<?php endwhile; ?>
It seems to work fine, until I did it a third time(three instances of the code above) on a single page. Now the page seems to load forever and it breaks as if it is compiling more then 1 page template. I should mention that all works fine unless I publish a post to the third category
Has anyone had a problem like this, or know why it happens?
Is this bad practise for querying posts?
Use WP_query instead so you can make use of the wp_reset_postdata which should clear up the issue.
<?php
$the_query = new WP_Query( 'cat=8' );
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<article>
<h4><?php the_title(); ?> </h4>
<p><?php the_content(); ?></p>
</article>
<?php
endwhile;
wp_reset_postdata();
?>

Categories