I've created a custom posts called Projects and created a Projects template inside of archive-projects.php. I've also created a single-projects.php to display individual project. However, this became a problem when I had to use an ACF on the Projects template. I want to display a featured project on the Project page and also list out few of the posts I've created for projects on the same page. Below is my code to get featured project image.
<?php echo get_field('featured_project'); ?>
<?php
$featured_project = get_field('featured_project');
if ($featured_project):
$project = $featured_project;
setup_postdata( $project );
if ( has_post_thumbnail($featured_project->ID) ) { // check if the post has a Post Thumbnail assigned to it.
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($featured_project->ID), 'large' );
$featured_image = $featured_image[0];
}
?>
I've read few articles and similar questions on Stackoverflow but haven't found an answer yet. Has anyone been successful figuring this out?
You can try to use my plugin ACF CPT Options Page
That creates options page for each custom post type, then you can add Relationship Field to get posts easily
Related
Hi I have created with CTP custom fields plugin a custom field to appear only in taxonomy -product category- this field, appear in the back end when I edit the category.
Field is intended to be there to upload images and output the URL of the image like this:
<div class="page-heading" style="background-image:url('<?php the_field('field_name'); ?>') !important;">
This kind of code works perfectly OUTSIDE woocommerce (in the normal WP pages and posts) but for some reason, doesn't show anything, even that in the back end I can see the image attached to the category.
In the front end it shows like a empty field...
Im not sure what i'm doing wrong.
If you're not in a WP loop you have to explicitly point to the post you want to get the field from of using an ID:
the_field( 'field_name', $post->ID );
Thanks, your tips made me search the right thing and found the answer:
<?php
$term_id = get_queried_object()->term_id;
$post_id = 'product_cat_'.$term_id;
the_field('the_name_of_the_field', $post_id);
?>
I can try this code in page text but not working
<?php
$post_id = '138';
echo get_post_field('post_content', $post_id);
?>
show a code only not display content,
provide solutions in which file I can add this code.
wordpress provide insert_pages plugin and in this plugin in page add post or another page title, content etc
I have a plugin that automatically expires posts and changes it's post status to "archive" on a certain date.
We then have an archive section that houses all these posts. I still want to display the data for the post but it gives a page not found.
Can I alter the query to display this post on the single template?
I've tried the following but it pulls in all archive posts rather than just the page you're on.
<?php
$my_query = new WP_Query('post_status=archive');
?>
<div>
<?php
if ($my_query->have_posts()) : while ($my_query->have_posts()) :
$my_query->the_post();
?>
<ul>
<li>
<?php the_title(); ?>
</li>
</ul>
<?php endwhile; else: ?>
<div>
<ul>
<li><?php _e('No upcoming Posts'); ?></li>
</ul>
</div>
<?php endif; ?>
</div>
Post status allows users to set a workflow status for a post in WordPress.
There are 8 default statuses that WordPress uses. They are published, future, draft, pending, trash, auto-draft, and inherit.
A post may also have a new status if it was just created and hasn't had any previous status.
WordPress themes and plugins can also define custom post statuses for more complex websites. These statuses allows users to organize their posts in the admin panel. They are especially useful for sites with multiple authors or a complicated editorial process.
Some things that post status allows is for users to work on an article without publishing it and saving it as a draft. This way they can go back later and finish it. It also allows users to schedule posts which gives the post a status of future, or make a post private. Attachments have a post status of inherit. For multi author blogs the pending status can be useful as contributors can submit posts for review prior to publishing.
In your case, you have to be 100% sure that your archived posts get status archive, otherwise your WP query will not work, so check if this plugin you use set this post type (the easy way is to look directly in MySQL database, table wp_posts or, if you do not have access to the MySQL server via PHPMyAdmin - then look at the source code of the plugin itself.).
EDIT: Can you try, just for testing, to change your WP query like this:
$my_query = new WP_Query( array( 'post_status' => array( 'pending', 'archive', 'publish' ) ) ); and see the result?
The reason why you get all archived posts is because you're querying an entirely new query with the post_status=archive.
To solve this, I've been trying with some code to utilize WordPress Action API like pre_get_posts but it doesn't work properly for me mainly because WordPress has already thrown a 404 error.
So it might be a better idea to override this in the template like in single.php.
<?php
$post_id = get_query_var('p');
$args = array(
'p' => $post_id,
'post_status' => array( 'publish', 'archive' )
);
$my_query = new WP_Query($args);
?>
I might suggest to try this out first. As I mentioned, you were getting all the posts in archive. In this case, you are getting the post ID and adding that into the query. Therefore, you'll be getting on the current post and with publish and archive post_status so that all posts with both post_status will be queried.
Unfortunately, in my testing, it just doesn't work on visitors' who aren't logged in. It'll work for you who's logged in.
Let me hear about the result in the comment.
Display post by ID:
$query = new WP_Query( 'post_status=archive&p=7' );
Display page by ID:
$query = new WP_Query( 'post_status=archive&page_id=7' );
Thanks to #josephting & #bodi0 for getting me thinking in a different way, actually solved it without adjusting the single template from the original loop by using this (I'd used this on another site to get future posts to show):
add_filter('the_posts', 'show_future_posts');
function show_future_posts($posts)
{
global $wp_query, $wpdb;
if(is_single() && $wp_query->post_count == 0)
{
$posts = $wpdb->get_results($wp_query->request);
}
return $posts;
}
With all other solutions either it was still saying page not found or wasn't displaying the data fully.
I would like to add a feature to a WordPress page to display a short list of people, like the one seen here - http://www.blenderbox.com/company/team
I have done this on other sites by styling lists with css. This time however, it's for a client and I can't trust them to copy and paste a <li>, editing the name, job title and img name without messing it up.
What is the best way to go about creating this so that it can be easily reduced/added to in the WordPress dashboard? Basically, so that the user simply clicks "add person" then fills in some fields before updating the page.
I'm new to WordPress (though I understand how it works) but I am competent coding so hopefully an informative nudge in the right direction would be sufficient.
Thanks, Ben.
Use categories. Create a new category called Person or Team or something to that effect. Then use the Post Title for their Name - any other information can be put in the post and then add the image to the Featured Image.
Create a new template for this page by copying over another template file and giving it a header like this to name it.
<?
/*
Template Name: About Us / Team / Whatever
*/
?>
And in the page call only the information you want with something like this:
//Set up the Loop
<?php
global $post;
$args = array (
'category' => 4,
'order' => 'ASC');
$teamposts = get_posts( $args );
foreach( $teamposts as $post ) : setup_postdata($post); ?>
//Call the information you want - put them in <li> if you need
<?php the_title();?>
<?php the_excerpt();?> //OR the_content
<?php the_post_thumbnail('new-image-size');?>
//Close the loop
<?php endforeach; ?>
You can then go on to call anything else you want or run another loop on the same page to call other information.
To get the right sized thumbnail you have called 'new-image-size' - to set this up add a line like this to your functions.php file.
// Post Thumbnails
add_theme_support( 'post-thumbnails' );
// name of the thumbnail, width, height, crop mode
add_image_size( 'front-page-services', 450, 270, true );
I am having users of a Wordpress blog upload a single image to different post's galleries with the name "banner". Basically, every post will have an image named "banner" uploaded to its gallery and this image needs to be displayed on the post's page outside of the article content. So, how can I get the URL to display an image in a post's single.php template?
Can I iterate through the images of a given post's gallery and find the one with the correct title somehow?
I've searched through the Wordpress codex docs and haven't found anything on a way to do this, just information on displaying galleries of photos. Note that I'm already using Wordpress's post thumbnail feature for something else.
Thanks!
I think I finally figured it out. The part I couldn't figure out was using the vaguely named get_children() function.
<?php
have_posts(); //must be in the loop
the_post(); //set the ID
$images =& get_children(array('post_mime_type' => 'image', 'post_parent' => get_the_ID()));
rewind_posts(); //rewind for the actual loop
$image_url = null;
foreach($images as $image) {
if($image->post_title == 'banner') {
$image_url = $image->guid;
break;
}
}
?>