• Resolved adaptiman

    (@adaptiman)


    I’ve created a $query returns an array of posts that meet a certain taxonomy parameter:

    $control_query_params = array(
    	'post_type' => 'control',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'control-group',
    			'field' => 'slug',
    			'terms' => $family
    		)
    	)
    );
    
    //return $control_query_params;
    $query = new WP_Query( $control_query_params );

    Immediately following this block, I use the_shortlink() function to output links to the posts like this:

    $output = '<ul>';
    if ($query->have_posts()) {
    	while ($query->have_posts()) {
    		$query->the_post();
    		// do something
    		$output .= the_shortlink($text = $the_title, $title = $the_title, $before = '<li>', $after = '</li>' );
    	}
    } else {
    	// no posts found
    	$output .= 'No posts found.';
    }
    $output .= '</ul>';
    return $output;

    For some reason, this code returns a bulleted list of links, which link to the proper posts, and have a popup title with the post title, but the link text is the DEFAULT link text for the the_shortlink() function, like this:

    
    <a href="#">This is the short link.</a>

    So the title variable for the link displays the correct text ($the_title) but the text variable does not. Any idea why?

    • This topic was modified 2 years, 6 months ago by Jan Dembowski.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Firstly, the_shortlink() doesn’t return a value, it outputs it, so having it in your code as you have there won’t work anyway.

    With the way you have called that function, where does the value for $the_title come from? You aren’t showing that anywhere, so if there’s no value set for that, it will show the default text, which is what you’re seeing. Check where the value for $the_title is being set, because it doesn’t look like it’s set anywhere.

    You’re also not setting the values crorectly. It looks like you’ve jsut cut-and-pasted the function call from the codex. You don’t pass values to a function and have any “=” signs in there… that’s only for the functions definition.

    Seeing as how you want to return some text, I’m assuming that something like this would work better…

    ob_start ();
    ?>
        <ul>
        <?php while ($query->have_posts()): ?>
            <li><?php the_shortlink ($the_title, $the_title); ?></li>
        <?php endwhile; ?>
        </ul>
    <?php
    $content = ob_get_contents ();
    ob_end_clean ();
    
    return $content;
    Thread Starter adaptiman

    (@adaptiman)

    Actually, it does work as described with one issue – not able to display the post_title as the link text. I found the problem was that I was calling the_title() rather than get_the_title(). Here’s the working function:

    add_shortcode( 'return_controls', 'build_controls');
    
    function build_controls($atts, $content = null, $tag = '') {
        $family = shortcode_atts( array(
    		'family' => 'Default Family'
    	), $atts);
    
        //return $family['family'];
    
    	$control_query_params = array(
    		'post_type' => 'control',
    		'orderby' => 'control_identifier',
    		'order' => 'ASC',
    		'tax_query' => array(
    			array(
    				'taxonomy' => 'control-group',
    				'field' => 'slug',
    				'terms' => $family
    			)
    		)
    	);
    
    	//return $control_query_params;
    	$query = new WP_Query( $control_query_params );
    
    	$output = '<ul>';
    	if ($query->have_posts()) {
    		while ($query->have_posts()) {
    			$query->the_post();
    			// do something
    			$mytitle = get_the_title() . ' (' . rwmb_get_value( 'control_identifier' ) .')';
    			$output .= the_shortlink($mytitle, $mytitle, '<li>', '</li>' );
    		}
    	} else {
    		// no posts found
    		$output .= 'No controls found.';
    	}
    	
    	$output .= '</ul>';
        return $output;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘the_shortlink() returns default text string’ is closed to new replies.