Home › Forums › Backend Issues (wp-admin) › Permalink ACF Field
Hello i need to a custom function but i don’t know if is it possibile with ACF.
I’ve add a new text field and in this field i put an url. I have a custom template home page with in a loop appear post from only same category. I need that the post where i add the link in my ACF text field (with name url_field_past) have a custom permalink.
So when user click on title of this post (where i’ve use the “url_field_past” ACF Field) not redirect on your permalink but redirect to url that i’ve add using my custom field.
Is possibile? How can i do that?
I find this but i don’t know how ca apply it only is i use the field
// START External Permalink - Links
add_filter( 'post_link', 'wpdevelopers_permalink_links', 10, 2 );
function wpdevelopers_permalink_links( $link, $post )
{
$meta = get_post_meta( $post->ID, 'link_url', TRUE );
$url = esc_url( filter_var( $meta, FILTER_VALIDATE_URL ) );
return $url ? $url : $link;
}
// END External Permalink - Links
Thanks
Hi @serviceweb
It depends on your template. In the post loop, I believe you can replace the link with this one:
<?php the_field('url_field_past'); ?>
I hope this helps 🙂
Hi @serviceweb
Looking at your code, I believe you need to put the code in “content-loop.php” file. You can try this code if you want:
<?php
$the_link = get_permalink();
if( get_field('url_field_past') ) {
$the_link = get_field('url_field_past');
}
?>
<a href="<?php echo $the_link; ?>" ><?php the_title(); ?></a>
If you don’t know how to code or not familiar with WordPress template, I suggest you hire a developer to help you out with it, and I’d recommend looking for one on https://studio.envato.com/ or https://www.upwork.com/.
Hope this helps 🙂
Hello,
thanks. Good 🙂
Do you know if is possibile limit the excerpt of post (with a specific number) in relationship if my field (url_field_past) has use?
For example
if get_field (‘url_field_past) else limit_content(get_the_excerpt() 500,); ?
Hi @serviceweb
If you want to limit the amount of the excerpt, you can use the wp_trim_words() function. Maybe something like this:
if( get_field('url_field_past') ) {
echo wp_trim_words( get_the_excerpt(), 100);
} else {
echo get_the_excerpt()
}
I hope this helps 🙂
Hello,
i have one more problem. I have, under post, a row where see a “Read Also” and i need in this page not appeare the post have url_field_past field.
This is my code:
Thanks
Hi @serviceweb
I believe you need to set the value option too. Maybe something like this:
$args = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'url_field_past',
'compare' => '!=',
'value' => ''
)
)
);
$query = new WP_Query( $args );
If that doesn’t work, could you please try the following code instead?
$args = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'url_field_past',
'compare' => 'NOT EXISTS',
)
)
);
$query = new WP_Query( $args );
I hope this helps 🙂
Hello, thanks for reply. No, not work.
I try to put before
$catid= "";
/*foreach((get_the_category()) as $category) {
$catid = $category->cat_ID . ' ';
}*/
But continue to see the post that have value on custom field on loop latest post. Any new idea?
Hi @serviceweb
Please keep in mind that you need to use the result of this query instead of showing the result from $wpdb
class. Could you please try the following code?
$args = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'url_field_past',
'compare' => '=',
'value' => ''
),
array(
'key' => 'url_field_past',
'compare' => 'NOT EXISTS',
)
)
);
$the_posts = get_posts($args);
echo <pre>;
print_r($the_posts);
echo ;
Thanks 🙂
Hello thanks for reply. I try but now have a blank page.
Thanks for information.
I set this code:
$idinsiede = get_cat_ID( $catprima[0]->cat_name);
$args = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'url_field_past',
'compare' => '=',
'value' => ''
),
array(
'key' => 'url_field_past',
'compare' => 'NOT EXISTS',
)
)
);
$the_posts = get_posts($args);
echo <pre>;
print_r($the_posts);
global $args;
$query = "SELECT count FROM $args->term_taxonomy WHERE term_id = $idinsiede";
$num = $wpdb->get_col($query);
$ins = $num[0]-1;
Is it right?
Hello,
If you would like to use custom post fields (generated by ACF) in URL structure without modyfying the WP_Query you can use the plugin:
https://github.com/athlan/wordpress-custom-fields-permalink-plugin
Cheers,
Athlan
The topic ‘Permalink ACF Field’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.