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');
Related
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
Just like the title says. I'm trying to only show a line of code on a website if a value is set via a front end panel. The code which I currently have is like this:
For text field input:
<li id="mh-estate_attribute" class="mh-estate__list__element">
<strong>Energieträger:</strong>
<a>
<?php
$terms = get_the_terms( $post->ID, 'energietraeger' );
foreach($terms as $term) {
echo $term->name;
}
?>
</a>
</li>
For number input:
<li id="mh-estate_attribute" class="mh-estate__list__element">
<strong>Baujahr (lt. Energieausweis):</strong>
<?php the_field('estate_attr_' . 'attribute_21', $myhome_estate->get_ID() ); ?>
</li>
So the problem is that an error code is shown on my website when no value is put in.
I'm trying to only show a line of code on a website if a value is set
Try,
if(isset($variable)){
//do what you want to do with not NULL variable
}
OR more specifically
if(!empty($variable){
//do what you want to do with non-empty variable
}
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.
I have a sidebar menu that is created if a specific tag is found in a category. For example
Apples
Oranges
When clicked, the URL pattern would be as below:
/category/?tag=apples
The code that renders the menu is as below:
<?php if ( $term->slug == 'apples' ) {?>
<section class="item">
<h4><a href="<?php $category_id = get_query_var('cat');
echo get_category_link( $category_id );?>?tag=apples"> Apples</a>
</h4></section>
<?php } if ( $term->slug == 'oranges') {?>
<section class="item">
<h4><a href="<?php $category_id = get_query_var('cat');
echo get_category_link( $category_id );?>?tag=oranges"> Oranges</a>
</h4></section>
<? } ?>
<?php } } ?>
I'm having a difficult time to highlight "Apples" with a red background when that is the active URL pattern, and the same with Oranges having an orange background.
I have tried various Jquery fiddle examples that works perfectly on fiddle, but not when i implement them. Most are for static html pages which is easier to do.
This is probably something you should put in a loop, but an example for your apples section:
<section class="item <?php echo ($term->slug === $_GET('tag')) ? 'active' : '';">
<h4><a href="<?php $category_id = get_query_var('cat');
echo get_category_link( $category_id );?>?tag=apples"> Apples</a>
</h4></section>
Note that I have added an active class to the section element so that you can style that (or its children) differently.
Why not verify the $_GET variable and then highlight the active URL ?
if ($_GET['tag'] == 'apples')
echo ''; //Highlight : ON, with CSS
else
echo ''; //Highlight : OFF
Same goes for "oranges".
Since you tagged it with jQuery, you can solve it on the client side:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" :
decodeURIComponent(results[1].replace(/\+/g, " "));
}
$(function(){
var tag = getParameterByName('tag');
if(tag){
$('h4').filter(function(){
return $(this).text().toLowerCase() === tag;
}).css({ color: 'red' });
}
});
Allthough I suggest you solve it directly on the server using PHP. Just an alternative.
I'm attempting to fetch the subtitle from the PARENT page and echo the text within a title tag.
Function:
<?php
$id_to_get = $post->ID;
if ( $post->post_parent ) {
$id_to_get = $post->post_parent;
}
$parent = $id_to_get;
$title = get_the_title($parent);
$subtitle = get_post_meta($id_to_get, '_base_page_subtitle', true);
?>
HTML Implementation: ...title="<?php echo $subtitle; ?>">
Unfortunately, it is not successfully grabbing the subtitle text. I'm able to populate the title echoing the variable $title, just not with the variable $subtitle.
The value for the Subtitle option is stored within the table "_base_page_subtitle"; I'm using options framework.
I know my issue is with the if statement, however, being new to PHP, I'm having a bit of difficulty figuring out how to capture the data and display it properly.
Thanks again of time!
you can do it without a function - just echo it straight out
title="<?php if ( $post->post_parent ) { echo get_post_meta($post->post_parent, '_base_page_subtitle', true);}else{echo get_post_meta($post->ID, '_base_page_subtitle', true);}?>"