Im using advanced custom fields and have setup a custom post type for testimonials, on the testimonial page there is a relationship field (display_on_page) where you would select which page to display the testimonial on.
The issue is that the testimonial displays on everypage when a page is chosen
Does anyone know what I am doing wrong on the below please?
<?php query_posts( 'post_type=Testimonial'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $posts = get_field('display_on_page');
if( $posts ): ?>
<?php the_field('the_testimonial'); ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
If I understood correctly, you should check if the value that you get from get_field('display_on_page') matches the current page ID.
(I'm assuming that the page ID is what is stored by the custom field you created).
If that's the case, you may query your posts filtering by custom field values, like such:
<?php
// Fetches current page ID, assuming that this is page.php or similar
$current_page_id = get_the_ID();
query_posts(
'post_type=Testimonial',
'meta_key' => 'display_on_page',
'meta_value' => $current_page_id // Only gets Testimonials that are set for this page
);
while ( have_posts() ) : the_post();
the_field('the_testimonial');
endwhile;
// Resets default WP Post Data *AFTER* the loop is complete
wp_reset_postdata();
?>
Related
I was wondering if there is a way to change this code to only display posts from certain category created in wordpress. Now it displays every recent post. Let's say I would create "News" category in wordpress and I want this piece of code to display only News posts.
Thanks for help
<?php
if( have_posts() ){
while( have_posts() ){
the_post();
get_template_part( 'template-parts/content', 'koncert');
}
}
?>
You can override the usual query that wordpress uses and create a custom one;
https://developer.wordpress.org/reference/classes/wp_query/
usually though for just displaying a category you can just open the category slug and provided your template has the correct archive page it will display the posts for that category.
If not use something similar to below. You can further refine your search parameters like number of posts and post types as defined in the link above.
<?php
//refine your query to the category you desire either a slug(example below) or category id
$args = array(
'category_name' => 'my_category_slug',
);
//create the query using the arguments
$query = new WP_Query($args);
?>
//create the loop to show the posts
<?php if($query->have_posts()): ?>
<?php while($query->have_posts()): $query->the_post(); ?>
<h1><?php the_title(); ?></h1>
<div><?php the_content(); ?></div>
<?php endwhile; ?>
<?php endif; ?>
I want to display only one author's post say "abc_author" .
i have use the validation using if clause but THE PROBLEM is that i get pagination below the post where page 1 is blank (as an other author post is skipped) , page 2 (as an other author post is skipped) is blank and soon i get my desired post on page 6 . commenting the verbosa_pagination() hide the pagination navigation but the starting pages are blank and
I am a new to WordPress , i have also tries different plugin but they facilitate me to filter it on pages not post (though i can change the home page to a particular page but i want it on default blog post for few reasons )
<?php if ( have_posts() ) : ?>
<div id="content-masonry">
<?php /* Start the Loop */
while ( have_posts() ) : the_post();
if(get_the_author()=='abc_author')
{
get_template_part( 'content/content', get_post_format() );
}
endwhile; ?>
</div> <!-- content-masonry -->
<?php verbosa_pagination();
else :
get_template_part( 'content/content', 'notfound' );
endif;
Can you please try this code before the while loop?
<?php query_posts( array( 'author' => 'your author ID' ) ); ?>
Ref: https://developer.wordpress.org/reference/functions/query_posts/#usage
I have created a custom post type based on CPT Ui I am tryin to display the items of that in a template ideally what I want to be able to do is
www.siteurl.com/profile/profilename
Hence why I have created a template page with the following code.
But I dont no how I would get profilename into my query posts and then display the relivent data this is what I have tired so far to get id and permalink to display but to no avail.
I really want to be able to do it like how the documentation states. Ideally i would suspect my best approach would be to check if user has created a profile in the front end if not direct them to do so?.
Display a field
<p><?php the_field('field_name'); ?></p>
http://www.advancedcustomfields.com/resources/code-examples/
<?php
/**
* Template Name: Profile Page
*/
get_header(); ?>
<?php get_template_part( 'page', 'title' ); ?>
<div class="width-container">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'profiles',
'meta_key' => 'profilename',
'meta_value' => 'david'
));
if($posts)
{
echo '<ul>';
foreach($posts as $post)
{
echo '<li>' . get_the_title($post->ID) . '</li>';
}
echo '</ul>';
}
?>
<?php get_sidebar(); ?>
</div><!-- close .width-container -->
<?php get_footer(); ?>
If anyone has done anything simlar maybe I should be using current user but again i dont no how to link that up to acf as I am new to that plugin
Perhaps you could try another approach and build a custom query with WP_Query instead. The example bellow use the WP_Query object rather than the get_posts function, however the arguments and logic remain the same.
This implies we will find all posts that have a post_type of profiles where the ’custom field’ profilename is equal to david. The custom field ‘profilename’ in this case could be a text field, a radio button or a select field (something that saves a single text value).
<?php
/**
* Template Name: Profile Page
*/
get_header();
$user = get_post_meta($post->ID, 'profilename', true); // saving the field value in a $variable can help to build a more dynamic WP_Query...
?>
<?php get_template_part( 'page', 'title' ); ?>
<div class="width-container">
<h1>__('Listing Posts by:', 'text_domain');</h1>
<?php echo '<p class="text-center">' . esc_html( $user ) . '</p>'?>;
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'profiles',
'meta_key' => 'profilename',
'meta_value' => $user // retrieve the actual profilename field value
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
<?php get_sidebar(); ?>
</div><!-- close .width-container -->
<?php get_footer(); ?>
The code above is an example of how to query ("get and display") posts in WP using WP_Query object within a template based on custom field called ‘profilename’ which has a value of ‘david’ or whatever data is saved inside the field as per your example.
Perhaps you want to display the relevant data (values) of the custom fields you've created for the WP default user profile page, and if this is the case, you should follow another approach and instead of query posts you simply output the fields a user might fill on their profile. In order to that you could Get values from a user.
This example will display the field 'profilename' value from a user with an ID of 1.
<?php the_field('field_name', 'user_1'); ?>
Within your loop you could use $user_ID = get_current_user_id(); to retrieve the user_id as follows:
<?php
/**
* Template Name: Profile Page
*/
get_header();
$user = get_post_meta($post->ID, 'profilename', true); // saving the field value in a $variable can help to build a more dynamic WP_Query...
$user_ID = get_current_user_id();
?>
<?php get_template_part( 'page', 'title' ); ?>
<div class="width-container">
<h5>__('Listing Posts by:', 'text_domain');</h5>
<h1><?php the_field('profilename', '$user_ID'); ?>;</h1> // this will retrieve the data/value saved in the 'profilename' field for the current user
...
Check ACF's Query Posts by Custom Fields documentation for other ways to retrieve an array of post objects from the database using native WP functions.
I am building a personal website to host my university work, personal projects and photos etc.
The menu is a hierarchical structure made up of pages and links. Take my university pages for example. What I would like to achieve is to display posts that are related to the module code which is the page's slug.
I've used the following link http://codex.wordpress.org/Page_Templates#A_Page_of_Posts and managed to get it working but I have hard coded the module code into the template, meaning for each module I will have to have a separate template and the only thing that will be different from one file to the next is 5 characters which isn't great for code re-use.
What I am asking, is, is there a way to get the slug from the page I'm looking at and use that for the WP_Query arguments.
If you go to http://michaelnorris.co.uk/ and look at the menu structure. Navigate to University -> Year Three -> Individual Project, you will notice the url is http://michaelnorris.co.uk/uni/three/ci301 where ci301 is the module code for the Individual Project. I want to have this system on each of the module pages so that I can tag posts and they are displayed in the relevant module.
Ok, I actually found the answer myself, but for others looking to do the same. Below is a solution.
Solution found here on the Wordpress.org Codex http://codex.wordpress.org/Page_Templates#A_Page_of_Posts
Name the file pageofposts.php and edit the Page within the Wordpress Dashboard and set the Template (in the dropdown) to 'Page of Posts'. Bingo!
<?php
/*
Template Name: Page Of Posts
*/
/* This example is for a child theme of Twenty Thirteen:
* You'll need to adapt it the HTML structure of your own theme.
*/
get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
/* The loop: the_post retrieves the content
* of the new Page you created to list the posts,
* e.g., an intro describing the posts shown listed on this Page..
*/
global $post;
$slug = get_post( $post )->post_name;
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Display content of page
get_template_part( 'content', get_post_format() );
wp_reset_postdata();
endwhile;
endif;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
// Change these category SLUGS to suit your use. category_name is comma separated.
'tag' => $slug,
'paged' => $paged
);
$list_of_posts = new WP_Query( $args );
?>
<?php if ( $list_of_posts->have_posts() ) : ?>
<?php /* The loop */ ?>
<?php while ( $list_of_posts->have_posts() ) : $list_of_posts->the_post(); ?>
<?php // Display content of posts ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentythirteen_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
Hello I want to Create a home page that has updating blog entries.
So 4 lists of headlines from different categories
And I want to have a link to the regular blog page with a different template.
Right now I just changed index.php around to have the containers for the featured posts content.
So this is a two part question how do I get these mini updates for the thing
I want to use query_posts() multiple times I assume and separate by category.
And how do I make a linkable page to a blog.php file which currently is telling me that all these functions are undefined.
<?php get_header(); ?>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer(); ?>
If you want to create a page that includes WP data/posts outside the themes or blog folder, you need to include and make available the Wordpress functions first:
define('WP_USE_THEMES', false);
require('./blog/wp-blog-header.php');
And then you can make the queries for each set of posts by category:
$args = array( 'numberposts' => '5, 'offset'=> 1, 'category' => 'your category ID' );
$myposts = get_posts( $args );
foreach($myposts as $post) {
...some code...
}
I hope it helps.