Adjust Wordpress function to grab author ID of post - php

I use a point system plugin for Wordpress. By adding this code to the author.php page:
<?php cp_displayPoints($authordata->ID); ?>
It will echo X Points. This is the points of that respective author. When I add the same code to single.php (post page), it echos the logged in user's points, and if not logged in, it returns blank.
How can I alter this code so that it will function properly on the single.php page too? This would mean that it would echo the points of the author of that post.

Just call get_the_author_meta from within the loop.
So, you just need to test if you have a currently signed in user, if not use the post author instead. Something like this.
<?php
if(!$authordata->ID)
cp_displayPoints(get_the_author_meta('ID'));
else
cp_displayPoints($authordata->ID);
?>
EDIT:
To display only the post author's ID, just use
<?php cp_displayPoints(get_the_author_meta('ID')); ?>

Related

Wordpress, show or hide header on posts by post categories

Hi i have an wp website and i'm using avada theme. I want to hide header according to its category. For example if posts category is "A" then i dont want to show header on the top automatically. I know i can manually adjust that in the post settings on every posts, or set a layout and select that layout on the below but i dont want to deal that every time.
So i try a if statement in the archives.php.
its something like this:
<?php if(has_category( 'A' )) : ?> //i also try in_category and on_category
<?php else: ?>
<?php get_header(); ?>
<?php endif; ?>
but its probably for category page itself but not for posts.
Can anybody help with that?
Thanks :)
hi you can go with following links and follow the given solution it may worked..
here it show that how to hide specific c***ategorize header post*** ...
https://wpquestions.com/Remove_header_or_styling_from_specific_category_of_posts/11505

Display custom field from parent page

On a child page x, i want to output a custom field from the parent page
<?php the_field('imagecategory'); ?>
The child page can also have this field (but i dont want to grab that)
so what i want is:
On the child page, grab the parent page id. And for that specific page show the output of the code
<?php the_field('imagecategory'); ?>
I asume i need to grab the parent id with $post->post_parent;, but i got no idea how to tell the code to grab that custom field for that specific page :(
Aparently you are using Advanced Custom Fields (ACF) plugin right? If yes, you must to get your custom page where you set your custom field before to show it on a different page, something like this:
<?php $mypage = get_page_by_title('my page'); ?>
<p><?php the_field('custom_field', $mypage); ?></p>
I hope it helps.

Buddypress get full name print to new wordpress page

Referring to How to get username/display name in Buddypress?
I have tried putting this code to an added new page
<?php
$string = bp_get_displayed_user_fullname();
echo $string;
?>
But when i tried previewing the page it prints empty only. Is there something I missed on the plugin to make it working? TIA
bp_get_displayed_user_fullname() will work only on pages, that are displaying some user. That means your profile or profiles of other users. On all other pages you will always see an empty string (forums, ordinary WordPress pages and posts, groups etc).
To display a name of any user on any page you need to use something like bp_core_get_user_displayname( $user_id ), where $user_id is, obviously, the ID of a user you want the name of.

will do_shortcode() allow me to run shortcode in content before a line in the template?

G'day folks, I'm attempting to integrate Ajax Availability Calendar into a WordPress site via PHP.
I have multiple calendars set up, and need to display a different one on each property page.
The problem I'm having is that I need to be able to specify the ID e.g. $_GET["id_item"]=2 of the calendar before the <?php require_once 'pathtofile' ?> statement which is in the template.
The result I need to achieve is...
<?php $_GET["id_item"]=2; require_once 'path\to\required.file'; ?>
If I put this code in the template with the appropriate ID, it works, but I need the ID number to change on each page, otherwise I'll always be displaying the calendar with ID number 2.
I currently have custom shortcode (below) with which I can specify the ID in the page content, but I am at a loss as to how to access that ID before the statement in the template.
So what I currently have is...
In Functions.php
function ajaxcalendar_shortcode($atts) {
$args = shortcode_atts(
array(
'id_item' => 1
),
$atts
);
return '<?php $_GET["id_item"]='.$id_item.'; ?>';
}
add_shortcode('cal_display', 'ajaxcalendar_shortcode');
with the shortcode [cal_display id_item = "2"] in the content.
Is it even possible to do this or should I try another approach?
My research suggests that I may be able to use do_shortcode() to achieve this, but I haven't figured out how to use it in this situation, or I may be misinterpreting it's purpose.
I've tried putting this in the page template file:
<?php $content = the_content(); echo do_shortcode($the_content); ?>
<?php require_once 'C:\xampp\htdocs\MooreRiver\availability\ac-includes\cal.inc.php'; ?>
But it continues to display the calendar with ID 1.
Any tips would be appreciated :)

How to have different logic for a page and a post within Wordpress?

I am modifying a wordpress template and need to slightly separate rendering logic for a post and a page, specifically in relation to how the date renders out. The problem is that I cannot find any code to do this, I am sure that it exists. Is there a variable that exists within wordpress that tells me whether the item being displayed is a page or a post?
In an ideal world it would look something like this:
<?php if (is_page()) : ?>
page logic
<?php else: ?>
post logic
Would appreciate any help!
Pages are a type of post, so get_post_type should return appropriately different values for pages vs normal blog posts.
I found this link: http://wordpress.org/support/topic/sidebar-logic-for-postblogroll-and-page-type which seemed to do the business for me.
The answer (copied straight from the page) was:
<?php if(is_singular($post)): ?>
Page Content
<?php else:?>
Post Content
<?php endif;?>

Categories