Support

Account

Home Forums Backend Issues (wp-admin) Custom link with fallback if empty?

Solved

Custom link with fallback if empty?

  • Hello,

    I’m using ACF to link featured posts (which show up on the main page) to pages instead of the post itself. Works just fine, just have to inserte “the_field” and “get_field” where the original URLs where build.

    Now I wanted to link several posts to the post itself using the original permalink but I’m stuck creating a working fallback.

    Here’s what I do at the moment:

    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    	<a class="post-thumbnail" href="<?php the_field('link'); ?>">
    	<?php
    		// Output the featured image.
    		if ( has_post_thumbnail() ) :
    			if ( 'grid' == get_theme_mod( 'featured_content_layout' ) ) {
    				the_post_thumbnail();
    			} else {
    				the_post_thumbnail( 'twentyfourteen-full-width' );
    			}
    		endif;
    	?>
    	</a>
    
    	<header class="entry-header">
    		<?php if ( in_array( 'category', get_object_taxonomies( get_post_type() ) ) && twentyfourteen_categorized_blog() ) : ?>
    		<div class="entry-meta">
    			<span class="cat-links"><?php echo get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'twentyfourteen' ) ); ?></span>
    		</div><!-- .entry-meta -->
    		<?php endif; ?>
    
    		<?php the_title( '<h1 class="entry-title"><a href="' . get_field('link') . '" rel="bookmark">','</a></h1>' ); ?>
    	</header><!-- .entry-header -->
    </article><!-- #post-## -->

    As you can see, I simply replaced the original “get_permalink()” with the ACF functions. What I would need now is an extention for these function(s) to set the “the_field” to the permalink if no Link is selected in the post editor.

    I did this before without ACF by just setting and checking variables but I think there’s a better way?!

    Thanks!

  • Hm, I just tought I could try something different but I endet up with another problem …

    What I tried:

    I added 3 custom fields, 1st let’s the user select where the featured post should link to > I used radio buttons “page” and “post” to select.

    The 2nd custum field let’s the user choose a page to link to using the existing selct box in ACF. This field only shows up when the 1st is set to “page”

    The 3rd shows up when “post” is selected and uses the existing select box from ACF for posts.

    First I tried to simply point each of them to the same name “link”, I thought that should work but it did not 🙁 Now I’m stuck trying to handle two names (link1 and link2) but have no idea how I can do this with the above code…

  • puh, just solved the prob., I think I was tricked by some missunderstanding…

    For the “link to a post” option I’ve set the field type to “article” and set the article type to post. That did not work because you have to set field type for both opations to “link to page” and then set article type to page or post.

    Maybe it’s a problem with the german translation, don’t know which description are used in the englesh version …?!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Custom link with fallback if empty?’ is closed to new replies.