I am using openDoor WordPress theme. In this theme we have two custom post types.
1) Listing
2) Agents
While adding Listings to website we have an option to select Name of Agent from DropDown, and it will show details of the Agent on Listing Page : Listing Page
Now when I try to display Agent details on print_post.php (A file from wp-print Plugin)
I use this code to Display Name of agent and it worked for me
<?php echo "Agent Name:"; global $post; echo get_post_meta($post->ID, "agent_value",true); ?>
But I am unable to display Phone numbers of Agent.
Please help me what code I should use to display Phone Number ?
if you have already value of agent phone number in database( in postmeta ), then you can fetch in the same way, as you fetch agent name
<?php echo "Agent Name:"; global $post; echo get_post_meta($post->ID, "agent_value",true); ?>
you must know post meta name, that you saved in postmeta.
Hope this helps
Cheers!!!
Related
I have returned a list of registered users. I am looking to be able to query these results and also generate a click through page. Is that possible.
Im trying to essentially get a list of users by searching (first name for example), then click through to show information about them, on the front end. Its mainly the click through im having difficulty with.
For example if i could click a member and it would go through to a page that had the user first name, last name etc.
<?php
$blogusers = get_users();
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
if ( in_array( 'team_member', (array) $user->roles ) ) {
?>
<div class="member_user">
<a href="<?php echo the_permalink();?>">
<?php
echo $user->first_name." ".$user->last_name;
?>
</a>
</div>
<?php
}
}
?>
If i understood correctly, you already have a list of users, and you want each one of them to be linkable to its own page, where you would display the info you want, such as first name, last name etc.
Firstly you have to query for your users and loop through them to display them in a list. You already have this in your code, but if you want something more advanced a query would look like this:
$authors = get_users(array(
'role__not_in' => array('Subscriber', 'Administrator'),
//if you do not want to list the admins and subscribers
'orderby' => array(
'post_count' => 'DESC',
//order users by post count (how many posts they have)
)
));
foreach ($authors as &$author){
//loop through the users
$user_info = get_userdata($author->ID);
$user_link = get_author_posts_url($author->ID);
?>
<a href="<?php echo $user_link; ?>" style="display:block;">
<?php echo $user_info->first_name.' '.$user_info->last_name; ?>
</a>
<?php
}
Then you need to create the page for the user. WordPress handles this by author.php page. If your theme does not have one, simple create a php file called author.php and place it at the root folder of your theme.
You may edit author.php to include the data you want. However if you remove the list of posts that are displayed by default for the selected user you will loose this functionality in your theme.
So inside author.php you may get the user like so:
//get current user (object):
$current_author = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
//get User data by user ID
$user_info = get_userdata($current_author->ID);
//echo first & last name:
echo $user_info->first_name.' '.$user_info->last_name;
If you make a print_r($user_info) you may see what info you can display for the selected user.
You may also use the 'get_the_author_meta' function to get more info from the user's profile, eg
get_the_author_meta('description', $current_author->ID);
You can find more info about this function here: https://developer.wordpress.org/reference/functions/get_the_author_meta/
I would generate a link like this:
<?php
echo $user->first_name." ".$user->last_name;
echo 'view user';
?>
And then query the user ID on the next page to get the desired information.
On the next page you can:
<?php
$user_ID = $_GET['user_id'];
get_header();
?>
and then query this to get pretty much any content related to that user
Two and a half approaches:
You could customize your theme's author archive template to display the desired information. Three author archives are available in the template hierarchy:
author-{nicename}.php
author-{id}.php
author.php
You'd be able to link to https://{site_url}/author/{username} for each user.
However, this may not be ideal if you want to keep that normal author blog archive and add the new profile pages. So what may be better long term is to setup a custom post type for the member profiles, then every time a new "member" user is created run a function that creates a new post representing the user.
You can register that custom post type manually with register_post_type() or if you need help a good plugin is Custom Post Type UI.
A good action for your function to hook into is user_register since it happens immediately after user registration but will have access to user data. Within your function you can create the new post using wp_insert_post().
You can then display the information of that custom post type using the archive-$posttype.php and single-$posttype.php templates.
2 1/2. You may also consider using only the custom post type and not querying for users at all. If this member profile is the only reason some folks are users it may even be more convenient this way. Then you can use the archive-$posttype.php and single-$posttype.php templates easily to display the information you choose.
There is a function for that. Use get_author_posts_url()
In the required template use the following code:
// only return required user based on role
$args = array(
'role' => 'team_member'
);
$blogusers = get_users( $args );
// loop though and create output
foreach ( $blogusers as $user ) : ?>
<div class="member_user">
<a href="<?php echo get_author_posts_url( $user->ID );?>"> // links to author page
<?php echo $user->first_name." ".$user->last_name; ?>
</a>
</div>
<?php endforeach;
You will then need to customise the author.php template in your theme to render as required.
I made two custom post types with WP types plugin
Property (child)
Agent (Parent)
In Agent(Parent), i have created custom post fields with WP types Plugin:
Email
Phone
Fax No.
Now I want to show the fields in Property(Child) Posts.
I am using this code in single-property.php.
$parent_id = wpcf_pr_post_get_belongs($post->ID, 'agent');
$parent = get_post($parent_id);
$agentPhoto = wp_get_attachment_image_src(get_post_thumbnail_id($parent->ID), 'agentPhoto');
//successfully get the parent photo
<img src="<?php echo $agentPhoto[0];?>" alt="<?php echo $parent->post_title;?>">
//successfully get the title of the parent
<?php echo $parent->post_title;?>
I am able to get the Title and Featured Image of Agent(Parent) but I can't get the custom fields which is (1. EMail, 2. Phone, and 3. Fax No.).
Below is the code for custom field through which i can get the fields but only in assign post types which is Agent(Parent).
$agentPhone = types_render_field( "agent-phone" );
$agentEmail = types_render_field( "agent-email" );
$agentFax = types_render_field( "agent-fax" );
Any help appreciated thanks.
use this sortcode to get value of agentPhone , email etc...:and in place of id pass the parent post id:
<?php echo(do_shortcode('[types field="adresse" id="746" arg1="val1" arg2="val2"]')); ?>
<?php echo(do_shortcode('[types field="agent-email" id="746" arg1="val1" arg2="val2"]')); ?>
<?php echo(do_shortcode('[types field="agent-fax" id="746" arg1="val1" arg2="val2"]')); ?>
I currently have an advanced custom field setup in the user's profile with a field name of author_title. This would display what the user does at the company blog.
I currently have the following working but the title only updates for first user and all users get that title instead of being able to use their own.
Updated
<?php $current_user = wp_get_current_user(); ?>
<h3><?php the_field('author_title', 'user_' . $current_user->ID); ?></h3>
Try to get user ID in another way.
Now you get ID for currently logged in user so it shows the same field everywhere. I assume you need show that filed for author posts so:
$author_id = get_the_author_meta('ID');
$author_field = get_field('author_title', 'user_'. $author_id );
I have a custom page named 'Journal', which I use as a blog index page for my wordpress website. I've run into a rather strange problem. When I enter <?php echo get_the_title(); ?> or whatever in home.php, it returns the title of a post, instead of the page title 'Journal'. Is anyone familiar with this problem?
Thanks!
This is the expected behavior for this page. When you set a page to be your "blog", you can't access the template tags for that page. Instead, the template tags are for the loop of the posts to be displayed on that page.
To get the title, you have to first get the id of that page, and then you can pass it to a function:
<?php
$page_for_blog = get_option( 'page_for_posts' );
$page_title = get_the_title( $page_for_blog );
?>
Now you can print the $page_title and you should see "Journal".
Updated with Advanced Custom Fields
Now that you have the Journal page's id ($page_for_blog), you can get your field values with:
$field_value = get_field( 'field_name', $page_for_blog );
Obviously, replace 'field_name' with whatever field you're trying to retrieve.
if been searching for hours :-( Hope anyone can help me.
I'm in a custom post type called "grhdogs". There I need to fetch the current page title of that page and search with that title in another custom post type called "slideshow" and echo (as string) the page ID of the matching pagetitle in "slideshow".
The background is that in both custom post types are always matching page titles. I have to bring them together because "grhdogs" is a post and "slideshow" is the matching slideshow that I want to display in the post.
You can try some thing like this.
<?php
//get the current title of the page
$postTitle = get_the_title();
//search for similer post in db table
$postID = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type='slideshow' AND post_title = %s",$postTitle));
//print id
echo $postID;
//or use get_post($post_id) to get whatever you want
$getCustomPost = get_post($postID);
echo $getCustomPost->id;
?>