WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. eberger3
    Member
    Posted 11 months 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?

    Thanks,
    Erik

    http://wordpress.org/plugins/reusable-text-blocks/

  2. eberger3
    Member
    Posted 11 months 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); }
    }

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.