I have a repetitive sentence throughout my site with different parameters.

Example: NAME read a book on DAY | TITLE

where the capitalized words vary with each post and (here's the kicker) I want DAY to be a link - one of seven individual links (one for each day of the week).

I have seven shortcodes for the links with each day of the week and a shortcode for the sentence but I can't quite get them to work together.

function books($atts, $content = null) {
    "name" => '',
    "day" => '',
    "title" => '',
), $atts));
$output = '<div class="cite">';
if($name) { $output .= ''.$name.' read a book';}
if($day) { $output .= ' on '.do_shortcode($content).'';}
$output .= ' | '.$title.'</div>';
    return $output;
add_shortcode("books", "books");

And then the code for the days of the week is pretty straight forward:

function monday() {        
return '<a href="http://website.com/" target="_blank">Monday</a>'; 
add_shortcode("monday", "monday");

In my post

[books name="Mary Sue" title="See Jane Run"]

works just fine and outputs: Mary Sue read a book | See Jane Run


[books name="Mary Sue" title="See Jane Run" day="[monday]"]

goes wonky and outputs: "Mary read a book on | "See

I feel like I may be approaching the problem wrong, but am not sure how else to go about defining several different variables in each post and keep the consistency and simplicity of reusing the same text.

1 Answer 1


The days of the week shortcodes aren't needed. Also, the way you're trying to use them isn't allowed. Why not just use the title and do what you want in the books shortcode?

function books($atts, $content = null) {
  $atts = shortcode_atts( [
    "name" => '',
    "day" => '',
    "title" => '',
  ], $atts );

  $atts[ 'day' ] = ucfirst( strtolower( $atts[ 'day' ] ) );

  //* If you want to change the URL, you could switch on the day
  switch( $atts[ 'day' ] ) {
    case 'Monday':
      $url = 'https://example.com/';
    case 'Tuesday':
      $url = 'https://someotherurl.com/';
    //* etc.

  return sprintf( 
    '<div class="cite">%1$s%2$s | %3$s</div>',
    '' === $atts[ 'name' ] ? '' : $atts[ 'name' ] . ' read a book',
    '' === $atts[ 'day' ] ? '' : 
      sprintf( ' on <a href="%1$s" target="_blank">%2$s</a>', $atts[ 'day' ], $url ),
    $atts[ 'title' ]
  • This makes great sense as an alternate approach but I can only get it to 80% work. I copied the above code as a test then used: [books name="June Smith" day="Monday" title="See jane Run"] in my post and this is what it output: June Smith read a book on example.com | $3June Smith read a book It's duplicated and outputting the url instead of as a link without the title info and I'm not sure why.
    – A J
    Commented Mar 7, 2018 at 22:18
  • It had a typo. It should be %3$s not $3%s. Editted. Commented Mar 8, 2018 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.