• The end result I need is a dynamically chosen Call to action Image on the top of the page that is easy to edit and is different for different pages. For the sake of user-friendliness I’d like to build in a metabox option on each Page/Post that allows the user to select which (if any) Call to Action from a dropdown.

    Those various Call to Actions are currently being populated by a Custom Post Type, but I’m open to other options. What I’d like is to have the metabox select be populated with a new option for each of new Call To Action Custom Post Types that is added.

    Is there a more efficient way to do this?

    I have a staging site at test.josiahmann.com.

Viewing 6 replies - 1 through 6 (of 6 total)
  • I think what you’ve done so far is pretty good. I’d most likely do it the same way.

    One thing that I have found is that there’s always trade-offs, and getting a little bit more efficiency out of a system that’s a whole lot more complex just isn’t worth it. Users don’t notice an extra 1/10ht of a second overall, but you’ll certianly notice the time differnce when you come back to de-bug something in your code later on and it’s a huge jumbled mess.

    Thread Starter josiahmann

    (@josiahmann)

    Thanks catacaustic. Any other thoughts on how to complete?

    What part are you stuck on?

    Thread Starter josiahmann

    (@josiahmann)

    I don’t know how to make the metabox select populate based on the Custom Posts. FYI, I’m a PHP beginner so this is pretty new to me.

    OK, that makes sense. ??

    To get the values for a drop-down select or any other sort of list you can use get_posts() with a foreach() loop. Assuming that your custom post type is registered as ‘foowidget’, you could do something like this:

    <?php
    $foo_widgets = get_post( array(
        'numberposts' => -1,
    	'post_type' => 'foowidget'
    ));
    ?>
    <select name="foowidget">
        <option value="">Please choose...</option>
        <?php foreach ($foo_widgets as $foo_widget): ?>
        	<option value="<?php echo $foo_widget->ID; ?>"><?php echo $foo_widget->post_title; ?></option>
        <?php endforeach; ?>
    </select>

    There’s other things that you can add in to make it more complicated, but that’s the baiscs for the process. From here you can make changes to it as your system needs.

    Thread Starter josiahmann

    (@josiahmann)

    That is very helpful.

    I’ve got that in and changes the CPT name, but it doesn’t populate the metabox. I changed it to a wp_query and that populated the box, but I don’t know that it’s the best way to code it.

    What am I missing?

    function cd_meta_box_cb () {
    	$foo_widgets = get_post(array (
                'numberposts' => -1,
    	    'post_type' => 'call_to_action'
    ));
    ?>
    	<select name="foowidget">
      	  <option value="">Please choose...</option>
     	   <?php foreach ($foo_widgets as $foo_widget): ?>
     	   	 <?php if ($foo_widget->post_status == 'publish') :?>
    			<option value="<?php echo $foo_widget->ID; ?>"><?php echo $foo_widget->post_title; ?></option>
    		<?php endif;?>
     	   <?php endforeach; ?>
    	</select>
    <?php
    }

    Thanks again for your help.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Help with dynamic metabox functionality calling a Custom Post Type’ is closed to new replies.