From the front end, I want to allow the author to pick up the best comment/answer of his post. I am close, but not there yet. Every time I click the button, all comments of that post are selected as best (this is wrong). It should only select one comment per post.
In function.php I have the following:
add_action ('comment_post', 'add_comment_field', 1);
function add_comment_field($comment_id) {
add_comment_meta($comment_id, 'bestanswer', 'no', false);
}
In comment.php I have the following:
<?php
$current_author = get_the_author_meta('nickname');
global $current_user; get_currentuserinfo();
if ($current_user->nickname == $current_author) {
?>
<?php
$my_post_meta = get_comment_meta($comment->comment_ID, 'bestanswer', true);
if ( $my_post_meta == 'yes' ) {
echo '<h4>Best answer</h4>';
} else {
?>
<form method="post" action="" id="bestanswer-<?php comment_ID() ?>">
<input type="submit" name="confirm" value="Best Answer" />
</form>
<?php
if (isset($_POST['confirm'])) {
update_comment_meta($comment->comment_ID, 'bestanswer', 'yes');
}
}
?>
Thoughts? Suggestions? Any help will be appreciated.
I solved this by adding a link to each comment. The link goes to a page that displays that comment. On this page, the author then selects the comment as best answer. Now the comment meta is only saved for that particular comment. I know is an extra step, but I prefer this method since the author has to make sure that that is the comment he wants to pick. Once the comment is selected as best, author cannot change it.
Related
I couldn't find an answer to this question anywhere and I have a hard time believing I'm the only one who has had this problem.
My title function inside my title tag of header.php, <?php wp_title('|', true, 'right'); ?>, is returning the page title but not the site title.
Example: About Us |
Why would it not be returning the site name??
Looks like wp_title() does not return the site's title. Sorry, hopefully someone will benefit from this answer.
I've never tried combining the entire title inside one function. Instead I use multiple calls:
<?php wp_title(''); ?><?php if(wp_title('', false)) { echo ' |'; } ?> <?php bloginfo('name'); ?>
I'm updating a legacy site for a client. It was a very old wordpress from 2009. A lot of the code is mixed with static and dynamic.
The sidebar changes with different pages depending on the page template.
This is the current side bar code
<div id="sidebar">
<?php
$pageID = $_GET["page_id"];
$sidebar = get_post_meta($pageID, 'sidebar', true);
if(is_single()){
$sidebar = 'Blog';
}
if(!dynamic_sidebar($sidebar)){
echo "Could not find sidebar";
}
?>
</div>
When I change the permalinks to prettypermalinks it breaks the sidebar and get this error "Could not find sidebar"
When I change the permalinks back to default it works again.
Does anyone have any idea of the issue? Thanks
This is likely due to the $_GET request that is specifically looking for the page ID instead of something like the slug, or whatever is in the query string after you updated the permalinks.
Try this instead.
<div id="sidebar">
<?php
$pageID = get_the_ID();
$sidebar = get_post_meta($pageID, 'sidebar', true);
if(is_single()){
$sidebar = 'Blog';
}
if(!dynamic_sidebar($sidebar)){
echo "Could not find sidebar";
}
?>
</div>
Also for future reference, the "Could not find sidebar" message is not really an "error" - It's just the output you're getting from the last "if".
Having an issue whereby the checkboxes inside a metabox aren't saving. I can save one value and have it return the value of checked to the checkboxes, just not multiple values, which I need. I've tried adding a foreach loop to the update_post_meta code but it didn't work. Slightly confused as to where I'm going wrong.
checkbox code is:
$areas = $wpdb->get_results("SELECT * FROM locations ORDER BY locationName ASC");
if( count($areas) ) :
?>
<div id="locationAssignedBoxes" size="1">
<?php
foreach($areas as $area) :
?>
<input type="checkbox" id="locationAssigned" name="locationAssigned" value="<?php echo $area->id; ?>"<?php if(get_post_meta($post->ID, 'locationAssigned', true) == $area->id) { ?> checked="checked"<?php } ?> /> <?php echo $area->locationName; ?><br>
<?php
endforeach;
?>
</div>
<?php
endif;
?>
Update_post_meta code is:
update_post_meta($post->ID, 'locationAssigned', $_POST['locationAssigned']);
Thanks very much!
This isn't a cut-and-paste answer to your question, but it should help. (I'm trying to solve a similar problem.)
Your problem in general: update_post_meta() saves a single meta value, not a collection of values. Because you want to save the values of multiple checkboxes, you have two choices:
Call update_post_meta() once for each of the checkboxes, creating a
collection of meta values associated with the post.
Combine all of the checkbox values into a single string and save those as a single value with one update_post_meta() call.
These two earlier questions are related and might point you in the right direction.
Save checkboxes of a metabox with a foreach loop (invalid argument)
Listing Pages With Checkboxes In a Metabox (and saving them)
I apologize if this is answered elsewhere, but I'm having an issue with this ACF code here: http://goo.gl/9onrFN I want the client to be able to add a portfolio site link (if applicable) to the artist page and the link would say "View Artist's Website" and the link would take the user to the artist's site in a new window. How would I go about not making this text not visible, unless there was a url entered into the custom field on the post? Here's the code:
<p><?php the_field('contact_phone_number'); ?><br />
or <?php the_field('contact_email'); ?><br />
View Artist's Website</p>
Thanks in advance!
You can check if a ACF field is set with:
if(get_field('artist_website')) {
the_field('artist_website');
}
Using the_field will simple echo the contents of your field, whereas get_field will return the value which is a lot more helpful. For example you could write the above code as:
Note: get_field simple returns the value of the field, if you want to check if a valid url has been entered you will have to use a regular expression.
Below is your code with an if statement performing an empty field check:
<p>
<?php the_field('contact_phone_number'); ?><br />
or <?php the_field('contact_email'); ?>
<?php if(get_field('artist_website')) { ?>
<br />View Artist's Website
You may find your code easier to read by pre-setting your variables and including HTML in the echo:
<p>
<?php
$contact_phone_number = get_field('contact_phone_number');
$contact_email = get_field('contact_email');
$artist_website = get_field('artist_website');
echo "{$contact_phone_number}<br />";
echo "or <a href='mailto:{$contact_email}'>{$contact_email}</a><br/ >;
if($artist_website) {
echo "View <a href='{$artist_website}' target='_blank'>Artist's website</a>";
}
?>
</p>
I am not sure where to start, but I want to add in a symbol or change the css for the comments for registered users. Show a difference between non registered user comments and registered user comments.
How would I go about adding this to my wordpress website?
<div class="commentdetails">
<p class="commentauthor"><?php comment_author_link() ?></p>
<?php if ($comment->comment_approved == '0') : ?>
<em>Your review is pending approval by gsprating staff.</em>
<?php endif; ?>
<p class="commentdate"><?php comment_date('F jS, Y') ?>
IP/Hostname: <small>(coming soon)</small>
<?php edit_comment_link('Edit Comment','',''); ?>
</p>
I want to add make it so that the entire class is a different color if the user is a registered logged in user.
Here's an all php version of Saladin's code using the standard if/else sysntax:
<?php
if ($comment->user_ID) {
echo "<div class='comment_registeredUser'>";
}
else { // The user is not logged in
echo "<div class='commentdetails'>";
}
?>
Putting all the code in php has fixed execution errors for me. Of course, that may have been because I was doing something else wrong.
As comments are displayed in the wp_list_comments() function, you will need to edit the code there. The easiest way to achieve this is to use a simple if/else statement checking whether or not the comment has a user ID associated with it. If it does, that means the comment was made by a registered user.
Of course, as well as this, you will need to create a new CSS class to give the distinction. Here is some example code:
<?php if($comment->user_ID) : ?>
<div class="comment_registeredUser"> <!-- Or whatever you decide to call the CSS class -->
<?php else : ?> <!-- The commenter isn't a registered user -->
<div class="commentdetails">
<?php endif; ?>
// Then include the rest of the code as is
The $comment->user_ID variable will return a true if the comment poster is a registered user and was logged in when they posted the comment. The code above will assign your custom CSS class to the div if it does indeed return true. If not, it will simply apply the standard class and styling.
There is also a really good tutorial for developing themes over at the Wordpress Codex. Definitely worth having a read through if you are unsure on what you need to do to create/edit your WordPress theme.
Edit: Cleaned up the answer and better explained the correct logic.