I'm saving some extra information on each wordpress tag using an textarea field and a function:
This is the textarea field (using stripslashes):
<textarea name="Tag_meta[related_links]" id="Tag_meta[related_links]" size="25" placeholder="enter here the links html"><?php echo stripslashes($tag_meta['related_links'] ? $tag_meta['related_links'] : ''); ?></textarea>
This is the saving function:
add_action ( 'edit_term', 'save_termmeta_tag');
// save extra category extra fields callback function
function save_termmeta_tag( $term_id ) {
if ( isset( $_POST['Tag_meta'] ) ) {
$t_id = $term_id;
$tag_meta = get_option( "tag_$t_id");
$tag_keys = array_keys($_POST['Tag_meta']);
foreach ($tag_keys as $key){
if (isset($_POST['Tag_meta'][$key])){
$tag_meta[$key] = $_POST['Tag_meta'][$key];
}
}
//save the option array
update_option( "tag_$t_id", $tag_meta );
}
}
The problem is that in one of those saved fields (in wp_options table), I have a bit of HTML and the problem resides on the link inside of that HTML.
As you can see on the example bellow, this is the HTML entered on the textarea field (before saving the form):
<li><a rel="nofollow" target="_blank" href="https://www.blablabla.com/the-rest-of-the-link">link</a></li>
This is the way it's being saved in wp_options table:
<li><a rel=\"nofollow\" target=\"_blank\" href=\"https://www.blablabla.com/the-rest-of-the-link\">link</a></li>
Please note: The saving process also ads extra's \ each time I save the options. eg:
Saving one:
<li><a rel=\"nofollow\" target=\"_blank\" href=\"https://www.blablabla.com/the-rest-of-the-link\">link</a></li>
Saving two:
<li><a rel=\\\"nofollow\\\" target=\\\"_blank\\\" href=\\\"https://www.blablabla.com/the-rest-of-the-link\\\">link</a></li>
etc..
And this is the way it's being outputed (by the echo):
http://www.mydomainname.com/https://www.blablabla.com/the-rest-of-the-link\"
What might be wrong here?
Working solution:
Replace this on the textarea:
<?php echo stripslashes($tag_meta['related_links'] ? $tag_meta['related_links'] : ''); ?>
by this:
<?php echo stripslashes($tag_meta['related_links'] ? $tag_meta['related_links']) : ''; ?>
and on the echo do this:
echo stripslashes($tag_meta['related_links']);
Edited:
Answer corrected with great contribution of #bossman
Related
The code below is displaying the title in black text before it displays blue title with hyperlink under it.
I only want the link to appear.
if ( $query2->have_posts() ) {
// The 2nd Loop
while ( $query2->have_posts() ) {
$query2->the_post();
if ($post->ID == $do_not_duplicate)
continue;
$permalink = get_the_permalink($query2->post->ID);
$ID = $post->ID;
$titleAtribute = the_title_attribute();
$title = get_the_title();
echo '<h2 id="post-' .$ID.' ">
<a href="'.$permalink.'" rel="bookmark" title="Permanent Link to '.$permalink.' ">
'.$title.'</a></h2>';
}
// Restore original Post Data
wp_reset_postdata();
}
For example, on my website: http://skkelti.cz/, the following text appears in black above the link with the same text:
-Martin Davídek ml. : „Fanoušci jsou vždy to, co vás žene kupředu“-
Where is this coming from and what do I need to do to stop it from appearing?
The problem is with the_title_attribute(). This is displaying the value directly instead of returning it.
The function accepts echo in $args to specify whether to display or return the value. The default value is true (to display it), so pass false to return the value e.g.:
$titleAtribute = the_title_attribute('echo=0');
I made the ACF plugin group with files to download. In group I have fields "File 1", "File 2"...etc.
I would like to display all attached files to page. It is possible to display all fields belong to group? I try with basic code, but in this case I have only 1 file.
How can I add iteration to this or display all fields?
<?php
$file = get_field('attachment_1');
if( $file ):
// vars
$url = $file['url'];
$title = $file['title'];
$caption = $file['caption'];
if( $caption ): ?>
<div class="wp-caption">
<?php endif; ?>
<ul>
<li><a href="<?php echo $url; ?>" title="<?php echo $title; ?>">
<span><?php echo $title; ?></span>
</a>
</li>
<ul>
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
</div>
<?php endif; ?>
<?php endif; ?>
As all your fields are set up individually, it isn't just a matter of looping through an array of all your fields of the same type (i.e. just your file fields).
There are a few ways that might work for you:
Option 1.
If all the field names for your files follow the same naming pattern and are named sequentially, you could loop using the name.
Example, assuming your fields are named attachment_1 up to attachment_5:
$statement = get_field('name_of_your_statement_field');
//do whatever you need to with $statement
for ($i=1; $i<=5; $i++){
//use the number from the loop to find the file by name
$file = get_field('attachment_'.$i);
if( $file ){
// display file details as appropriate
}
}
Option 2.
If the file field names do not follow the same pattern, you could loop through an array of the field names.
Example:
$statement = get_field('name_of_your_statement_field');
//do whatever you need to with $statement
// Create an array with the field names of all your files
// N.B. This also lets you specify the order to process them
$file_fieldnames = array('file_1', 'file2', 'another_file');
foreach ($file_fieldnames as $fieldname) {
$file = get_field($fieldname);
if( $file ){
// display file details as appropriate
}
}
Option 3. If you want to loop through ALL fields on the post/page, you can save the fields into an array.
This might seem like the most generic approach at first, but it is complicated by the fact that you don't know what type each field is in order to know how to process and display them... you first have to work out what field type it is. You could do this by name (similar to above) or you could try to identify what each field by checking the field content.
Note, checking the field content is very risky, as there are other field types that can have similar featured (e.g. a file is not the only type that can have a url) so I wouldn't advise that strategy unless you are 100% sure you'll never change the field group or add another field group to the post/page.
Example:
$fields = get_fields();
foreach ($fields as $fieldname => $content) {
if (is_string ($content)){
// display the string
}
else if (is_array($content) && $content['url']) {
// then you could assume its a file and display as appropriate
}
}
Note that none of this code is tested. However it should give you an idea of the logic behind each option so you can decide what works for you.
UPDATE based on new code provided:
See below based on the code in your JSFiddle. I've ignored the caption outside the file list because it makes no sense - every file can have its own caption.
<?php
for ($i=1; $i<=5; $i++){
//use the number from the loop to find the file by name
$file = get_field('attachment_'.$i);
if( $file ){
$url = $file['url'];
$title = $file['title'];
$caption = $file['caption'];
// now display this file INSIDE the loop so that each one gets displayed:
?>
<li>
<a href="<?php echo $url; ?>" title="<?php echo $title; ?>" target="_blank">
<span><?php echo $title; ?></span>
</a>
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
<?php endif; ?>
</li>
<?php
} // end if
} // end for loop
?>
<ul>
If you understand arrays, I'd suggest you add the file details into an array and then do a second loop to display the files... however I'm guessing you're not that proficient with basic coding constructs as you don't understand loops, so I've tried to keep it simple. I strongly recommend that you do some tutorials on programming basics if you are attempting to write code.
Im using lightgallery so i need to load images before call them in lightgallery. Problem is that images are large so it takes too much time to load. Is there any way to load that specific gallery when user click on link.
<div id="lightgallery-<?php echo get_the_ID(); ?>" class="hidelightgallery">
<?php
foreach ($files as $image) {
$image_attributes = wp_get_attachment_url( $image->ID );
$attachment_title = get_the_title($image->ID);
$caption = get_post_field('post_excerpt', $image->ID);
?>
<a class="item" href="<?php echo $image_attributes ?>" data-sub-html="<?php echo $attachment_title; ?> <?php if($caption!= '') echo ' - ' ?> <?php echo $caption ?>"><img src="<?php echo $image_attributes ?>"></a>
<?php } ?>
</div>
Now what i want is if user click for example link with this id then do foreach. Is that possible?
Just follow these steps,
create new template / html page where you will write html and populate by foreach loop
add your id = lightgallery whole code to that html page
when you will click on your link (which you mentioned) fire an ajax
Ajax function will get some id or number of images need to show or your logic on how you will populate data in foreach loop
in php you will get all relevant data, and you will populate that data in html file you created in step 1
php function will return that data to ajax function
Ajax function will get all your dynamic html data
populate that html where ever you want or just append that html wherever you want
Go step by step, this will solve your problem.
Im working on customizing a Wordpress theme with some custom fields. For this I'm using the plugin "Advanced Custom Fields", but I want it to display these fields ONLY IF something is written in them. This is the code I'm using to display the custom fields:
<p class="tittelboks">Artikkelforfatter:</p>
<?php if( get_field('artikkelforfatter') )
{
echo '<p>' . get_field('artikkelforfatter') . '</p>';
} ?>
How do I change the code so that it only echos the information AND the label (in this case .tittelboks) if something is written in the meta boxes?
Michael
<?php if( $field = get_field('artikkelforfatter') ): ?>
<p class="tittelboks">Artikkelforfatter:</p>
<p><?php echo $field; ?></p>
<?php endif; ?>
This is a different way of doing an if statement, so you don't have to enclose your HTML in quotes and worry about escaping. The two lines in the middle will only print out if get_field('artikkelforfatter') returns a value or true. That value will be assigned to the $field variable.
Something like this should work:
<?php
$artikkel = get_field( 'artikkelforfatter' );
if ( ! empty( $artikkel ) ) {
?>
<p class="tittelboks">Artikkelforfatter:</p>
<p><?php echo $artikkel; ?></p>
<?php
}
?>
To use and display custom fields need you may try to put this in your loop.
<?php $what_name_you_want=get_post_meta($post->ID,'Your Custom Field Name',true); ?>
<?php echo $what_name_you_want; ?>// This call the value of custom field
Only replace
what_name_you_want with your favorite name
and
Your Custom Field Name with the name of custom field
If the value of custom field is empty the echo will be empty to.
Tell me if it's work
I'm currently trying to create a site for TV shows and due to certain wordpress limitations this is becoming a challenge.
However I bypass that with the use of implementing custom meta fields in the functions.php file, now my problem is that I need it to actively create new fields when I submit information in the current field. For example custom metabox names are
(Episode Name="This is It") (Episode Number="1") (Season Number="5")
Instead of having to create all the boxes from the beginning I would like the use Javascript (jQuery) or any solution to automatically create a new set of these 3 boxes
(Episode Name="") (Episode Number="") (Season Number="")
so I can just enter the new information as they come. Thank you in advance for your help!
Note: I have invested too much time into wordpress to just switch to another cms, so that is not an option at this point in time.
from what I understand of your question, you are looking for a simple solution to automate the input process. I have a general idea of what it is you nee to achieve as I have had to do something similar on a brochure type of website.
I have tried answering your question using Jquery, but I find it to increase the amount of text input required when creating your post, there is unfortunately no completely automated method of doing it, but hopefully below would provide you with a solution.
first I found the following plugin: Types - Complete Solution for Custom Fields and Types here: http://wordpress.org/extend/plugins/types/
This allows you to create custom meta feilds when creating a new post/page. The custom feilds are brought added with a perfix of "wpcf-" and then the name of the field, e.g. "Episode Name" becomes "wpcf-episode-name" in the database.
The following is an edit of wordpress's get_meta function:
function get_specifications(){
if ( $keys = get_post_custom_keys() ) {
echo '<div class="entry_specifications">';
foreach ( (array) $keys as $key ) {
$keyt = trim($key);
if ( '_' == $keyt[0] )
continue;
$values = array_map('trim', get_post_custom_values($key));
$value = implode($values,', ');
//remove "wpcf-"
$key = str_replace('wpcf-','',$key);
//convert "-" to a space " "
$key = str_replace('-',' ',$key);
//check if it has a value, continue if it does, skip if it doesn't:
if ($value <> ''){
echo apply_filters('the_meta_key', "
<div class='meta_wrapper'>
<div class='meta_title'>$key</div>
<div class='meta_value'>$value</div>
</div>\n", $key, $value);
};
}
}
// echo '</div>'; comment out and remove the line below if you are not using floats in your css
echo '</div><div class="clear"></div>';
}
In my page.php (or your template-page.php) I have added the following code when/where I want the meta to be produced:
<?php
//check to see if there is meta data, as you only want the meta data on TV Program pages
$met_check = get_post_meta($post->ID, 'wpcf-features', true);
if ($met_check <> ''){
//if it has a value, spit out "About EpisodeName" ?>
<h2 class="post-title clear">About <?php echo $title ?></h2>
<?php //perform the function from functions.php and return results:
echo get_specifications();
}
?>
I've styled the result with the following CSS:
.meta_entry {
clear:both;
}
.meta_title {
float:left;
text-transform:capitalize;
width:200px;
}
.meta_value {
float:left;
width:480px;
}
Hope this helps mate!
There is a wonderful plugin for wordpress called Pods which might be a viable solution.
Try http://wordpress.org/extend/plugins/custom-field-template/