Reusable Text Blocks
[resolved] Get array of all text block titles (2 posts)

  1. eberger3
    Posted 2 years ago #

    Hi Hal - great plugin.

    I'm trying to use your plugin on certain tag and category pages where the tag slug matches the Reusable Text Block title so if the url is:

    http://www.domain.com/tag/cool-stuff and the text block title is "Cool Stuff" it will work via the php template code:

    <?php if(function_exists('show_text_block')) { echo show_text_block('cool-stuff', true); } ?>

    However, I'll use a variable based on the current tag url segment instead of hardcoding 'cool-stuff'.

    My issue is that I need to be able to get an array of all of the text block titles that are currently in the database so I can write an if(in_array() ){} saying that if the user is on a tag page with a url segment that matches a title of the text box, then show the text box.

    How can I get all of the titles into an array?



  2. eberger3
    Posted 2 years ago #

    Hey, I figured this out.

    $textblocks = $wpdb->get_col( "SELECT post_name FROM $wpdb->posts WHERE post_type = 'text-blocks' AND post_status = 'publish'" );

    worked for me so now I can do something like this:

    // check if user is on a tag page or category page
    if (is_tag() || is_category() ){

    // get tag or category segment from url
    $r = $_SERVER['REQUEST_URI'];
    $r = explode('/', $r);
    $r = array_filter($r);
    $r = array_merge($r, array()); //reset keys
    $URLseg= strtolower($r[1]);

    // get all text block titles
    $textblocks = $wpdb->get_col( "SELECT post_name FROM $wpdb->posts WHERE post_type = 'text-blocks' AND post_status = 'publish'" );

    // if page url matches an existing text block...
    if(in_array($URLseg, $textblocks)){

    // echo text block!
    if(function_exists('show_text_block')) { echo show_text_block($URLseg, true); }

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Reusable Text Blocks
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic


No tags yet.